4

サーバーへの 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 が提供するクラスとともに、HTTPS Post リクエストをサーバーに送信します。

応答オブジェクトを取得しています。しかし、私の 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 と voila を設定しました。HTTP/200 OK を取得しています。

4

1 に答える 1

6

これcontentType: "application/json"を試してみてください。

ありがとうございました

于 2012-12-28T05:59:13.080 に答える