Search in sources :

Example 26 with HttpException

use of retrofit2.HttpException in project SmartCampus by Vegen.

the class HttpError method getErrorCode.

public static int getErrorCode(Throwable throwable) {
    if (throwable instanceof HttpException) {
        try {
            HttpException httpException = (HttpException) throwable;
            String errorMsg = httpException.response().errorBody().string();
            HashMap data = new Gson().fromJson(errorMsg, HashMap.class);
            if (data.containsKey("msg")) {
                return ((Double) data.get("code")).intValue();
            } else {
                return -1;
            }
        } catch (IOException e) {
            LogUtils.e(e.getMessage());
        } catch (JsonSyntaxException e) {
            LogUtils.e(e.getMessage());
            return -1;
        } catch (Exception e) {
            return -1;
        }
    }
    return -1;
}
Also used : JsonSyntaxException(com.google.gson.JsonSyntaxException) HashMap(java.util.HashMap) Gson(com.google.gson.Gson) HttpException(retrofit2.HttpException) IOException(java.io.IOException) JsonSyntaxException(com.google.gson.JsonSyntaxException) SocketTimeoutException(java.net.SocketTimeoutException) HttpException(retrofit2.HttpException) IOException(java.io.IOException) UnknownHostException(java.net.UnknownHostException)

Example 27 with HttpException

use of retrofit2.HttpException in project BaseProject by fly803.

the class ExceptionHandle method handleException.

public static ResponeThrowable handleException(Throwable e) {
    ResponeThrowable ex;
    if (e instanceof HttpException) {
        HttpException httpException = (HttpException) e;
        ex = new ResponeThrowable(e, ERROR.HTTP_ERROR);
        switch(httpException.code()) {
            case UNAUTHORIZED:
            case FORBIDDEN:
            case NOT_FOUND:
            case REQUEST_TIMEOUT:
            case GATEWAY_TIMEOUT:
            case INTERNAL_SERVER_ERROR:
            case BAD_GATEWAY:
            case SERVICE_UNAVAILABLE:
            default:
                ex.message = "网络错误";
                break;
        }
        return ex;
    } else if (e instanceof ServerException) {
        ServerException resultException = (ServerException) e;
        ex = new ResponeThrowable(resultException, resultException.getCode());
        ex.message = resultException.getMessage();
        return ex;
    } else if (e instanceof JsonParseException || e instanceof JSONException || e instanceof ParseException) {
        ex = new ResponeThrowable(e, ERROR.PARSE_ERROR);
        ex.message = "解析错误";
        return ex;
    } else if (e instanceof ConnectException) {
        ex = new ResponeThrowable(e, ERROR.NETWORD_ERROR);
        ex.message = "连接失败";
        return ex;
    } else if (e instanceof javax.net.ssl.SSLHandshakeException) {
        ex = new ResponeThrowable(e, ERROR.SSL_ERROR);
        ex.message = "证书验证失败";
        return ex;
    } else if (e instanceof ApiException) {
        ex = new ResponeThrowable(e, ((ApiException) e).getCode());
        ex.message = e.getMessage();
        return ex;
    } else {
        ex = new ResponeThrowable(e, ERROR.UNKNOWN);
        ex.message = "未知错误";
        return ex;
    }
}
Also used : JSONException(org.json.JSONException) HttpException(retrofit2.HttpException) JsonParseException(com.google.gson.JsonParseException) ParseException(android.net.ParseException) JsonParseException(com.google.gson.JsonParseException) ConnectException(java.net.ConnectException)

Example 28 with HttpException

use of retrofit2.HttpException in project AndroidComponent by funnyzhaov.

the class ExceptionHandle method handleException.

/**
 * 异常的处理
 */
@NonNull
public static Throwable handleException(Throwable e) {
    ResponseThrowable ex;
    if (e instanceof HttpException) {
        HttpException httpException = (HttpException) e;
        ex = new ResponseThrowable(e, ERROR.HTTP_ERROR);
        switch(httpException.code()) {
            case UNAUTHORIZED:
            case FORBIDDEN:
            case NOT_FOUND:
            case REQUEST_TIMEOUT:
            case GATEWAY_TIMEOUT:
            case INTERNAL_SERVER_ERROR:
            case BAD_GATEWAY:
            case SERVICE_UNAVAILABLE:
            default:
                ex.message = "网络错误";
                break;
        }
        return ex;
    } else if (e instanceof ServerException) {
        ServerException resultException = (ServerException) e;
        ex = new ResponseThrowable(resultException, resultException.status);
        ex.message = resultException.message;
        return ex;
    } else if (e instanceof JsonParseException || e instanceof JSONException || e instanceof ParseException) {
        ex = new ResponseThrowable(e, ERROR.PARSE_ERROR);
        ex.message = "解析错误";
        return ex;
    } else if (e instanceof ConnectException) {
        ex = new ResponseThrowable(e, ERROR.NETWORD_ERROR);
        ex.message = "连接失败";
        return ex;
    } else if (e instanceof javax.net.ssl.SSLHandshakeException) {
        ex = new ResponseThrowable(e, ERROR.SSL_ERROR);
        ex.message = "证书验证失败";
        return ex;
    } else if (e instanceof ConnectTimeoutException) {
        ex = new ResponseThrowable(e, ERROR.TIMEOUT_ERROR);
        ex.message = "连接超时";
        return ex;
    } else if (e instanceof java.net.SocketTimeoutException) {
        ex = new ResponseThrowable(e, ERROR.TIMEOUT_ERROR);
        ex.message = "连接超时";
        return ex;
    } else {
        ex = new ResponseThrowable(e, ERROR.UNKNOWN);
        ex.message = "未知错误";
        return ex;
    }
}
Also used : JSONException(org.json.JSONException) HttpException(retrofit2.HttpException) JsonParseException(com.google.gson.JsonParseException) ParseException(java.text.ParseException) JsonParseException(com.google.gson.JsonParseException) ConnectException(java.net.ConnectException) ConnectTimeoutException(org.apache.http.conn.ConnectTimeoutException) NonNull(android.support.annotation.NonNull)

Example 29 with HttpException

use of retrofit2.HttpException in project talk-android by nextcloud.

the class CallsListController method fetchData.

private void fetchData(boolean fromBottomSheet) {
    dispose(null);
    callItems = new ArrayList<>();
    roomsQueryDisposable = ncApi.getRooms(ApiUtils.getCredentials(userEntity.getUsername(), userEntity.getToken()), ApiUtils.getUrlForGetRooms(userEntity.getBaseUrl())).subscribeOn(Schedulers.newThread()).observeOn(AndroidSchedulers.mainThread()).subscribe(roomsOverall -> {
        if (roomsOverall != null) {
            for (int i = 0; i < roomsOverall.getOcs().getData().size(); i++) {
                callItems.add(new CallItem(roomsOverall.getOcs().getData().get(i), userEntity));
            }
            adapter.updateDataSet(callItems, true);
            Collections.sort(callItems, (callItem, t1) -> Long.compare(t1.getModel().getLastPing(), callItem.getModel().getLastPing()));
            if (searchItem != null) {
                searchItem.setVisible(callItems.size() > 0);
            }
        }
        if (swipeRefreshLayout != null) {
            swipeRefreshLayout.setRefreshing(false);
        }
    }, throwable -> {
        if (searchItem != null) {
            searchItem.setVisible(false);
        }
        if (throwable instanceof HttpException) {
            HttpException exception = (HttpException) throwable;
            switch(exception.code()) {
                case 401:
                    if (getParentController() != null && getParentController().getRouter() != null) {
                        getParentController().getRouter().pushController((RouterTransaction.with(new WebViewLoginController(userEntity.getBaseUrl(), true)).pushChangeHandler(new VerticalChangeHandler()).popChangeHandler(new VerticalChangeHandler())));
                    }
                    break;
                default:
                    break;
            }
        }
        if (swipeRefreshLayout != null) {
            swipeRefreshLayout.setRefreshing(false);
        }
        dispose(roomsQueryDisposable);
    }, () -> {
        dispose(roomsQueryDisposable);
        if (swipeRefreshLayout != null) {
            swipeRefreshLayout.setRefreshing(false);
        }
        if (fromBottomSheet) {
            new Handler().postDelayed(() -> {
                bottomSheet.setCancelable(true);
                if (bottomSheet.isShowing()) {
                    bottomSheet.cancel();
                }
            }, 2500);
        }
    });
}
Also used : AutoInjector(autodagger.AutoInjector) Bundle(android.os.Bundle) UserUtils(com.nextcloud.talk.utils.database.user.UserUtils) EntryMenuController(com.nextcloud.talk.controllers.bottomsheet.EntryMenuController) SearchView(android.support.v7.widget.SearchView) CallMenuController(com.nextcloud.talk.controllers.bottomsheet.CallMenuController) AndroidSchedulers(io.reactivex.android.schedulers.AndroidSchedulers) DividerItemDecoration(android.support.v7.widget.DividerItemDecoration) BindView(butterknife.BindView) ApiUtils(com.nextcloud.talk.utils.ApiUtils) BottomSheet(com.kennyc.bottomsheet.BottomSheet) Handler(android.os.Handler) View(android.view.View) Schedulers(io.reactivex.schedulers.Schedulers) VerticalChangeHandler(com.bluelinelabs.conductor.changehandler.VerticalChangeHandler) Participant(com.nextcloud.talk.models.json.participants.Participant) MenuItemCompat(android.support.v4.view.MenuItemCompat) HttpException(retrofit2.HttpException) InputType(android.text.InputType) FlexibleAdapter(eu.davidea.flexibleadapter.FlexibleAdapter) ThreadMode(org.greenrobot.eventbus.ThreadMode) ViewGroup(android.view.ViewGroup) NoOpControllerChangeHandler(com.bluelinelabs.conductor.internal.NoOpControllerChangeHandler) BundleKeys(com.nextcloud.talk.utils.bundle.BundleKeys) List(java.util.List) Disposable(io.reactivex.disposables.Disposable) Parcels(org.parceler.Parcels) FastScroller(eu.davidea.fastscroller.FastScroller) CallItem(com.nextcloud.talk.adapters.items.CallItem) Nullable(android.support.annotation.Nullable) SearchManager(android.app.SearchManager) EditorInfo(android.view.inputmethod.EditorInfo) Context(android.content.Context) MoreMenuClickEvent(com.nextcloud.talk.events.MoreMenuClickEvent) UserEntity(com.nextcloud.talk.models.database.UserEntity) SmoothScrollLinearLayoutManager(eu.davidea.flexibleadapter.common.SmoothScrollLinearLayoutManager) Intent(android.content.Intent) NonNull(android.support.annotation.NonNull) MenuItem(android.view.MenuItem) ArrayList(java.util.ArrayList) BottomSheetLockEvent(com.nextcloud.talk.events.BottomSheetLockEvent) Inject(javax.inject.Inject) BaseController(com.nextcloud.talk.controllers.base.BaseController) MenuInflater(android.view.MenuInflater) EventBus(org.greenrobot.eventbus.EventBus) Menu(android.view.Menu) SwipeRefreshLayout(android.support.v4.widget.SwipeRefreshLayout) NcApi(com.nextcloud.talk.api.NcApi) BottomNavigationView(android.support.design.widget.BottomNavigationView) R(com.nextcloud.talk.R) Room(com.nextcloud.talk.models.json.rooms.Room) LayoutInflater(android.view.LayoutInflater) TextUtils(android.text.TextUtils) HorizontalChangeHandler(com.bluelinelabs.conductor.changehandler.HorizontalChangeHandler) RecyclerView(android.support.v7.widget.RecyclerView) Subscribe(org.greenrobot.eventbus.Subscribe) KeyboardUtils(com.nextcloud.talk.utils.KeyboardUtils) CallActivity(com.nextcloud.talk.activities.CallActivity) ViewTreeObserver(android.view.ViewTreeObserver) Collections(java.util.Collections) NextcloudTalkApplication(com.nextcloud.talk.application.NextcloudTalkApplication) RouterTransaction(com.bluelinelabs.conductor.RouterTransaction) Handler(android.os.Handler) VerticalChangeHandler(com.bluelinelabs.conductor.changehandler.VerticalChangeHandler) NoOpControllerChangeHandler(com.bluelinelabs.conductor.internal.NoOpControllerChangeHandler) HorizontalChangeHandler(com.bluelinelabs.conductor.changehandler.HorizontalChangeHandler) CallItem(com.nextcloud.talk.adapters.items.CallItem) HttpException(retrofit2.HttpException) VerticalChangeHandler(com.bluelinelabs.conductor.changehandler.VerticalChangeHandler)

Example 30 with HttpException

use of retrofit2.HttpException in project xabber-android by redsolution.

the class RetrofitErrorConverter method throwableToHttpError.

@Nullable
public static String throwableToHttpError(Throwable throwable) {
    String errorMessage = null;
    APIError error = null;
    if (throwable instanceof HttpException) {
        HttpException exception = (HttpException) throwable;
        Response response = exception.response();
        ResponseBody responseBody = response.errorBody();
        if (responseBody != null) {
            Converter<ResponseBody, APIError> converter = HttpApiManager.getRetrofit().responseBodyConverter(APIError.class, new Annotation[0]);
            try {
                error = converter.convert(responseBody);
            } catch (IOException | JsonSyntaxException e) {
                e.printStackTrace();
            }
        }
        if (error != null) {
            if (error.getDetail() != null)
                errorMessage = error.getDetail();
            else if (error.getDetails() != null)
                errorMessage = error.getDetails();
            else if (error.getEmail() != null && error.getEmail().size() > 0)
                errorMessage = error.getEmail().get(0);
            else if (error.getCredentials() != null && error.getCredentials().size() > 0)
                errorMessage = error.getCredentials().get(0);
            else if (error.getCode() != null && error.getCode().size() > 0)
                errorMessage = error.getCode().get(0);
            else if (error.getUsername() != null && error.getUsername().size() > 0)
                errorMessage = error.getUsername().get(0);
            else if (error.getPhone() != null && error.getPhone().size() > 0)
                errorMessage = error.getPhone().get(0);
        }
    }
    return errorMessage;
}
Also used : Response(retrofit2.Response) JsonSyntaxException(com.google.gson.JsonSyntaxException) HttpException(retrofit2.HttpException) IOException(java.io.IOException) ResponseBody(okhttp3.ResponseBody) Nullable(androidx.annotation.Nullable)

Aggregations

HttpException (retrofit2.HttpException)32 HttpException (retrofit2.adapter.rxjava.HttpException)18 Test (org.junit.Test)14 JSONException (org.json.JSONException)12 Intent (android.content.Intent)11 JsonParseException (com.google.gson.JsonParseException)11 ConnectException (java.net.ConnectException)10 Bundle (android.os.Bundle)9 ParseException (android.net.ParseException)8 IOException (java.io.IOException)7 SocketTimeoutException (java.net.SocketTimeoutException)7 UnknownHostException (java.net.UnknownHostException)7 Response (retrofit2.Response)7 View (android.view.View)6 Build (com.github.vase4kin.teamcityapp.buildlist.api.Build)6 Disposable (io.reactivex.disposables.Disposable)6 List (java.util.List)6 TextView (android.widget.TextView)5 TextUtils (android.text.TextUtils)4 BuildCancelRequest (com.github.vase4kin.teamcityapp.build_details.api.BuildCancelRequest)4