0
我需要發送json字符串{'post_id':3}到我的服務器。我使用put方法。當我運行它時,我從我的服務器得到一個500錯誤。我是否正確發送json數據?並幫助嗎?發送json通過put
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());
500內部服務器錯誤。這意味着出了問題服務器端 – panini
我也有[同樣的問題](http://stackoverflow.com/questions/18840601/android-json-post-response-status-code-500-and-status-line-內部服務器呃)和我的後端人員解決了這個問題。看起來與你的代碼錯誤..檢查後端.. –
我可以知道你的協議是什麼,http或https? – Dev