使用asynhttpclient以raw格式发送数据

时间:2017-09-19 05:27:47

标签: android json asynchttpclient

我使用此Url以原始方式发布JSON [URL我将数据发布到] [1]

  

param is = eventName =“countryList”

 private void testApp() {
    try {
        JSONObject jsonParams = new JSONObject();
        jsonParams.put("key", "value");
        StringEntity entity = new StringEntity(new Gson().toJson(jsonParams));
        entity.setContentType(new BasicHeader(HTTP.CONTENT_TYPE, "application/json"));

        AsyncHttpClient client = new AsyncHttpClient();
        client.post(getApplicationContext(), "url", entity, "application/json", new JsonHttpResponseHandler() {
            @Override
            public void onSuccess(int statusCode, Header[] headers, JSONArray response) {
                super.onSuccess(statusCode, headers, response);

                Log.e("good",response.toString());
            }

            @Override
            public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) {
                super.onFailure(statusCode, headers, responseString, throwable);

                Log.e("fail",throwable.toString());
            }
        });


    } catch (Exception e) {

    }

}
  

错误:失败:cz.msebera.android.httpclient.client.HttpResponseException:内部服务器错误

1 个答案:

答案 0 :(得分:0)

更改 StringEntity entity = new StringEntity(new Gson().toJson(jsonParams)); **StringEntity stringEntity = new StringEntity(jsonParams.toString());**

并覆盖

 @Override
            public void onSuccess(int statusCode, Header[] headers, JSONObject response) {
                super.onSuccess(statusCode, headers, response);


            }

您的回复返回jsonobject not array