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.第一次写博客,本人又是技术男,表述力有限,请多多包容~


0 0
原创粉丝点击