如何使用Android中的AsyncHttpClient将参数作为Form参数发送而不是URL参数?

时间:2017-02-28 07:17:58

标签: android android-async-http loopj

目前我正在创建参数:

RequestParams params = new RequestParams();
params.put("Param1","Value1");

然后发布如下:

AsyncHttpClient client = new AsyncHttpClient();
client.post(restApiUrl, params, responseHandler);

然而,它使它成为一个URL参数。

如何将其作为Form参数添加到POST请求的正文中?

2 个答案:

答案 0 :(得分:1)

我建议使用Volley。 Volley是一个HTTP库,它使Android应用程序的网络更容易,最重要的是,更快。排球可在GitHub上找到。

与GET一起排球:

RequestQueue queue = Volley.newRequestQueue(getContext);
String url = "http://www.someurl.com?param1=value1&param2=value2"; // your url
StringRequest stringRequest = new StringRequest(Request.Method.GET, url, new Response.Listener<String>() {
        @Override
        public void onResponse(String response) {
           // here you will get the response from the url
           Log.d(TAG, response);
        }
    }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            // in case there is error in request, it'll be thrown here
            Log.e(TAG, error.toString);
        }
    });

    queue.add(stringRequest);

使用POST排球:

RequestQueue queue = Volley.newRequestQueue(getContext);
String url = "http://www.someurl.com"; // your url
StringRequest stringRequest = new StringRequest(Request.Method.POST, url, new Response.Listener<String>() {
        @Override
        public void onResponse(String response) {
           // here you will get the response from the url
           Log.d(TAG, response);
        }
    }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            // in case there is error in request, it'll be thrown here
            Log.e(TAG, error.toString);
        }
    }){
        @Override
        protected Map<String,String> getParams(){
            // here you add params
            Map<String,String> params = new HashMap<>();
            params.put("param1", "value1");
            params.put("param2", "value2");
            return params;
        }
        @Override
        public Map<String, String> getHeaders() throws AuthFailureError {
            return super.getHeaders();
        }

        @Override
        public String getBodyContentType() {
            return super.getBodyContentType();
        }
    };
    queue.add(stringRequest);

答案 1 :(得分:0)

最简单的方法 - 使用网址编码器类

示例AsyncTask类

接收url,Arguments中的参数...你可以在URL Encoder中传递任意数量的参数!

public class LongOperation  extends AsyncTask<String, Void, String> {


    Context context;
    ProgressDialog progressDialog;

    public LongOperation(Context context)
    {
        this.context=context;
        progressDialog=new ProgressDialog(context);
    }


    protected void onPreExecute() {

        progressDialog.setTitle("test");
        progressDialog.setMessage("Loading . . . .");
        progressDialog.setCancelable(false);
        progressDialog.show();
        Toast.makeText(context,"loading . . .",Toast.LENGTH_LONG).show();
    }

    // Call after onPreExecute method
    protected String doInBackground(String... urls) {

        try {
            URL url=new URL(urls[0]);
            URLConnection con=url.openConnection();
            con.setDoOutput(true);

            con.setDoInput(true);

            // request
            OutputStreamWriter io=new OutputStreamWriter(con.getOutputStream());
            // parameters
            io.write(URLEncoder.encode("name", "UTF-8") + "=" + URLEncoder.encode(urls[1], "UTF-8"));
            io.flush();

            // response
            BufferedReader reader=new BufferedReader(new InputStreamReader(con.getInputStream()));

            String line,received="";

            while ((line=reader.readLine())!=null)
            {
                received+=line;
            }
            return received;
        }
        catch (Exception e) {
            return e.getMessage().toString();
        }
    }
    protected void onPostExecute(String un) { 
       if(processDialog.isShowing())
            processDialog.dimiss();

    }
}

我想,这会对你有所帮助! 感谢