我需要将json字符串{'post_id':3}发送到我的服务器。我使用put方法。当我运行它时,我的服务器出现500错误。我正确发送json数据吗?并帮忙吗?
HttpClient httpclient = new DefaultHttpClient();
HttpPut httput = new HttpPut("my url");
HttpParams params2 = httpclient.getParams();
HttpConnectionParams.setConnectionTimeout(params2, 30000);
String responseBody = "";
HttpResponse response = null;
try {
String base64EncodedCredentials = "Basic " + Base64.encodeToString(
("username"+ ":" + "password").getBytes(),
Base64.NO_WRAP);
httput.setHeader("Authorization", base64EncodedCredentials);
httput.setHeader(HTTP.CONTENT_TYPE, "application/json");
JSONObject obja = new JSONObject();
try {
obja.put("post_id","3");
httput.setEntity(new StringEntity(obj.toString(), "UTF-8"));
// Execute HTTP Post Request
response = httpclient.execute(httput);
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
///
HttpEntity entity= response.getEntity();
Log.d(" ", " LIKE RESPONSE " + response.getStatusLine().getStatusCode());
答案 0 :(得分:1)
你可以尝试使用Ion for make http请求更容易https://github.com/koush/ion,支持header和body params。还要检查web服务“500”是内部服务器错误