Search in sources :

Example 6 with JsonParseException

use of com.google.gson.JsonParseException 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 7 with JsonParseException

use of com.google.gson.JsonParseException in project fc-java-sdk by aliyun.

the class DefaultFcClient method doAction.

public HttpResponse doAction(HttpRequest request, String form, String method) throws ClientException, ServerException {
    request.validate();
    try {
        PrepareUrl prepareUrl = signRequest(request, form, method);
        int retryTimes = 1;
        HttpResponse response = HttpResponse.getResponse(prepareUrl.getUrl(), prepareUrl.getHeader(), request, method, config.getConnectTimeoutMillis(), config.getReadTimeoutMillis());
        while (500 <= response.getStatus() && AUTO_RETRY && retryTimes < MAX_RETRIES) {
            prepareUrl = signRequest(request, form, method);
            response = HttpResponse.getResponse(prepareUrl.getUrl(), prepareUrl.getHeader(), request, method, config.getConnectTimeoutMillis(), config.getReadTimeoutMillis());
            retryTimes++;
        }
        if (response.getStatus() >= 500) {
            String requestId = response.getHeaderValue(HeaderKeys.REQUEST_ID);
            String stringContent = response.getContent() == null ? "" : new String(response.getContent());
            ServerException se;
            try {
                se = new Gson().fromJson(stringContent, ServerException.class);
            } catch (JsonParseException e) {
                se = new ServerException("InternalServiceError", "Failed to parse response content", requestId);
            }
            se.setStatusCode(response.getStatus());
            se.setRequestId(requestId);
            throw se;
        } else if (response.getStatus() >= 300) {
            ClientException ce;
            if (response.getContent() == null) {
                ce = new ClientException("SDK.ServerUnreachable", "Failed to get response content from server");
            } else {
                try {
                    ce = new Gson().fromJson(new String(response.getContent()), ClientException.class);
                } catch (JsonParseException e) {
                    ce = new ClientException("SDK.ResponseNotParsable", "Failed to parse response content", e);
                }
            }
            if (ce == null) {
                ce = new ClientException("SDK.UnknownError", "Unknown client error");
            }
            ce.setStatusCode(response.getStatus());
            ce.setRequestId(response.getHeaderValue(HeaderKeys.REQUEST_ID));
            throw ce;
        }
        return response;
    } catch (InvalidKeyException exp) {
        throw new ClientException("SDK.InvalidAccessSecret", "Speicified access secret is not valid.");
    } catch (SocketTimeoutException exp) {
        throw new ClientException("SDK.ServerUnreachable", "SocketTimeoutException has occurred on a socket read or accept.");
    } catch (IOException exp) {
        throw new ClientException("SDK.ServerUnreachable", "Server unreachable: " + exp.toString());
    } catch (NoSuchAlgorithmException exp) {
        throw new ClientException("SDK.InvalidMD5Algorithm", "MD5 hash is not supported by client side.");
    }
}
Also used : PrepareUrl(com.aliyuncs.fc.model.PrepareUrl) ServerException(com.aliyuncs.fc.exceptions.ServerException) SocketTimeoutException(java.net.SocketTimeoutException) HttpResponse(com.aliyuncs.fc.http.HttpResponse) Gson(com.google.gson.Gson) ClientException(com.aliyuncs.fc.exceptions.ClientException) IOException(java.io.IOException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) JsonParseException(com.google.gson.JsonParseException) InvalidKeyException(java.security.InvalidKeyException)

Example 8 with JsonParseException

use of com.google.gson.JsonParseException in project Rutgers-Course-Tracker by tevjef.

the class SubjectFragment method showError.

@Override
public void showError(Throwable t) {
    String message;
    Resources resources = getContext().getResources();
    if (t instanceof UnknownHostException) {
        message = resources.getString(R.string.no_internet);
    } else if (t instanceof JsonParseException || t instanceof RutgersServerIOException) {
        message = resources.getString(R.string.server_down);
    } else if (t instanceof RutgersDataIOException) {
        notifySectionNotOpen();
        getParentActivity().onBackPressed();
        return;
    } else if (t instanceof SocketTimeoutException) {
        message = resources.getString(R.string.timed_out);
    } else {
        message = t.getMessage();
    }
    //error message finalized, now save it.
    mViewState.errorMessage = message;
    // Redirects the message that would usually be in the snackbar, to error layout.
    if (!adapterHasItems()) {
        showLayout(LayoutType.ERROR);
        TextView textViewMessage = ButterKnife.findById(mErrorView, R.id.text);
        textViewMessage.setText(message);
    } else {
        showSnackBar(message);
    }
}
Also used : RutgersDataIOException(com.tevinjeffrey.rutgersct.rutgersapi.exceptions.RutgersDataIOException) SocketTimeoutException(java.net.SocketTimeoutException) UnknownHostException(java.net.UnknownHostException) RutgersServerIOException(com.tevinjeffrey.rutgersct.rutgersapi.exceptions.RutgersServerIOException) TextView(android.widget.TextView) Resources(android.content.res.Resources) JsonParseException(com.google.gson.JsonParseException)

Example 9 with JsonParseException

use of com.google.gson.JsonParseException in project Rutgers-Course-Tracker by tevjef.

the class CourseFragment method showError.

@Override
public void showError(Throwable t) {
    String message;
    Resources resources = getContext().getResources();
    if (t instanceof UnknownHostException) {
        message = resources.getString(R.string.no_internet);
    } else if (t instanceof JsonParseException || t instanceof RutgersServerIOException) {
        message = resources.getString(R.string.server_down);
    } else if (t instanceof SocketTimeoutException) {
        message = resources.getString(R.string.timed_out);
    } else {
        message = t.getMessage();
    }
    //error message finalized, now save it.
    mViewState.errorMessage = message;
    // Redirects the message that would usually be in the snackbar, to error layout.
    if (!adapterHasItems()) {
        showLayout(LayoutType.ERROR);
        TextView textViewMessage = ButterKnife.findById(mErrorView, R.id.text);
        textViewMessage.setText(message);
    } else {
        showSnackBar(message);
    }
}
Also used : SocketTimeoutException(java.net.SocketTimeoutException) UnknownHostException(java.net.UnknownHostException) RutgersServerIOException(com.tevinjeffrey.rutgersct.rutgersapi.exceptions.RutgersServerIOException) TextView(android.widget.TextView) Resources(android.content.res.Resources) JsonParseException(com.google.gson.JsonParseException)

Example 10 with JsonParseException

use of com.google.gson.JsonParseException in project Rutgers-Course-Tracker by tevjef.

the class TrackedSectionsFragment method showError.

@Override
public void showError(Throwable t) {
    String message;
    Resources resources = getContext().getResources();
    if (t instanceof UnknownHostException) {
        message = resources.getString(R.string.no_internet);
    } else if (t instanceof JsonParseException || t instanceof RutgersServerIOException) {
        message = resources.getString(R.string.server_down);
    } else if (t instanceof SocketTimeoutException) {
        message = resources.getString(R.string.timed_out);
    } else {
        message = t.getMessage();
    }
    // Save current error message ahead of config change.
    mViewState.errorMessage = message;
    // https://www.google.com/design/spec/patterns/errors.html#errors-app-errors
    if (!adapterHasItems()) {
        showLayout(LayoutType.ERROR);
        TextView textViewMessage = ButterKnife.findById(mErrorView, R.id.text);
        textViewMessage.setText(message);
    } else {
        showSnackBar(message);
    }
}
Also used : SocketTimeoutException(java.net.SocketTimeoutException) UnknownHostException(java.net.UnknownHostException) RutgersServerIOException(com.tevinjeffrey.rutgersct.rutgersapi.exceptions.RutgersServerIOException) TextView(android.widget.TextView) Resources(android.content.res.Resources) JsonParseException(com.google.gson.JsonParseException)

Aggregations

JsonParseException (com.google.gson.JsonParseException)210 JsonObject (com.google.gson.JsonObject)81 JsonElement (com.google.gson.JsonElement)55 IOException (java.io.IOException)54 Gson (com.google.gson.Gson)34 InputStreamReader (java.io.InputStreamReader)24 JsonArray (com.google.gson.JsonArray)21 InputStream (java.io.InputStream)20 GsonBuilder (com.google.gson.GsonBuilder)18 JsonParser (com.google.gson.JsonParser)18 JsonPrimitive (com.google.gson.JsonPrimitive)18 Type (java.lang.reflect.Type)17 Map (java.util.Map)17 JsonReader (com.google.gson.stream.JsonReader)16 ArrayList (java.util.ArrayList)14 HttpUrl (okhttp3.HttpUrl)13 Request (okhttp3.Request)13 Response (okhttp3.Response)13 JsonSyntaxException (com.google.gson.JsonSyntaxException)10 SocketTimeoutException (java.net.SocketTimeoutException)8