android-async-http post request with body

后端 未结 2 1392
囚心锁ツ
囚心锁ツ 2020-12-22 04:01

It\'s require to realize a function that user can send feedback to the server in the app.And I use the asynchttpclient in the app.But I don\'t know how to do this.anyont kn

相关标签:
2条回答
  • 2020-12-22 04:11
        AsyncHttpClient client = new AsyncHttpClient();
        RequestParams params = new RequestParams();
        params.put("key", value);
        client.post(context, URL_YOURURL,
                params, new AsyncHttpResponseHandler() {
    
            Progress pd = new Progress(context);
    
    
                    public void onStart() {
    
                        super.onStart();
                        pd.show();
    
                    }
    
                    @Override
                    public void onSuccess(int arg0,
                            Header[] arg1, byte[] arg2) {
    
    
    
                        try {
                            JSONObject json = new JSONObject(
                                    new String(arg2));
    
                        } catch (JSONException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }
    
                    }
    
                    @Override
                    public void onFailure(int arg0,
                            Header[] arg1, byte[] arg2,
                            Throwable arg3) {
    
    
                    }
    
                    public void onFinish() {
                        super.onFinish();
                        if(pd!=null){
                            pd.dismiss();
                        }
    
    
    
                    }
                });
    
    
    }
    

    I think You are asking for this example.

    0 讨论(0)
  • 2020-12-22 04:27
    //entity
        JSONObject jsonObject = new JSONObject();
        StringEntity entity;
        try {
            jsonObject.put("suggestion", suggestion);
    
            entity = new StringEntity(jsonObject.toString());
            //url
            String url = "user/feedback?";
    
            YXUser yxUser = YXUser.currentUser();
            //公司id
            if (yxUser != null && yxUser.getCompanyId() != null) {
                url = url + "company_id=" + yxUser.getCompanyId();
            }
    
            if (null != YXPersistence.getValueByKey(AppConstant.APP_TOKEN_KEY)) {
                url = url + "&access_token=" + YXPersistence.getValueByKey(AppConstant.APP_TOKEN_KEY);
            }
    
            url = AppConstant.BASE_URL + url;
            client.post(this, url, entity, "application/json", new MyHttpResponseHandler(this) {
                @Override
                protected void onNormalResponse(int statusCode, Header[] headers, JSONObject info) {
                    if (JSONUtil.checkStatusSuccess(info)) {
                        showToast("上传成功");
                        finish();
                    }
                }
            });
        } catch (JSONException e) {
            e.printStackTrace();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
    
    0 讨论(0)
提交回复
热议问题