如何在 Android 中发送 POST 方法,一个参数需要是 String 和另一个 JSON 对象?
字符串参数是“年龄”
JSONObject createRequest = new JSONObject();
try {
createRequest.put("ID", 2173);
createRequest.put("Name", "Munja");
createRequest.put("Address", "New York");
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// Add your data
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
//nameValuePairs.add(new BasicNameValuePair("client", createRequest.toString()));
nameValuePairs.add(new BasicNameValuePair("age", "21"));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
StringEntity se = new StringEntity( createRequest.toString());
se.setContentType(new BasicHeader(HTTP.CONTENT_TYPE, "application/json"));
httppost.setEntity(se);
// Execute HTTP Post Request
HttpResponse response = httpclient.execute(httppost);
但它不起作用,收到 400,就像它没有很好地形成一样。