AsyncHttp请求Post请求格式

这种是可以自定义post的参数的格式的,一般使用json,详情请见(http://stackoverflow.com/questions/26842090/asynchttpclient-passed-contenttype-will-be-ignored-because-httpentity-sets-cont)举例说明

                AsyncHttpClient client = new AsyncHttpClient();
                final String url = Utils.baseUrl+"regist.html";
                JSONObject jsonObject = new JSONObject();
                try {
                    jsonObject.put("username",etPhoneNumber.getText());
                    jsonObject.put("password",etPassword.getText());
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                ByteArrayEntity entity = null;
                try {
                    entity = new ByteArrayEntity(jsonObject.toString().getBytes("UTF-8"));
                    entity.setContentType(new BasicHeader(HTTP.CONTENT_TYPE, "application/json"));
                } catch (UnsupportedEncodingException e) {
                    e.printStackTrace();
                }

                client.post(mContext,url,entity,"application/json",new JsonHttpResponseHandler(){
                    @Override
                    public void onSuccess(int statusCode, Header[] headers, JSONObject response) {
                        super.onSuccess(statusCode, headers, response);
                        Log.e("rs",response.toString());
                    }

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

 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30

另一种是使用

post(java.lang.String url, RequestParams params, ResponseHandlerInterface responseHandler)
 
 
  • 1
  • 1

这个post方法,这种是就是常规的提价表单格式,齐参数的格式为username=yzk&password=yzk这种。使用举例

 final RequestParams params = new RequestParams();
                params.put("username",etPhoneNumber.getText());
                params.put("password",etPassword.getText());
                client.post(url,params,new JsonHttpResponseHandler(){
                    @Override
                    public void onSuccess(int statusCode, Header[] headers, JSONObject response) {
                        super.onSuccess(statusCode, headers, response);
                        try {
                            Log.e("rs",response.getString("status"));
                            if(response.getString("status").equals("0")){
                                user = User.getInsstance();
                                user.setIsOnline(true);
                                user.setToken(response.getString("token"));
                                user.setId(etPhoneNumber.getText().toString());
                                Toast.makeText(mContext,"注册成功",Toast.LENGTH_LONG).show();
                                mContext.finish();
                            }else{
                                Toast.makeText(mContext,"改用已注册",Toast.LENGTH_LONG).show();
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                    @Override
                    public void onFailure(int statusCode, Header[] headers, Throwable throwable, JSONObject errorResponse) {
                        super.onFailure(statusCode, headers, throwable, errorResponse);
                        Toast.makeText(mContext,"网络错误",Toast.LENGTH_LONG).show();
                    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值