机构

时间:2016-02-26 05:57:29

标签: android post android-async-http

需要实现一个功能,用户可以在app中向服务器发送反馈。我在app中使用asynchttpclient。但是我不知道怎么做。不知道怎么办请告诉我,非常感谢你。

以下是要求:

  1. 使用带身体的帖子请求(有参数建议内容)
  2. API:API /用户/反馈COMPANY_ID =安培; =的access_token
  3. 当我检查asynchttpclient的post方法时,我发现了以下方法:     public RequestHandle post(String url,ResponseHandlerInterface responseHandler){         return post(null,url,null,responseHandler);     }

    // [-] HTTP POST
    // [+] HTTP PUT
    
    /**
     * Perform a HTTP POST request with parameters.
     *
     * @param url             the URL to send the request to.
     * @param params          additional POST parameters or files to send with the request.
     * @param responseHandler the response handler instance that should handle the response.
     * @return RequestHandle of future request process
     */
    public RequestHandle post(String url, RequestParams params, ResponseHandlerInterface responseHandler) {
        return post(null, url, params, responseHandler);
    }
    
    /**
     * Perform a HTTP POST request and track the Android Context which initiated the request.
     *
     * @param context         the Android Context which initiated the request.
     * @param url             the URL to send the request to.
     * @param params          additional POST parameters or files to send with the request.
     * @param responseHandler the response handler instance that should handle the response.
     * @return RequestHandle of future request process
     */
    public RequestHandle post(Context context, String url, RequestParams params, ResponseHandlerInterface responseHandler) {
        return post(context, url, paramsToEntity(params, responseHandler), null, responseHandler);
    }
    
    /**
     * Perform a HTTP POST request and track the Android Context which initiated the request.
     *
     * @param context         the Android Context which initiated the request.
     * @param url             the URL to send the request to.
     * @param entity          a raw {@link cz.msebera.android.httpclient.HttpEntity} to send with the request, for
     *                        example, use this to send string/json/xml payloads to a server by
     *                        passing a {@link cz.msebera.android.httpclient.entity.StringEntity}.
     * @param contentType     the content type of the payload you are sending, for example
     *                        application/json if sending a json payload.
     * @param responseHandler the response ha   ndler instance that should handle the response.
     * @return RequestHandle of future request process
     */
    public RequestHandle post(Context context, String url, HttpEntity entity, String contentType, ResponseHandlerInterface responseHandler) {
        return sendRequest(httpClient, httpContext, addEntityToRequestBase(new HttpPost(getURI(url)), entity), contentType, responseHandler, context);
    }
    
    /**
     * Perform a HTTP POST request and track the Android Context which initiated the request. Set
     * headers only for this request
     *
     * @param context         the Android Context which initiated the request.
     * @param url             the URL to send the request to.
     * @param headers         set headers only for this request
     * @param params          additional POST parameters to send with the request.
     * @param contentType     the content type of the payload you are sending, for example
     *                        application/json if sending a json payload.
     * @param responseHandler the response handler instance that should handle the response.
     * @return RequestHandle of future request process
     */
    public RequestHandle post(Context context, String url, Header[] headers, RequestParams params, String contentType,
                              ResponseHandlerInterface responseHandler) {
        HttpEntityEnclosingRequestBase request = new HttpPost(getURI(url));
        if (params != null) request.setEntity(paramsToEntity(params, responseHandler));
        if (headers != null) request.setHeaders(headers);
        return sendRequest(httpClient, httpContext, request, contentType,
                responseHandler, context);
    }
    
    /**
     * Perform a HTTP POST request and track the Android Context which initiated the request. Set
     * headers only for this request
     *
     * @param context         the Android Context which initiated the request.
     * @param url             the URL to send the request to.
     * @param headers         set headers only for this request
     * @param entity          a raw {@link HttpEntity} to send with the request, for example, use
     *                        this to send string/json/xml payloads to a server by passing a {@link
     *                        cz.msebera.android.httpclient.entity.StringEntity}.
     * @param contentType     the content type of the payload you are sending, for example
     *                        application/json if sending a json payload.
     * @param responseHandler the response handler instance that should handle the response.
     * @return RequestHandle of future request process
     */
    public RequestHandle post(Context context, String url, Header[] headers, HttpEntity entity, String contentType,
                              ResponseHandlerInterface responseHandler) {
        HttpEntityEnclosingRequestBase request = addEntityToRequestBase(new HttpPost(getURI(url)), entity);
        if (headers != null) request.setHeaders(headers);
        return sendRequest(httpClient, httpContext, request, contentType, responseHandler, context);
    }
    

2 个答案:

答案 0 :(得分:1)

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



                }
            });


}

我想你是在问这个例子。

答案 1 :(得分:0)

//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();
    }
相关问题