標籤:
3. 佈建要求類型和參數
Volley預設的請求類型是GET,如果需要用POST,可以在建構函式中進行設定。設定參數可以通過重寫getParams()方法來實現。
private void postRequest(){ JsonObjectRequest postObjectRequest = new JsonObjectRequest(Method.POST, "http://192.168.199.18:8080/myhos/query", null, new Response.Listener<JSONObject>() { @Override public void onResponse(JSONObject response) { Log.i("Response:", response.toString()); } }, new Response.ErrorListener() { @Override public void onErrorResponse(VolleyError error) { Log.i("Error:", error.getMessage(), error); } }){ @Override protected Map<String, String> getParams() throws AuthFailureError { Map<String, String> map = new HashMap<String, String>(); map.put("qid", "446"); map.put("count", "10"); map.put("page", "1"); return map; } }; mQueue.add(postObjectRequest); }
以上代碼中,使用了Volley請求的另一個建構函式,通過Method.POST指定了請求方式為POST,並通過重寫getParams()方法設定了三個參數。運行結果如下:
Android Volley架構的使用(2)