如何将参数作为形式参数发送,而不是用Android中的AsynchttpClient的URL参数发送



当前我正在用:

创建参数
RequestParams params = new RequestParams();
params.put("Param1","Value1");

然后以这样的发布:

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

但是,它使其成为URL参数。

如何将其添加到邮政请求的正文中,作为表单参数?

我建议使用凌空。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);

发帖的排球:

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

样品asynctask类

接收URL,参数中的参数...您可以传递URL编码中的任何数量参数!

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

相关内容

  • 没有找到相关文章

最新更新