android下申请php json的方法
来源:互联网 发布:php 点击增加添加表单 编辑:程序博客网 时间:2024/06/05 15:17
1.android版本为4.0
2.网络权限已经开启
3.网上找到了一个写好的方法
------------------------------------------------
public static String SendRequest(String adress_Http, String strJson) {
String returnLine = "";
try {
System.out.println("**************开始http通讯**************");
System.out.println("**************调用的接口地址为**************" + adress_Http);
System.out.println("**************请求发送的数据为**************" + strJson);
URL my_url = new URL(adress_Http);
HttpURLConnection connection = (HttpURLConnection) my_url.openConnection();
connection.setDoOutput(true);
connection.setDoInput(true);
connection.setRequestMethod("POST");
connection.setUseCaches(false);
connection.setInstanceFollowRedirects(true);
connection.setRequestProperty("Content-Type", "application/json");
connection.connect();
DataOutputStream out = new DataOutputStream(connection
.getOutputStream());
byte[] content = strJson.getBytes("utf-8");
out.write(content, 0, content.length);
out.flush();
out.close(); // flush and close
BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream(), "utf-8"));
//StringBuilder builder = new StringBuilder();
String line = "";
System.out.println("Contents of post request start");
while ((line = reader.readLine()) != null) {
// line = new String(line.getBytes(), "utf-8");
returnLine += line;
System.out.println(line);
}
System.out.println("Contents of post request ends");
reader.close();
connection.disconnect();
System.out.println("========返回的结果的为========" + returnLine);
} catch (Exception e) {
e.printStackTrace();
}
return returnLine;
}
------------------------------------------------
4.传入一个url,和一个json格式的字符串
5.发现一直没有返回值,android老是提示没有找到文件。。。
6.后来自己找书写了一个貌似java的原始写法竟然成功了
-------------------------------
public class gg extends Thread {
@Override
public void run() {
HttpPost httpPost=new HttpPost("http://192.168.1.186/classUserJson.php");
List<NameValuePair> parms=new ArrayList<NameValuePair>();
parms.add(new BasicNameValuePair("name","123"));
parms.add(new BasicNameValuePair("pwd","123"));
try {
HttpEntity httpEntity=new UrlEncodedFormEntity(parms,"UTF-8");
httpPost.setEntity(httpEntity);
HttpClient httpClient=new DefaultHttpClient();
try {
HttpResponse httpResponse=httpClient.execute(httpPost);
if(httpResponse.getStatusLine().getStatusCode()==HttpStatus.SC_OK){
String uiString=EntityUtils.toString(httpResponse.getEntity(),"UTF-8");
Message msg = new Message();
msg.what = 0x01;
msg.obj = uiString;
handler.sendMessage(msg);
System.out.println(uiString);
}
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
-----------------------------
7.网上那个方法我一直在用也没有问题,为什么申请php的json就不行了呢?我想应该是php跟其他不一样吧,有可能是因为机制问题,也有可能是传参时android新版本不支持jsonobject 而是用了NameValuePair
8.第一次写博客,本人又是技术男,表述力有限,请多多包容~
- android下申请php json的方法
- php 下的json操作
- Android Map 申请Debug Key的方法
- 申请Android应用内存的方法
- Linux系统下内存申请应用的方法原理总结
- Linux系统下内存申请应用的方法原理总结
- centos下加php 的json扩展
- PHP JSON decode 的替代方法
- php构造json多维数组的方法
- 申请GMAIL的方法
- Android 6.0(API 23)及其以上动态申请的权限与申请权限的方法
- android 申请移动应用的签名生成方法
- android与php的json交互
- php和android的json解析
- android php mysql json
- Google code的申请方法
- 6.0申请权限的方法
- android下支持json的远程访问
- 更新Android SDK 出错 Failed to rename directory \temp\ToolPackage.old01
- Windows下Git Bash中文乱码
- jstree 操作
- LeetCode:Maximum Depth of Binary Tree
- 快速恢复网站SEO优化排名的技巧
- android下申请php json的方法
- 零基础学习在Android进行SDL开发系列文章
- java 线程的简单例子
- 软考——(5)计算机系统之CPU组成
- Eucalyptus-利用镜像启动一个Windows Server 2008r2实例
- html dtd文件解释
- Json 数据常见出错问题
- Notepad++前端开发常用插件介绍
- 常见的的样本数据归一化