Update the json readers.

Proper exceptions
Fixed boardsreader throwing an exception instead of just skiping the key, if the key was not "boards"
captchafix
Florens Douwes 11 years ago
parent d6e87ae5ce
commit 40cdc676d1
  1. 13
      Clover/app/src/main/java/org/floens/chan/core/net/BoardsRequest.java
  2. 39
      Clover/app/src/main/java/org/floens/chan/core/net/ChanReaderRequest.java
  3. 47
      Clover/app/src/main/java/org/floens/chan/core/net/JsonReaderRequest.java

@ -34,16 +34,10 @@ public class BoardsRequest extends JsonReaderRequest<List<Board>> {
}
@Override
public List<Board> readJson(JsonReader reader) {
return parseJson(reader);
}
private List<Board> parseJson(JsonReader reader) {
public List<Board> readJson(JsonReader reader) throws Exception {
List<Board> list = new ArrayList<>();
try {
reader.beginObject();
// Page object
while (reader.hasNext()) {
String key = reader.nextName();
if (key.equals("boards")) {
@ -55,13 +49,10 @@ public class BoardsRequest extends JsonReaderRequest<List<Board>> {
reader.endArray();
} else {
throw new IOException("Invalid data received");
reader.skipValue();
}
}
reader.endObject();
} catch (IOException | IllegalStateException | NumberFormatException e) {
e.printStackTrace();
}
return list;
}

@ -19,7 +19,6 @@ package org.floens.chan.core.net;
import android.util.JsonReader;
import com.android.volley.ParseError;
import com.android.volley.Response.ErrorListener;
import com.android.volley.Response.Listener;
@ -71,7 +70,7 @@ public class ChanReaderRequest extends JsonReaderRequest<List<Post>> {
}
@Override
public List<Post> readJson(JsonReader reader) {
public List<Post> readJson(JsonReader reader) throws Exception {
List<Post> list;
if (loadable.isBoardMode()) {
@ -84,16 +83,10 @@ public class ChanReaderRequest extends JsonReaderRequest<List<Post>> {
throw new IllegalArgumentException("Unknown mode");
}
List<Post> result = null;
try {
result = processPosts(list);
} catch (IOException e) {
setError(new ParseError(e));
}
return result;
return processPosts(list);
}
private List<Post> processPosts(List<Post> serverList) throws IOException {
private List<Post> processPosts(List<Post> serverList) throws Exception {
boolean anonymize = ChanPreferences.getAnonymize();
boolean anonymizeIds = ChanPreferences.getAnonymizeIds();
String name = ChanApplication.getInstance().getString(R.string.default_name);
@ -188,14 +181,14 @@ public class ChanReaderRequest extends JsonReaderRequest<List<Post>> {
return totalList;
}
private List<Post> loadThread(JsonReader reader) {
private List<Post> loadThread(JsonReader reader) throws Exception {
ArrayList<Post> list = new ArrayList<>();
try {
reader.beginObject();
// Page object
while (reader.hasNext()) {
if (reader.nextName().equals("posts")) {
String key = reader.nextName();
if (key.equals("posts")) {
reader.beginArray();
// Thread array
while (reader.hasNext()) {
@ -208,18 +201,13 @@ public class ChanReaderRequest extends JsonReaderRequest<List<Post>> {
}
}
reader.endObject();
} catch (IOException | IllegalStateException | NumberFormatException e) {
e.printStackTrace();
setError(new ParseError(e));
}
return list;
}
private List<Post> loadBoard(JsonReader reader) {
private List<Post> loadBoard(JsonReader reader) throws Exception {
ArrayList<Post> list = new ArrayList<>();
try {
reader.beginObject(); // Threads array
if (reader.nextName().equals("threads")) {
@ -251,18 +239,13 @@ public class ChanReaderRequest extends JsonReaderRequest<List<Post>> {
}
reader.endObject();
} catch (IOException | IllegalStateException | NumberFormatException e) {
e.printStackTrace();
setError(new ParseError(e));
}
return list;
}
private List<Post> loadCatalog(JsonReader reader) {
private List<Post> loadCatalog(JsonReader reader) throws Exception {
ArrayList<Post> list = new ArrayList<>();
try {
reader.beginArray(); // Array of pages
while (reader.hasNext()) {
@ -286,15 +269,11 @@ public class ChanReaderRequest extends JsonReaderRequest<List<Post>> {
}
reader.endArray();
} catch (IOException | IllegalStateException | NumberFormatException e) {
e.printStackTrace();
setError(new ParseError(e));
}
return list;
}
private Post readPostObject(JsonReader reader) throws IllegalStateException, NumberFormatException, IOException {
private Post readPostObject(JsonReader reader) throws Exception {
Post post = new Post();
post.board = loadable.board;

@ -20,7 +20,6 @@ package org.floens.chan.core.net;
import android.util.JsonReader;
import com.android.volley.NetworkResponse;
import com.android.volley.ParseError;
import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.Response.ErrorListener;
@ -35,7 +34,6 @@ import java.io.UnsupportedEncodingException;
public abstract class JsonReaderRequest<T> extends Request<T> {
protected final Listener<T> listener;
private VolleyError error;
public JsonReaderRequest(String url, Listener<T> listener, ErrorListener errorListener) {
super(Method.GET, url, errorListener);
@ -48,36 +46,51 @@ public abstract class JsonReaderRequest<T> extends Request<T> {
listener.onResponse(response);
}
public void setError(VolleyError error) {
this.error = error;
}
@Override
protected Response<T> parseNetworkResponse(NetworkResponse response) {
try {
ByteArrayInputStream baos = new ByteArrayInputStream(response.data);
JsonReader reader = new JsonReader(new InputStreamReader(baos, "UTF-8"));
JsonReader reader = null;
try {
reader = new JsonReader(new InputStreamReader(baos, "UTF-8"));
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
T read = readJson(reader);
Exception exception = null;
T read = null;
try {
read = readJson(reader);
} catch (Exception e) {
exception = e;
}
if (reader != null) {
try {
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (read == null && error == null) {
return Response.error(new VolleyError());
} else if (error != null) {
return Response.error(error);
if (read == null) {
if (exception != null) {
return Response.error(new VolleyError(exception));
} else {
return Response.success(read, HttpHeaderParser.parseCacheHeaders(response));
return Response.error(new VolleyError("Unknown error"));
}
} catch (UnsupportedEncodingException e) {
return Response.error(new ParseError(e));
} else {
return Response.success(read, HttpHeaderParser.parseCacheHeaders(response));
}
}
public abstract T readJson(JsonReader reader);
/**
* Read your json. Returning null or throwing something means a Response.error, Response.success is returned otherwise.
* The reader is closed for you.
*
* @param reader A json reader to use
* @return null or the data
* @throws Exception none or an exception
*/
public abstract T readJson(JsonReader reader) throws Exception;
}

Loading…
Cancel
Save