-
Notifications
You must be signed in to change notification settings - Fork 0
/
Volley Example
315 lines (268 loc) · 8.25 KB
/
Volley Example
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
String Request
*********************************************************
String url = "https:// string_url/";
StringRequest
stringRequest
= new StringRequest(
Request.Method.GET,
url,
new Response.Listener() {
@Override
public void onResponse(String response)
{
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error)
{
}
});
requestQueue.add(stringRequest);
*****************************************************************************************
JSONObject Request
String url = "https:// json_url/";
JsonObjectRequest
jsonObjectRequest
= new JsonObjectRequest(
Request.Method.GET,
url,
null,
new Response.Listener() {
@Override
public void onResponse(JSONObject response)
{
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error)
{
}
});
requestQueue.add(jsonObjectRequest);
**************************************************************************************
JSONArray Request
JsonArrayRequest
jsonArrayRequest
= new JsonArrayRequest(
Request.Method.GET,
url,
null,
new Response.Listener() {
@Override
public void onResponse(JSONArray response)
{
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error)
{
}
});
requestQueue.add(jsonArrayRequest);
**********************************************************************************************************
Image Request
int max - width = ...;
int max_height = ...;
String URL = "http:// image_url.png";
ImageRequest
imageRequest
= new ImageRequest(URL,
new Response.Listener() {
@Override
public void
onResponse(Bitmap response)
{
// Assign the response
// to an ImageView
ImageView
imageView
= (ImageView)
findViewById(
R.id.imageView);
imageView.setImageBitmap(response);
}
},
max_width, max_height, null);
requestQueue.add(imageRequest);
******************************************************************************************************
Adding Post Parameters
String tag_json_obj = "json_obj_req";
String
url
= "https:// api.xyz.info/volley/person_object.json";
ProgressDialog pDialog = new ProgressDialog(this);
pDialog.setMessage("Loading...PLease wait");
pDialog.show();
JsonObjectRequest
jsonObjReq
= new JsonObjectRequest(
Method.POST,
url,
null,
new Response.Listener() {
@Override
public void onResponse(JSONObject response)
{
Log.d(TAG, response.toString());
pDialog.hide();
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error)
{
VolleyLog.d(TAG, "Error: "
+ error.getMessage());
pDialog.hide();
}
}) {
@Override
protected Map getParams()
{
Map params = new HashMap();
params.put("name", "Androidhive");
params.put("email", "abc@androidhive.info");
params.put("password", "password123");
return params;
}
};
AppController.getInstance()
.addToRequestQueue(jsonObjReq, tag_json_obj);
****************************************************************************************************************
Adding Request Headers
String tag_json_obj = "json_obj_req";
String
url
= "https:// api.androidhive.info/volley/person_object.json";
ProgressDialog pDialog = new ProgressDialog(this);
pDialog.setMessage("Loading...");
pDialog.show();
JsonObjectRequest
jsonObjReq
= new JsonObjectRequest(
Method.POST,
url,
null,
new Response.Listener() {
@Override
public void onResponse(JSONObject response)
{
Log.d(TAG, response.toString());
pDialog.hide();
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error)
{
VolleyLog.d(TAG, "Error: "
+ error.getMessage());
pDialog.hide();
}
}) {
@Override
public Map getHeaders() throws AuthFailureError
{
HashMap headers = new HashMap();
headers.put("Content-Type", "application/json");
headers.put("apiKey", "xxxxxxxxxxxxxxx");
return headers;
}
};
AppController.getInstance()
.addToRequestQueue(jsonObjReq, tag_json_obj);
***************************************************************************************************************
Handling the Volley Cache
// Loading request from cache
Cache
cache
= AppController.getInstance()
.getRequestQueue()
.getCache();
Entry entry = cache.get(url);
if (entry != null) {
try {
String
data
= new String(entry.data, "UTF-8");
// handle data, like converting it
// to xml, json, bitmap etc.,
}
catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
}
}
else
{
// If cached response doesn't exists
}
// Invalidate cache
AppController.getInstance()
.getRequestQueue()
.getCache()
.invalidate(url, true);
// Turning off cache
// String request
StringRequest
stringReq
= new StringRequest(....);
// disable cache
stringReq.setShouldCache(false);
// Deleting cache for particular cache</strong>
AppController.getInstance()
.getRequestQueue()
.getCache()
.remove(url);
// Deleting all the cache
AppController.getInstance()
.getRequestQueue()
.getCache()
.clear(url);
Cancelling Request
filter_none
brightness_4
// Cancel single request
String tag_json_arry = "json_req";
ApplicationController.getInstance()
.getRequestQueue()
.cancelAll("feed_request");
// Cancel all request
ApplicationController.getInstance()
.getRequestQueue()
.cancelAll();
****************************************************************************************************************
Request Prioritization
private Priority priority = Priority.HIGH;
StringRequest
strReq
= new StringRequest(
Method.GET,
Const.URL_STRING_REQ,
new Response
.Listener() {
@Override
public void onResponse(String response) {
Log.d(TAG, response.toString());
msgResponse.setText(response.toString());
hideProgressDialog();
} },
new Response
.ErrorListener() {
@Override
public void
onErrorResponse(VolleyError error) {
VolleyLog.d(TAG,
"Error: "
+ error.getMessage());
hideProgressDialog();
} }) {
@Override
public Priority getPriority()
{
return priority;
}
};