2017-02-28 7 views
1

は現在、私はパラメータを作成しています:AndroidでAsyncHttpClientを使用してURLパラメータではなくフォームパラメータとしてパラメータを送信するにはどうすればよいですか?

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

そして、このように掲示する:それはそれURLパラメータになりしかし

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

POSTリクエストの本文にFormパラメータとして追加する方法はありますか?

+1

は、より多くのコードを入力してください。 paramsをどのように渡してリクエストしますか? – MaxV

+0

[Android AsyncHttpClient:マルチパートフォームデータをPOSTする方法は?](http://stackoverflow.com/questions/33649852/android-asynchttpclient-how-to-post-multipart-form-data) – MaxV

答えて

0

最も簡単な方法 - 使用URLエンコーダークラス

URLを受け取るサンプルAsyncTaskクラス

は、引数のパラメータ...あなたは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(); 

    } 
} 

私はこれがあなたを助けると思う! ありがとう

1

私はVolleyを使用することをお勧めします。 Volleyは、Androidアプリのネットワーキングをより簡単で重要なものに早くするHTTPライブラリです。 VolleyはGitHubにあります。 GETと

ボレー:POSTと

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); 
関連する問題