【发布时间】:2013-04-19 19:36:14
【问题描述】:
服务器采用两个参数:String 和 JSON。
提示,我正确地在 POST 请求中传输JSON 和字符串?
try {
HttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost("my_url");
List parameters = new ArrayList(2);
JSONObject jsonObject = new JSONObject();
jsonObject.put("par_1", "1");
jsonObject.put("par_2", "2");
jsonObject.put("par_3", "3");
parameters.add(new BasicNameValuePair("action", "par_action"));
parameters.add(new BasicNameValuePair("data", jsonObject.toString()));
httpPost.setEntity(new UrlEncodedFormEntity(parameters));
HttpResponse httpResponse = httpClient.execute(httpPost);
Log.v("Server Application", EntityUtils.toString(httpResponse.getEntity())+" "+jsonObject.toString());
} catch (UnsupportedEncodingException e) {
Log.e("Server Application", "Error: " + e);
} catch (ClientProtocolException e) {
Log.e("Server Application", "Error: " + e);
} catch (IOException e) {
Log.e("Server Application", "Error: " + e);
} catch (JSONException e) {
e.printStackTrace();
}
【问题讨论】:
-
您是否遇到错误?你的问题到底是什么?
-
没有错误。我的问题 - 这个实现正确传输 JSON 吗?
标签: android json post http-post