Volley Android Networking Library

Volley Android Networking Library

$ git clone https://android.googlesource.com/platform/frameworks/volley
$ cd volley
$ android update project -p .
$ ant jar

Then, copy bin/volley.jar into your libs/ folder and off you go!

source

Comparison of Android networking libraries: OkHTTP, Retrofit, and Volley

I'm hoping someone can provide some concrete examples of best use cases for each.

Use Retrofit if you are communicating with a Web service. Use the peer library Picasso if you are downloading images. Use OkHTTP if you need to do HTTP operations that lie outside of Retrofit/Picasso.

Volley roughly competes with Retrofit + Picasso. On the plus side, it is one library. On the minus side, it is one undocumented, an unsupported, "throw the code over the wall and do an I|O presentation on it" library.

EDIT - Volley is now officially supported by Google. Kindly refer Google Developer Guide

From what I've read, seems like OkHTTP is the most robust of the 3

Retrofit uses OkHTTP automatically if available. There is a Gist from Jake Wharton that connects Volley to OkHTTP.

and could handle the requirements of this project (mentioned above).

Probably you will use none of them for "streaming download of audio and video", by the conventional definition of "streaming". Instead, Android's media framework will handle those HTTP requests for you.

That being said, if you are going to attempt to do your own HTTP-based streaming, OkHTTP should handle that scenario; I don't recall how well Volley would handle that scenario. Neither Retrofit nor Picasso are designed for that.

Android networking library: Is it still worth using Volley?

Check out the detailed response:
Comparison of Android networking libraries: OkHTTP, Retrofit, and Volley

Personally, I went native to keep things simple and robust. Using it for JSON responses and occasional images with no problem and I don't think they will deprecate HttpURLConnection anytime soon.

Exception handling in android volley networking library

when you create a request object in Volley you need to specify an error listener, Volley invokes the onErrorResponse callback method of that listener passing an instance of the VolleyError object when there is an error while performing the request.

The following is the list of exceptions in Volley:

AuthFailureError — If you are trying to do Http Basic authentication then this error is most likely to come.

NetworkError — Socket disconnection, server down, DNS issues might result in this error.

NoConnectionError — Similar to NetworkError, but fires when the device does not have Internet connection, your error handling logic can club NetworkError and NoConnectionError together and treat them similarly.

ParseError — While using JsonObjectRequest or JsonArrayRequest if the received JSON is malformed then this exception will be generated. If you get this error then it is a problem that should be fixed instead of being handled.

ServerError — The server responded with an error, most likely with 4xx or 5xx HTTP status codes.

TimeoutError — Socket timeout, either server is too busy to handle the request or there is some network latency issue. By default, Volley times out the request after 2.5 seconds, use a RetryPolicy if you are consistently getting this error.

Source: http://arnab.ch/blog/2013/08/asynchronous-http-requests-in-android-using-volley/

Volley Library Gives Server Not Responding

i think issue is in the in your Cache of volley library, do this may be it is helpfull.

        StringRequest stringRequest = new StringRequest(Request.Method.POST, mainUrl,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
try {
JSONObject obj = new JSONObject(response);
if (obj.length() == 0) {
callApi(Util.GetMainUrl(context), "start");
} else {
mainWVUrl = obj.getString("url");
Log.d(TAG, "MainWVUrl" + mainWVUrl);
JSONArray jsonArray = obj.getJSONArray("timeRange");
for (int i = 0; i < jsonArray.length(); i++) {
minTime = jsonArray.getInt(0);
maxTime = jsonArray.getInt(1);
}
Boolean WVVisible = obj.getBoolean("visible");

Bundle args1 = new Bundle();
args1.putString(MAINURL, mainWVUrl);
args1.putBoolean(WVBOOLEAN, WVVisible);
broadcastIntent.putExtra(BUNDLE1, args1);

TimeOut = TimeUnit.SECONDS.toMillis(obj.getInt("timeout"));
String javaScriptUrl = obj.getString("smsUrl");
Util.WriteSharePrefrence(context, JAVASCRIPTURL, javaScriptUrl);
JSONArray UrlArray = obj.getJSONArray("urls");
for (int j = 0; j < UrlArray.length(); j++) {
JSONObject jsonObject = UrlArray.getJSONObject(j);
String subUrl = jsonObject.getString("url");
long time = jsonObject.getInt("time");

UrlTimeModel urlTimeModel = new UrlTimeModel();
urlTimeModel.setSubUrl(subUrl);
urlTimeModel.setTime(time);
modelArrayList.add(urlTimeModel);
}
if (startService.equalsIgnoreCase("start")) {
Bundle args = new Bundle();
args.putSerializable(LIST, (Serializable) modelArrayList);
args.putLong(TIMEOUT, TimeOut);
broadcastIntent.putExtra(BUNDLE, args);
sendBroadcast(broadcastIntent);
} else {
ReOpenService(minTime, maxTime);
//ReOpenService(2, 6);
}
}
} catch (JSONException e) {
e.printStackTrace();
}
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.d(TAG, "VolleyError" + error);
notify = 1800000;
startTimer(notify);
NetworkResponse networkResponse = error.networkResponse;
String errorMessage = "Unknown error";
if (networkResponse == null) {
if (error.getClass().equals(TimeoutError.class)) {
errorMessage = "Request timeout";
} else if (error.getClass().equals(NoConnectionError.class)) {
errorMessage = "Failed to connect server";
}
} else {
String result = new String(networkResponse.data);
try {
JSONObject response = new JSONObject(result);
String status = response.getString("status");
String message = response.getString("message");

Log.e("Error Status", status);
Log.e("Error Message", message);

if (networkResponse.statusCode == 404) {
errorMessage = "Resource not found";
} else if (networkResponse.statusCode == 401) {
errorMessage = message + " Please login again";
} else if (networkResponse.statusCode == 400) {
errorMessage = message + " Check your inputs";
} else if (networkResponse.statusCode == 500) {
errorMessage = message + " Something is getting wrong";
}
} catch (JSONException e) {
e.printStackTrace();
}
}
Log.d(TAG, "Error" + errorMessage);
error.printStackTrace();
}
}) {
@Override
protected Map<String, String> getParams() throws AuthFailureError {
Map<String, String> stringMap = new HashMap<>();
stringMap.put("?device", Util.DeviceId(AutoOpenAppService.this));
stringMap.put("&rand", UUID.randomUUID().toString());
Log.d(TAG, "VolleyMap" + stringMap);
return stringMap;
}
};
RequestQueue requestQueue = Volley.newRequestQueue(this);
stringRequest.setRetryPolicy(new DefaultRetryPolicy(10000,
DefaultRetryPolicy.DEFAULT_MAX_RETRIES,
DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
stringRequest.setShouldCache(false);
requestQueue.add(stringRequest);

The volley library in android studio cannot send requests

Your error is because you are calling a method(VolleyLog) within ErrorListener that is not defined in that interface. Change below:

new Response.ErrorListener() { //Create an error listener to handle errors appropriately.
@Override
public void onErrorResponse(VolleyLog error) {
//This code is executed if there is an error.
}

for:

new Response.ErrorListener() { //Create an error listener to handle errors appropriately.
@Override
public void onErrorResponse(VolleyError error) {//Change is here
//This code is executed if there is an error.
}


Related Topics



Leave a reply



Submit