具有有效ResponseBody的翻新/ Moshi EoFException

时间:2019-10-10 09:36:31

标签: android retrofit2 moshi

我正在设置一个ErrorHandler,以在调用失败后解析ResponseBody,尽管将看起来像有效的JSON传递给转换器,但我仍然不断抛出EoFException。

Web服务的原始调用

    public void login(final String username, final String password) {
        executor.execute(() -> {
            webservice.login(username, password).enqueue(new Callback<APIResponse<APIResult<List<User>>>>() {
                @Override
                public void onResponse(Call<APIResponse<APIResult<List<User>>>> call, Response<APIResponse<APIResult<List<User>>>> response) {
                    if (response.isSuccessful()) {
                        executor.execute(() -> {
                            User user = response.body().getResult().getData().get(0);
                            user.setLastRefresh(new Date());
                            user.setIsLoggedIn(1);
                            userDao.save(user);
                        });
                    } else {
                        APIErrorResponse error = errorUtils.parseError(response);
                        Log.v("TopperLogin",error.getResult().getError().getMessage());
                    }
                }
                @Override
                public void onFailure(Call<APIResponse<APIResult<List<User>>>> call, Throwable t) {
                }
            });
        });
    }

ErrorUtils

public class ErrorUtils {

    private Retrofit retrofit;

    @Inject
    public ErrorUtils(Retrofit retrofit) {
        this.retrofit = retrofit;
    }

    public APIErrorResponse parseError(final Response<?> response) {

        Converter<ResponseBody, APIErrorResponse> converter =
                retrofit.responseBodyConverter(APIErrorResponse.class, new Annotation[0]);

        APIErrorResponse result;

        try {
            Log.v("TopperLogin", "Try to map error");
            Log.v("TopperLogin", response.errorBody().string());
            result = converter.convert(response.errorBody());
        } catch (EOFException e) {
            e.printStackTrace();
            return defaultError("Unknown API Error - EOF Exception");
        } catch (JsonDataException e) {
            e.printStackTrace();
            return defaultError("Unknown API Error - Json Data Exception");
        } catch (Exception e) {
            e.printStackTrace();
            return defaultError("Unknown API Error - Exception");
        }
        return result;
    }

    private APIErrorResponse defaultError(String message) {
        APIErrorResponse defResponse = new APIErrorResponse();
        APIErrorResult defResult = new APIErrorResult();
        APIError defError = new APIError();
        defError.setMessage(message);
        defResult.setError(defError);
        defResponse.setResult(defResult);
        return defResponse;
    }
}

Logcat

V/TopperLogin: Try to convert error!
V/TopperLogin: ResponseBody: {"status_code":400,"status":"Error","result":{"data":[],"error":{"message":"Please check your username and password"}}}
W/System.err: java.io.EOFException: End of input
W/System.err:     at com.squareup.moshi.JsonUtf8Reader.nextNonWhitespace(JsonUtf8Reader.java:1043)
W/System.err:     at com.squareup.moshi.JsonUtf8Reader.doPeek(JsonUtf8Reader.java:319)
W/System.err:     at com.squareup.moshi.JsonUtf8Reader.peek(JsonUtf8Reader.java:193)
W/System.err:     at com.squareup.moshi.JsonAdapter$2.fromJson(JsonAdapter.java:134)
W/System.err:     at com.squareup.moshi.JsonAdapter$4.fromJson(JsonAdapter.java:197)
W/System.err:     at retrofit2.converter.moshi.MoshiResponseBodyConverter.convert(MoshiResponseBodyConverter.java:45)
W/System.err:     at retrofit2.converter.moshi.MoshiResponseBodyConverter.convert(MoshiResponseBodyConverter.java:27)
W/System.err:     at com.topper.topper.data.api.ErrorUtils.parseError(ErrorUtils.java:43)
W/System.err:     at com.topper.topper.data.repo.UserRepository$1.onResponse(UserRepository.java:67)
W/System.err:     at retrofit2.DefaultCallAdapterFactory$ExecutorCallbackCall$1$1.run(DefaultCallAdapterFactory.java:83)
W/System.err:     at android.os.Handler.handleCallback(Handler.java:873)
W/System.err:     at android.os.Handler.dispatchMessage(Handler.java:99)
W/System.err:     at android.os.Looper.loop(Looper.java:193)
W/System.err:     at android.app.ActivityThread.main(ActivityThread.java:6898)
W/System.err:     at java.lang.reflect.Method.invoke(Native Method)
W/System.err:     at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:537)
W/System.err:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
V/TopperLogin: Unknown API Error - EOF Exception

也许值得一提的是,如果我输入正确的详细信息,那么在原始调用中一切都可以正常工作(即,我只有在API返回错误时才能解析问题)。

我已经尝试了几个小时,但是我一直回想的是,我正在传递有效的回复,我们将不胜感激。

谢谢。

0 个答案:

没有答案