1. 程式人生 > >AsyncHttp請求Post請求格式

AsyncHttp請求Post請求格式

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