1. 程式人生 > >android下申請php json的方法

android下申請php json的方法

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.第一次寫部落格,本人又是技術男,表述力有限,請多多包容~