2014-02-28 151 views
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()); 
+0

500內部服務器錯誤。這意味着出了問題服務器端 – panini

+0

我也有[同樣的問題](http://stackoverflow.com/questions/18840601/android-json-post-response-status-code-500-and-status-line-內部服務器呃)和我的後端人員解決了這個問題。看起來與你的代碼錯誤..檢查後端.. –

+0

我可以知道你的協議是什麼,http或https? – Dev

回答

1

你可以嘗試使用離子爲進行HTTP請求更容易https://github.com/koush/ion,支持標題和正文PARAMS。還檢查Web服務「500」是一個內部服務器錯誤