2
我知道這個問題已被詢問了很多次。我也搜索了論壇,但無法找到我正在尋找的答案。Android ICS HTTP POST
該代碼適用於許多Android版本,但不適用於3.0或更高版本。 String response_from_server = null;
// Create a new HttpClient and Post Header
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(file_n_folder.URL_POST);
//Log.d("URL",file_n_folder.URL_POST);
try {
JSONArray postjson = new JSONArray();
postjson.put(obj);
// Post the data:
httppost.setHeader("json",obj.toString());
httppost.getParams().setParameter("jsonpost",postjson);
// Execute HTTP Post Request
HttpResponse response = httpclient.execute(httppost);
// for JSON:
if(response != null){
InputStream is = response.getEntity().getContent();
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
StringBuilder sb = new StringBuilder();
String line = null;
try {
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
response_from_server = sb.toString();
//Log.d("RESPONSE FROM SERVER",response_from_server);
return response_from_server;
}
}catch (ClientProtocolException e){
Log.d("EXCEPTION line 94",e.getMessage());
}catch (IOException e){
Log.d("EXCEPTION line 96",e.getMessage());
}
return response_from_server;
它總是在httpclient.execute(httppost)上崩潰應用程序;
你們可以請我建議我,什麼或如何解決這個問題?
感謝名單
我可以看到如何做到這一點的例子嗎? –