asynchttpclient的post方法发送json格式参数和表单格式参数

Android使用 asynchttpclient的post方法发送数据大致可分为两种情况;一种使用

post(android.content.Context context, java.lang.String url, cz.msebera.android.httpclient.HttpEntity entity, java.lang.String contentType, ResponseHandlerInterface responseHandler)

这种是可以自定义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);
                    }
                });

另一种是使用

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

这个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();
                    }

这其实是比赛项目中碰到的一个问题,一开始服务器写的是解析json格式的,而我使用的是第二种post方法,所以报错,还子啊上面花费一些时间,把服务器的代码改为了处理非json格式的,当时觉得asynchttpclient不可能不提供json的传递方法,于是又静下来看看,特在此总结。

你可能感兴趣的:(android)