use of com.android.volley.ParseError in project Douya by DreaminginCodeZH.
the class ApiRequest method parseNetworkResponse.
@Override
protected Response<T> parseNetworkResponse(NetworkResponse response) {
Gson gson = GsonHelper.get();
String responseString;
try {
responseString = new String(response.data, HttpHeaderParser.parseCharset(response.headers));
} catch (UnsupportedEncodingException e) {
return Response.error(new ParseError(e));
}
try {
return Response.success(gson.<T>fromJson(responseString, mType), HttpHeaderParser.parseCacheHeaders(response));
} catch (JsonParseException | OutOfMemoryError e) {
LogUtils.e("Error when parsing response: " + responseString);
return Response.error(new ParseError(e));
}
}
use of com.android.volley.ParseError in project Space-Station-Tracker by Kiarasht.
the class PeopleinSpace method display_people.
/**
* Displays a list of astronauts in a RecyclerView
*/
private void display_people() {
final String url = "http://www.howmanypeopleareinspacerightnow.com/peopleinspace.json";
final List<Astronaut> peopleInSpace = new ArrayList<>();
JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.GET, url, null, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
try {
LinearLayoutManager layoutManager = new LinearLayoutManager(mActivity, LinearLayoutManager.VERTICAL, false);
mRecyclerView.setLayoutManager(layoutManager);
mAdapter = new PeopleInSpaceAdapter(mActivity, peopleInSpace);
mRecyclerView.setHasFixedSize(true);
mRecyclerView.setNestedScrollingEnabled(true);
mAdapter.setDataSet(peopleInSpace);
mRecyclerView.setAdapter(mAdapter);
} catch (Exception e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError e) {
}
}) {
@Override
protected Response<JSONObject> parseNetworkResponse(NetworkResponse response) {
try {
String jsonString = new String(response.data, HttpHeaderParser.parseCharset(response.headers, PROTOCOL_CHARSET));
JSONObject jsonResponse = new JSONObject(jsonString);
try {
JSONArray astronauts = jsonResponse.getJSONArray("people");
for (int i = 0; i < astronauts.length(); ++i) {
JSONObject anAstronaut = astronauts.getJSONObject(i);
final String name = anAstronaut.getString("name");
final String image = anAstronaut.getString("biophoto");
final String countryLink = anAstronaut.getString("countryflag");
final String launchDate = anAstronaut.getString("launchdate");
String role = anAstronaut.getString("title");
final String location = anAstronaut.getString("location");
final String bio = anAstronaut.getString("bio");
final String wiki = anAstronaut.getString("biolink");
final String twitter = anAstronaut.getString("twitter");
if (role != null && !role.isEmpty())
role = role.substring(0, 1).toUpperCase() + role.substring(1);
Astronaut storeAnAstronaut = new Astronaut(name, image, countryLink, launchDate, role, location, bio, wiki, twitter);
peopleInSpace.add(storeAnAstronaut);
}
Collections.sort(peopleInSpace);
} catch (Exception e) {
e.printStackTrace();
}
return Response.success(new JSONObject(jsonString), HttpHeaderParser.parseCacheHeaders(response));
} catch (UnsupportedEncodingException e) {
return Response.error(new ParseError(e));
} catch (JSONException je) {
return Response.error(new ParseError(je));
}
}
};
mRequestQueue.add(jsonObjectRequest);
}
use of com.android.volley.ParseError in project OkVolley by googolmo.
the class BaseRequest method parseNetworkResponse.
@Override
protected // 解析返回的数据
Response<JSONObject> parseNetworkResponse(NetworkResponse response) {
try {
byte[] data = response.data;
String json = new String(data, HttpHeaderParser.parseCharset(response.headers));
if (VolleyLog.DEBUG) {
VolleyLog.d("response:%s", json);
}
return Response.success(new JSONObject(json), HttpHeaderParser.parseCacheHeaders(response));
} catch (UnsupportedEncodingException e) {
return Response.error(new ParseError(e));
} catch (JSONException e) {
return Response.error(new ParseError(e));
}
}
use of com.android.volley.ParseError in project android-volley by mcxiaoke.
the class ImageRequest method doParse.
/**
* The real guts of parseNetworkResponse. Broken out for readability.
*/
private Response<Bitmap> doParse(NetworkResponse response) {
byte[] data = response.data;
BitmapFactory.Options decodeOptions = new BitmapFactory.Options();
Bitmap bitmap = null;
if (mMaxWidth == 0 && mMaxHeight == 0) {
decodeOptions.inPreferredConfig = mDecodeConfig;
bitmap = BitmapFactory.decodeByteArray(data, 0, data.length, decodeOptions);
} else {
// If we have to resize this image, first get the natural bounds.
decodeOptions.inJustDecodeBounds = true;
BitmapFactory.decodeByteArray(data, 0, data.length, decodeOptions);
int actualWidth = decodeOptions.outWidth;
int actualHeight = decodeOptions.outHeight;
// Then compute the dimensions we would ideally like to decode to.
int desiredWidth = getResizedDimension(mMaxWidth, mMaxHeight, actualWidth, actualHeight, mScaleType);
int desiredHeight = getResizedDimension(mMaxHeight, mMaxWidth, actualHeight, actualWidth, mScaleType);
// Decode to the nearest power of two scaling factor.
decodeOptions.inJustDecodeBounds = false;
// TODO(ficus): Do we need this or is it okay since API 8 doesn't support it?
// decodeOptions.inPreferQualityOverSpeed = PREFER_QUALITY_OVER_SPEED;
decodeOptions.inSampleSize = findBestSampleSize(actualWidth, actualHeight, desiredWidth, desiredHeight);
Bitmap tempBitmap = BitmapFactory.decodeByteArray(data, 0, data.length, decodeOptions);
// If necessary, scale down to the maximal acceptable size.
if (tempBitmap != null && (tempBitmap.getWidth() > desiredWidth || tempBitmap.getHeight() > desiredHeight)) {
bitmap = Bitmap.createScaledBitmap(tempBitmap, desiredWidth, desiredHeight, true);
tempBitmap.recycle();
} else {
bitmap = tempBitmap;
}
}
if (bitmap == null) {
return Response.error(new ParseError(response));
} else {
return Response.success(bitmap, HttpHeaderParser.parseCacheHeaders(response));
}
}
use of com.android.volley.ParseError in project SimplifyReader by chentao0707.
the class ImageRequest method doParse.
/**
* The real guts of parseNetworkResponse. Broken out for readability.
*/
private Response<Bitmap> doParse(NetworkResponse response) {
byte[] data = response.data;
BitmapFactory.Options decodeOptions = new BitmapFactory.Options();
Bitmap bitmap = null;
if (mMaxWidth == 0 && mMaxHeight == 0) {
decodeOptions.inPreferredConfig = mDecodeConfig;
bitmap = BitmapFactory.decodeByteArray(data, 0, data.length, decodeOptions);
} else {
// If we have to resize this image, first get the natural bounds.
decodeOptions.inJustDecodeBounds = true;
BitmapFactory.decodeByteArray(data, 0, data.length, decodeOptions);
int actualWidth = decodeOptions.outWidth;
int actualHeight = decodeOptions.outHeight;
// Then compute the dimensions we would ideally like to decode to.
int desiredWidth = getResizedDimension(mMaxWidth, mMaxHeight, actualWidth, actualHeight, mScaleType);
int desiredHeight = getResizedDimension(mMaxHeight, mMaxWidth, actualHeight, actualWidth, mScaleType);
// Decode to the nearest power of two scaling factor.
decodeOptions.inJustDecodeBounds = false;
// TODO(ficus): Do we need this or is it okay since API 8 doesn't support it?
// decodeOptions.inPreferQualityOverSpeed = PREFER_QUALITY_OVER_SPEED;
decodeOptions.inSampleSize = findBestSampleSize(actualWidth, actualHeight, desiredWidth, desiredHeight);
Bitmap tempBitmap = BitmapFactory.decodeByteArray(data, 0, data.length, decodeOptions);
// If necessary, scale down to the maximal acceptable size.
if (tempBitmap != null && (tempBitmap.getWidth() > desiredWidth || tempBitmap.getHeight() > desiredHeight)) {
bitmap = Bitmap.createScaledBitmap(tempBitmap, desiredWidth, desiredHeight, true);
tempBitmap.recycle();
} else {
bitmap = tempBitmap;
}
}
if (bitmap == null) {
return Response.error(new ParseError(response));
} else {
return Response.success(bitmap, HttpHeaderParser.parseCacheHeaders(response));
}
}
Aggregations