0

我需要将 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());
4

1 回答 1

1

you can try using Ion for make http request is more easy https://github.com/koush/ion , support headers and body params .also check the web service "500" is a Internal Server Error

于 2014-02-28T04:43:14.523 回答