How to implement the concept of JSON streaming for volleyball to avoid OutOfMemory error?

I recently had one error in my implementation of volleyball, I do not use the concept of loading images volleyball, I use it to call more than 10 REST api calls in the view-pager FragmentStatePagerAdapterone http restapi call in one miss, everything works fine, but the application crashed several times . If I run the application 1000 times, it will only work 5 times.

Error Log:

 FATAL EXCEPTION: 
 java.lang.OutOfMemoryError
 E/AndroidRuntime(5798): at com.android.volley.toolbox.DiskBasedCache.streamToBytes(DiskBasedCache.java:316)
 E/AndroidRuntime(5798): at com.android.volley.toolbox.DiskBasedCache.readString(DiskBasedCache.java:526)
 E/AndroidRuntime(5798): at com.android.volley.toolbox.DiskBasedCache.readStringStringMap(DiskBasedCache.java:549)
 E/AndroidRuntime(5798): at com.android.volley.toolbox.DiskBasedCache$CacheHeader.readHeader(DiskBasedCache.java:392)
 E/AndroidRuntime(5798): at com.android.volley.toolbox.DiskBasedCache.initialize(DiskBasedCache.java:155)
 E/AndroidRuntime(5798): at com.android.volley.CacheDispatcher.run(CacheDispatcher.java:85)

By Googling I found a solution that we should use in this JSON streaming concept

Details:

API . JSON : . , . , .

?:

volley OutOfMemory JSON, , Volley Helper JSON.

Volley:

public class GsonRequest<T> extends Request<T> {
private final Gson gson = new Gson();
private final Class<T> clazz;
private final Map<String, String> headers;
private final Listener<T> listener;

/**
* Make a GET request and return a parsed object from JSON.
*
* @param url URL of the request to make
* @param clazz Relevant class object, for Gson reflection
* @param headers Map of request headers
*/
public GsonRequest(String url, Class<T> clazz, Map<String, String> headers,
Listener<T> listener, ErrorListener errorListener) {
super(Method.GET, url, errorListener);
this.clazz = clazz;
this.headers = headers;
this.listener = listener;
}

@Override
public Map<String, String> getHeaders() throws AuthFailureError {
return headers != null ? headers : super.getHeaders();
}

@Override
protected void deliverResponse(T response) {
listener.onResponse(response);
}

@Override
protected Response<T> parseNetworkResponse(NetworkResponse response) {
try {
String json = new String(
response.data, HttpHeaderParser.parseCharset(response.headers));
return Response.success(
gson.fromJson(json, clazz), HttpHeaderParser.parseCacheHeaders(response));
} catch (UnsupportedEncodingException e) {
return Response.error(new ParseError(e));
} catch (JsonSyntaxException e) {
return Response.error(new ParseError(e));
}
}
}

- , JSON, ?

.

+3
1

! Okttp java NIO !

Okhttp 2.0 RC Okio java.io java.nio, , ByteString Buffer, . (FYI Koush OIN NIO!) , ! , .

0

All Articles