1String uri = String.format("http://somesite.com/some_endpoint.php?param1=%1$s¶m2=%2$s",
2 num1,
3 num2);
4
5StringRequest myReq = new StringRequest(Method.GET,
6 uri,
7 createMyReqSuccessListener(),
8 createMyReqErrorListener());
9queue.add(myReq);
1// http client instance
2private DefaultHttpClient mHttpClient;
3public RequestQueue getRequestQueue() {
4 // lazy initialize the request queue, the queue instance will be
5 // created when it is accessed for the first time
6 if (mRequestQueue == null) {
7 // Create an instance of the Http client.
8 // We need this in order to access the cookie store
9 mHttpClient = new DefaultHttpClient();
10 // create the request queue
11 mRequestQueue = Volley.newRequestQueue(this, new HttpClientStack(mHttpClient));
12 }
13 return mRequestQueue;
14}
15
16/**
17 * Method to set a cookie
18 */
19public void setCookie() {
20 CookieStore cs = mHttpClient.getCookieStore();
21 // create a cookie
22 cs.addCookie(new BasicClientCookie2("cookie", "spooky"));
23}
24
25
26// add the cookie before adding the request to the queue
27setCookie();
28
29// add the request to the queue
30mRequestQueue.add(request);
31
1public class VolleyErrorHelper {
2 /**
3 * Returns appropriate message which is to be displayed to the user
4 * against the specified error object.
5 *
6 * @param error
7 * @param context
8 * @return
9 */
10 public static String getMessage(Object error, Context context) {
11 if (error instanceof TimeoutError) {
12 return context.getResources().getString(R.string.generic_server_down);
13 }
14 else if (isServerProblem(error)) {
15 return handleServerError(error, context);
16 }
17 else if (isNetworkProblem(error)) {
18 return context.getResources().getString(R.string.no_internet);
19 }
20 return context.getResources().getString(R.string.generic_error);
21 }
22
23 /**
24 * Determines whether the error is related to network
25 * @param error
26 * @return
27 */
28 private static boolean isNetworkProblem(Object error) {
29 return (error instanceof NetworkError) || (error instanceof NoConnectionError);
30 }
31 /**
32 * Determines whether the error is related to server
33 * @param error
34 * @return
35 */
36 private static boolean isServerProblem(Object error) {
37 return (error instanceof ServerError) || (error instanceof AuthFailureError);
38 }
39 /**
40 * Handles the server error, tries to determine whether to show a stock message or to
41 * show a message retrieved from the server.
42 *
43 * @param err
44 * @param context
45 * @return
46 */
47 private static String handleServerError(Object err, Context context) {
48 VolleyError error = (VolleyError) err;
49
50 NetworkResponse response = error.networkResponse;
51
52 if (response != null) {
53 switch (response.statusCode) {
54 case 404:
55 case 422:
56 case 401:
57 try {
58 // server might return error like this { "error": "Some error occured" }
59 // Use "Gson" to parse the result
60 HashMap<String, String> result = new Gson().fromJson(new String(response.data),
61 new TypeToken<Map<String, String>>() {
62 }.getType());
63
64 if (result != null && result.containsKey("error")) {
65 return result.get("error");
66 }
67
68 } catch (Exception e) {
69 e.printStackTrace();
70 }
71 // invalid request
72 return error.getMessage();
73
74 default:
75 return context.getResources().getString(R.string.generic_server_down);
76 }
77 }
78 return context.getResources().getString(R.string.generic_error);
79 }
80}
81
1JsonObjectRequest req = new JsonObjectRequest(URL, new JSONObject(params),
2 new Response.Listener<JSONObject>() {
3 @Override
4 public void onResponse(JSONObject response) {
5 // handle response
6 }
7 }, new Response.ErrorListener() {
8 @Override
9 public void onErrorResponse(VolleyError error) {
10 // handle error
11 }
12 }) {
13
14 @Override
15 public Map<String, String> getHeaders() throws AuthFailureError {
16 HashMap<String, String> headers = new HashMap<String, String>();
17 headers.put("CUSTOM_HEADER", "Yahoo");
18 headers.put("ANOTHER_CUSTOM_HEADER", "Google");
19 return headers;
20 }
21 };
22
1StringRequest myReq = new StringRequest(Method.POST,
2 "http://somesite.com/some_endpoint.php",
3 createMyReqSuccessListener(),
4 createMyReqErrorListener()) {
5
6 protected Map<String, String> getParams() throws com.android.volley.AuthFailureError {
7 Map<String, String> params = new HashMap<String, String>();
8 params.put("param1", num1);
9 params.put("param2", num2);
10 return params;
11 };
12};
13queue.add(myReq);