【问题标题】:How to send post request with x-www-form-urlencoded body with loopj-async-http library如何使用 loopj-async-http 库发送带有 x-www-form-urlencoded 正文的发布请求
【发布时间】:2018-07-30 18:13:39
【问题描述】:

我正在尝试使用 x-www-form-urlencoded 在正文中发布数据,但我失败了

    private void sendData(final String toekn) {

    RequestParams params = new RequestParams();
    params.put("_token", toekn);
    StringEntity entity=null;
    try {
        entity = new StringEntity(params.toString());
        entity.setContentType(new BasicHeader(HTTP.CONTENT_TYPE, "application/x-www-form-urlencoded; charset=UTF-8"));
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    }
    PropClient.post(getBaseContext(), "", entity, new JsonHttpResponseHandler() {

        @Override
        public void onSuccess(int statusCode, Header[] headers, JSONObject response) {

            Log.e("see", response.toString());
            Toast.makeText(SplashActivity.this, response.toString()+"", Toast.LENGTH_SHORT).show();

        }

        @Override
        public void onSuccess(int statusCode, Header[] headers, JSONArray timeline) {
            // Pull out the first event on the public timeline
            // Do something with the response
        }

        @Override
        public void onFailure(int statusCode, Header[] headers, String response, Throwable e) {

        }

        @Override
        public void onRetry(int retryNo) {
            // called when request is retried
        }
    });
}

上面是我尝试过但总是失败的代码.. api 在邮递员中完美运行我附上了图片以了解参数..

image1

image2

静态类

公共类 PropClient { 私有静态最终字符串 BASE_URL = "";

private static AsyncHttpClient client = new AsyncHttpClient();

public static void get(String url, RequestParams params, AsyncHttpResponseHandler responseHandler) {
    client.get(getAbsoluteUrl(url), params, responseHandler);
}

public static void post(Context context, String url, StringEntity entity, AsyncHttpResponseHandler responseHandler) {
  //  client.addHeader("Accept", "application/json");
    client.addHeader("Content-Type", "application/x-www-form-urlencoded");
    entity.setContentType("application/json");
    client.setUserAgent("android");
    client.post(context, getAbsoluteUrl(url), entity, "application/json", responseHandler);
}

private static String getAbsoluteUrl(String relativeUrl) {
    String url = BASE_URL + relativeUrl;
    return url;
}

}

【问题讨论】:

    标签: android post loopj


    【解决方案1】:

    如果你想使用 volley 可以查看我的答案

    编译'com.android.volley:volley:1.0.0'

      private void getVolley(final String token, String url) {
        final RequestQueue requestQueue = Volley.newRequestQueue(getApplicationContext());
        StringRequest jsonObjRequest = new StringRequest(Request.Method.POST, url, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                Toast.makeText(SplashActivity.this, response + "  success", Toast.LENGTH_SHORT).show();
                Log.e("volley", response);
    
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                Toast.makeText(SplashActivity.this, error.toString() + "", Toast.LENGTH_SHORT).show();
            }
        }) {
            @Override
            public String getBodyContentType() {
                return "application/x-www-form-urlencoded; charset=UTF-8";
            }
    
            @Override
            public Map<String, String> getHeaders() throws AuthFailureError {
                Map<String, String> params = new HashMap<String, String>();
                params.put("User-Agent", "android");
                params.put("Content-Type", "application/x-www-form-urlencoded");
    
                return params;
            }
    
            @Override
            protected Map<String, String> getParams() throws AuthFailureError {
                Map<String, String> params = new HashMap<String, String>();
                params.put("_token", token);
                return params;
            }
        };
        jsonObjRequest.setRetryPolicy(new DefaultRetryPolicy(30 * 1000,
                DefaultRetryPolicy.DEFAULT_MAX_RETRIES,
                DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
    
    
        requestQueue.add(jsonObjRequest);
    }
    

    【讨论】:

      【解决方案2】:

      您需要添加如下标题:-

      public static void setHttpClient(AsyncHttpClient c, Application application) {
          c.addHeader("Accept-Language", Locale.getDefault().toString());
          c.addHeader("Host", HOST);
          c.addHeader("Connection", "Keep-Alive");
          //noinspection deprecation
          c.getHttpClient().getParams()
                  .setParameter(ClientPNames.ALLOW_CIRCULAR_REDIRECTS, true);
          // Set AppToken
          c.addHeader("AppToken", Verifier.getPrivateToken(application));
          //c.addHeader("AppToken", "123456");
          // setUserAgent
          c.setUserAgent(ApiClientHelper.getUserAgent(AppContext.getInstance()));
          CLIENT = c;
          initSSL(CLIENT);
          initSSL(API.mClient);
      }
      

      You need to see this link for more info.

      【讨论】:

      • 对于 user-Agent 和 Content-Type ?并删除两者的基本功能?
      • 你通过链接了吗?
      • 不,你弄错了,我已经尽力了但失败了......我第一次这样做......所以如果你能帮助我,那就太好了......
      猜你喜欢
      • 2017-03-27
      • 2023-04-10
      • 2019-06-14
      • 2022-02-07
      • 2018-12-25
      • 1970-01-01
      相关资源
      最近更新 更多