其他分享
首页 > 其他分享> > AsyncHttpClient 发送json形式的post请求

AsyncHttpClient 发送json形式的post请求

作者:互联网

使用AsyncHttpClient发送post请求报400

问题: 在Android利用AsyncHttpClient发送post请求时报400的错误,请求参数存放在RequestParams params里面,后台以requireBody接收参数

参数要以json的形式传送

加上以下代码请求就正常了

params.setUseJsonStreamer(true);

贴出请求封装代码

public void post(String url, RequestParams params, final Activity activity) {

        AsyncHttpClient client = AsyncHttpCilentUtil.getInstance(activity);

        Header[] headers = new Header[0];
        String token = CacheUtils.getString(activity, "token");
        if (!TextUtils.isEmpty(token)) {
            headers = new BasicHeader[2];
            headers[0] = new BasicHeader("Token", token);
            headers[1] = new BasicHeader("Content-Type", "application/json");
        }
        Log.i("请求地址", UrlHelper.getRequestUrl(url));
        params.setUseJsonStreamer(true);
        client.post(activity, UrlHelper.getRequestUrl(url), headers, params, "", new DataAsyncHttpResponseHandler() {

            @Override
            public void onStart() {
                super.onStart();
                pd = new ProgressDialog(activity);
                pd.setProgressStyle(ProgressDialog.STYLE_SPINNER);
                pd.setCancelable(false);
                pd.setMessage("请稍候...");
                pd.show();
            }

            @Override
            public void onFinish() {
                super.onFinish();
                pd.dismiss();
            }

            @Override
            public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) {
                String resultString = new String(responseBody);
                LogUtil.e("onSuccess-----------/n" + resultString);
                requireSuccess(resultString);

            }

            @Override
            public void onFailure(int statusCode, Header[] headers, byte[] responseBody, Throwable error) {
                pd.dismiss();
                requireFailure(activity,statusCode, headers, responseBody, error);
            }
        });
    }```

标签:void,AsyncHttpClient,headers,json,pd,activity,new,post,public
来源: https://blog.csdn.net/code_carrierV1/article/details/123250177