AsyncHttpClient:将忽略Passed contentType,因为HttpEntity设置了内容类型

pla*_*420 1 android web-services loopj androidhttpclient android-async-http

我试图使用android httpclient(loopj)发布一些数据.我在其正文中添加一些json数据并设置请求标头.但它显示AsyncHttpClient:将忽略Passed contentType,因为HttpEntity设置了内容类型.有谁知道如何解决这个问题?

 public static void post(Activity context,String url, RequestParams params, AsyncHttpResponseHandler responseHandler) {
        try {
            JSONObject jsonParams = new JSONObject();
            JSONObject innerObject = new JSONObject();
            innerObject.put("Name", "@MODE");
            innerObject.put("ParamType", "8");
            innerObject.put("Value", "0");
            JSONArray ar = new JSONArray();
            ar.put(innerObject);
            try {
                jsonParams.put("ProcName", "Core.MENUS_SPR");
                jsonParams.put("dbparams", ar);

               Log.i("jsonParams.toString()",""+jsonParams.toString());

                StringEntity se = null;
                try {
                    se = new StringEntity(jsonParams.toString());


                } catch (UnsupportedEncodingException e) {
                    e.printStackTrace();
                    return;
                }
                   client.post(context, (url), se, "application/json", responseHandler);


            } catch (JSONException e) {
                e.printStackTrace();
            }

      } catch (Exception e) {
            e.printStackTrace();
        }

    }
Run Code Online (Sandbox Code Playgroud)

Chi*_*ain 6

在帖子之前写这个然后它会工作.

se.setContentType(new BasicHeader(HTTP.CONTENT_TYPE, "application/json"));
Run Code Online (Sandbox Code Playgroud)

原因一旦您使用某个实体,它将忽略post中给出的内容类型并使用实体的内容.因此,上面的行将解决您的问题.


我深入研究了源代码并发现您的内容类型传入post(..)将被忽略,如果存在,那么您将在日志中出现此错误.

传递的contentType将被忽略,因为HttpEntity设置了内容类型

但是,一旦您将内容类型提供给您的实体,它就会起作用.要删除此错误,您可以在内容类型中传递null post(..).

AsyncHttpClient.java中的一些代码:

if (contentType != null) {
            if (uriRequest instanceof HttpEntityEnclosingRequestBase && ((HttpEntityEnclosingRequestBase) uriRequest).getEntity() != null) {
                Log.w(LOG_TAG, "Passed contentType will be ignored because HttpEntity sets content type");
            } else {
                uriRequest.setHeader(HEADER_CONTENT_TYPE, contentType);
            }
        }
Run Code Online (Sandbox Code Playgroud)