2012-12-28 184 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' –