Search in sources :

Example 1 with ParseError

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));
    }
}
Also used : Gson(com.google.gson.Gson) UnsupportedEncodingException(java.io.UnsupportedEncodingException) ParseError(com.android.volley.ParseError) JsonParseException(com.google.gson.JsonParseException)

Example 2 with ParseError

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);
}
Also used : VolleyError(com.android.volley.VolleyError) Astronaut(com.restart.spacestationtracker.data.Astronaut) ArrayList(java.util.ArrayList) JSONArray(org.json.JSONArray) UnsupportedEncodingException(java.io.UnsupportedEncodingException) JSONException(org.json.JSONException) LinearLayoutManager(android.support.v7.widget.LinearLayoutManager) PeopleInSpaceAdapter(com.restart.spacestationtracker.adapter.PeopleInSpaceAdapter) JSONException(org.json.JSONException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) Response(com.android.volley.Response) NetworkResponse(com.android.volley.NetworkResponse) JSONObject(org.json.JSONObject) NetworkResponse(com.android.volley.NetworkResponse) ParseError(com.android.volley.ParseError) JsonObjectRequest(com.android.volley.toolbox.JsonObjectRequest)

Example 3 with ParseError

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));
    }
}
Also used : JSONObject(org.json.JSONObject) UnsupportedEncodingException(java.io.UnsupportedEncodingException) JSONException(org.json.JSONException) ParseError(com.android.volley.ParseError)

Example 4 with ParseError

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));
    }
}
Also used : Bitmap(android.graphics.Bitmap) ParseError(com.android.volley.ParseError) BitmapFactory(android.graphics.BitmapFactory)

Example 5 with ParseError

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));
    }
}
Also used : Bitmap(android.graphics.Bitmap) ParseError(com.android.volley.ParseError) BitmapFactory(android.graphics.BitmapFactory)

Aggregations

ParseError (com.android.volley.ParseError)11 Bitmap (android.graphics.Bitmap)7 BitmapFactory (android.graphics.BitmapFactory)7 UnsupportedEncodingException (java.io.UnsupportedEncodingException)3 JSONException (org.json.JSONException)2 JSONObject (org.json.JSONObject)2 LinearLayoutManager (android.support.v7.widget.LinearLayoutManager)1 NetworkResponse (com.android.volley.NetworkResponse)1 Response (com.android.volley.Response)1 VolleyError (com.android.volley.VolleyError)1 JsonObjectRequest (com.android.volley.toolbox.JsonObjectRequest)1 Gson (com.google.gson.Gson)1 JsonParseException (com.google.gson.JsonParseException)1 PeopleInSpaceAdapter (com.restart.spacestationtracker.adapter.PeopleInSpaceAdapter)1 Astronaut (com.restart.spacestationtracker.data.Astronaut)1 Articles (com.stylingandroid.designlibrary.rss.model.Articles)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 InputStream (java.io.InputStream)1 ArrayList (java.util.ArrayList)1 JSONArray (org.json.JSONArray)1