xUtils怎么post请求上传字符串

xUtils算是一个非常实用老牌的框架了,但是有些情况,特别是网络请求部分,总是不太方便.需要折腾下才了解用法.今天来聊聊网络模块的用法.

其实看例子就明白了,post JSON数据也是一样道理,这里主要是要说明注释的地方params.addBodyParameter("number","123456"); 容易用错这个方法,这个post的数据是number=123456,哪怕你设置为null也是不行的.

 private void postString(){
        HttpUtils httpUtils = new HttpUtils();
        httpUtils.configTimeout(2000);//设置超时
        RequestParams params = new RequestParams();
       // params.addBodyParameter("number","123456");这个方法,如果这样写,实际post的数据是 number=123456因此不符合需求
        try {
            //这个方法post的就是123456了
            params.setBodyEntity(new StringEntity("123456"));
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        httpUtils.send(HttpRequest.HttpMethod.POST, "", params, new RequestCallBack<String>() {
            @Override
            public void onSuccess(ResponseInfo<String> responseInfo) {
                //成功的回调
                Log.d("message", "onSuccess: "+responseInfo.result);
            }

            @Override
            public void onFailure(HttpException e, String s) {
                //失败的回调
                Log.d("message", "onSuccess: "+s);
            }
        });
    }

 

posted @ 2019-04-11 17:30  _Vincent  阅读(582)  评论(0编辑  收藏  举报