这是我的代码
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(1);
nameValuePairs.add(new BasicNameValuePair("nama", nama));
nameValuePairs.add(new BasicNameValuePair("pesan", s));
try {
HttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost("http://192.168.173.1/chatgroup/insert.php");
httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpClient.execute(httpPost);
HttpEntity entity = response.getEntity();
is = entity.getContent();
textchat.setText("");
getData();
} catch (ClientProtocolException e1) {
Log.e("ClientProtocol", "Log_tag");
e1.printStackTrace();
} catch (UnsupportedEncodingException e1) {
e1.printStackTrace();
} catch (IOException e1) {
Log.e("IOException", "Log_tag");
e1.printStackTrace();
}
我在 API 10[Gingerbread] 中构建了这个并成功工作,...
但是,如果我为 API 11 构建> 不起作用并强制关闭应用程序..这里的任何人都可以给我建议,..或者此代码仅适用于 api 10?
注意:我在manifest.xml中使用uses-permision Internet:D谢谢您的回复
最佳答案
尝试这样
try {
URL url = new URL(url);
HttpURLConnection conn = (HttpURLConnection)url.openConnection();
conn.setRequestMethod("POST");
conn.setRequestProperty("Content-Type", "application/json");
conn.setDoOutput(true);
JSONObject postDataParams = new JSONObject();
try {
postDataParams.put("nama", nama);
postDataParams.put("pesan", s);
} catch (JSONException e) {
e.printStackTrace();
}
PrintWriter out = new PrintWriter(conn.getOutputStream());
out.print(postDataParams);
out.close();
int responseCode=conn.getResponseCode();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
关于php - 为什么 Http Post 仅适用于 API 10?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36661668/