POST请求500内部服务器错误

时间:2012-12-28 05:56:46

标签: android

这是我对服务器的POST请求的代码。

要发布到服务器的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;
}

Paresh Mayani在Send HTTPS Post Request to the server

提供的课程

我收到了响应对象。但我的response.getStatusLine()仅为POST请求显示500 /内部服务器错误。

注意: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;
}

我确实设置了application / json和瞧,我正在获得HTTP / 200 OK。

1 个答案:

答案 0 :(得分:3)

我认为您应该设置contentType: "application/json"可能会有所帮助,请尝试此操作。

谢谢

相关问题