首先使用Volley类创建 RequestQueue queue = Volley.newRequestQueue(this); 

Making GET Requests

 final String url = "http://httpbin.org/get?param1=hello";

 // prepare the Request
JsonObjectRequest getRequest = new JsonObjectRequest(Request.Method.GET, url, null,
new Response.Listener<JSONObject>()
{
@Override
public void onResponse(JSONObject response) {
// display response
Log.d("Response", response.toString());
}
},
new Response.ErrorListener()
{
@Override
public void onErrorResponse(VolleyError error) {
Log.d("Error.Response", response);
}
}
); // add it to the RequestQueue
queue.add(getRequest);

Making POST Requests

 url = "http://httpbin.org/post";
StringRequest postRequest = new StringRequest(Request.Method.POST, url,
new Response.Listener<String>()
{
@Override
public void onResponse(String response) {
// response
Log.d("Response", response);
}
},
new Response.ErrorListener()
{
@Override
public void onErrorResponse(VolleyError error) {
// error
Log.d("Error.Response", response);
}
}
) {
@Override
protected Map<String, String> getParams()
{
Map<String, String> params = new HashMap<String, String>();
params.put("name", "Alif");
params.put("domain", "http://itsalif.info"); return params;
}
};
queue.add(postRequest);

Making PUT Requests

 url = "http://httpbin.org/put";
StringRequest putRequest = new StringRequest(Request.Method.PUT, url,
new Response.Listener<String>()
{
@Override
public void onResponse(String response) {
// response
Log.d("Response", response);
}
},
new Response.ErrorListener()
{
@Override
public void onErrorResponse(VolleyError error) {
// error
Log.d("Error.Response", response);
}
}
) { @Override
protected Map<String, String> getParams()
{
Map<String, String> params = new HashMap<String, String> ();
params.put("name", "Alif");
params.put("domain", "http://itsalif.info"); return params;
} }; queue.add(putRequest);

Making DELETE Requests

 url = "http://httpbin.org/delete";
StringRequest dr = new StringRequest(Request.Method.DELETE, url,
new Response.Listener<String>()
{
@Override
public void onResponse(String response) {
// response
Toast.makeText($this, response, Toast.LENGTH_LONG).show();
}
},
new Response.ErrorListener()
{
@Override
public void onErrorResponse(VolleyError error) {
// error. }
}
);
queue.add(dr);

下一篇继续介绍Volley图片加载............

05-04 11:45