2012-12-28 153 views
3

這裏是我的POST請求到服務器代碼。500內部服務器錯誤請

的JSON被張貼到服務器:

{ 
"User": {  
"Name": "dog","Password": "123" } 

} 

我是如何創建JSON對象

object = new JSONObject(); 
    JSONObject jsonObject = new JSONObject(); 
    try { 
     jsonObject.put("Name", "dog"); 
     jsonObject.put("Password", "123"); 
    } catch (JSONException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 

    try { 
     object.put("User", jsonObject); 
    } catch (JSONException e1) { 
     // TODO Auto-generated catch block 
     e1.printStackTrace(); 
    } 

發佈到服務器

public HttpResponse request(JSONObject request) 
     throws ClientProtocolException, IOException, IllegalStateException, 
      JSONException { 
      client = (DefaultHttpClient) WebClientDevWrapper 
        .getNewHttpClient(); 

    HttpPost post = new HttpPost(
      "https://wbapi.cloudapp.net:443/api/User/LocalLogin/"); 
    post.setEntity(new StringEntity(request.toString(), "utf-8")); 
    HttpResponse response = client.execute(post); 
    return response; 
} 

隨着提供的類通過帕雷什Mayani這裏Send HTTPS Post Request to the server

我收到響應對象。但是我的response.getStatusLine()一直只顯示500 /內部服務器錯誤的POST請求。

注:GET請求工作的罰款。

什麼是我的代碼的問題?我怎樣才能解決這個錯誤?

改變了請求代碼(作爲推薦Bhavdip Pathar)

public HttpResponse request(JSONObject request) 
     throws ClientProtocolException, IOException, IllegalStateException, 
     JSONException { 

    HttpPost post = new HttpPost(
      "https://wbapi.cloudapp.net:443/api/User/LocalLogin/"); 
    // post.setEntity(new StringEntity(request.toString(), "utf-8")); 
    StringEntity entity = new StringEntity(request.toString(), HTTP.UTF_8); 
    entity.setContentType("application/json"); 
    post.setHeader("Content-Type", "application/json"); 
    post.setHeader("Accept", "application/json"); 
    post.setEntity(entity); 
    HttpResponse response = client.execute(post); 
    return response; 
} 

我並設置應用/ JSON和瞧,我收到HTTP/200 OK。

+0

它更好地使用'httpurlconnection'做任何服務器相關的任務 –

+0

感謝張貼的解決方案 – Manju

回答

3

我想你應該設置contentType: "application/json"可能的幫助,請試試這個。

謝謝

+0

感謝@Bhavdip這工作:),而不是'HttpPost'或'HttpGet' –