无法使用HttpPost发布JSON数据

时间:2015-03-23 07:46:32

标签: java javascript android httpclient

我正在尝试将JSON数据发布到我的API。但在执行后,我得到了以下结果:

{"name":"Corporate","addr":"Unknown","area":"Unknown","cityId":10,"phone":"--","fax":"--","wooqStoreId":1}]

Response 2 >>{"message":"Blank String","result":"Error","resultCode":"RESULT_CODE_002"}
true

前两行显示我的JSON字符串和 响应2是我得到的消息。它应该是一个成功的消息,因为我得到状态代码200。

public static boolean pushtoAPI(String url, String jsonObject) {
            DefaultHttpClient client = new DefaultHttpClient();
            HttpPost request = null;
            HttpResponse response = null;
            String postUrl = getHostUrl() + url;

            try {
                    request = new HttpPost(postUrl);


                    StringEntity postingString = new StringEntity(jsonObject.toString());
                    postingString.setContentType("application/json;charset=UTF-8");
                    postingString.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE,
                                    "application/json;charset=UTF-8"));

                    request.setEntity(postingString);


                    request.setHeader("Content-type", "application/json");


                    String custom_cookie = ConstantUtil.authCookie(ConstantUtil.getLoginJsessionId());
                    request.setHeader("Cookie", custom_cookie);

                    response = client.execute(request);
                    System.out.println("Response 2 >>" + EntityUtils.toString(response.getEntity()));
                    if (response.getStatusLine().getStatusCode() == 200) {
                            System.out.println("true");
                            return true;
                    }
            } catch (ClientProtocolException e) {
                    e.printStackTrace();
            } catch (IOException e) {
                    e.printStackTrace();
            } catch (Exception e) {
            } finally {
                    request.abort();
            }

            return false;
    }

1 个答案:

答案 0 :(得分:0)

看起来它是服务器端代码问题。

你能说明你在哪里创建这个字符串吗?

  

“消息”:“空白   串”, “结果”: “错误”, “发送resultCode”: “RESULT_CODE_002”