【发布时间】:2014-07-18 13:02:16
【问题描述】:
以下是我在运行 Android 4.1.2 的 Android 设备中的 HTTP POST 请求代码。我有问题:
-
此代码在三星设备上完美运行,而在 HTC 设备上给出“内容类型不是应用程序/json”。
public static String POST(String url, JSONObject obj) { Log.i("JSONPOSTBEGIN", "JSON POST 开始"); 输入流 inputStream = null; 字符串结果 = "";
HttpClient httpclient = new DefaultHttpClient(); try { HttpPost post = new HttpPost(url); post.setHeader("Content-type", "application/json"); post.setHeader("Accept", "application/json"); StringEntity se = new StringEntity(obj.toString()); se.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE, "application/json")); post.setEntity(se); HttpResponse httpResponse = mHhttpclient.execute(post); // receive response as inputStream inputStream = httpResponse.getEntity().getContent(); // convert inputstream to string if(inputStream != null) { result = convertInputStreamToString(inputStream); } else result = "Did not work!"; } catch (Exception e) { Log.d("InputStream", e.getLocalizedMessage()); } Log.i("JSONPOSTEND", "End of JSON data post methos..."); return result;}
【问题讨论】: