1

I am using Retrofit and RxAndroid to send GET request to server which is based on Django, and the server response with a JSON data. The interesting thing i found is, with stetho, the last right brace of Json is lost. So i got this error msg:

W/System.err: java.io.EOFException: End of input at line 1 column 812 path $.user
W/System.err:     at com.google.gson.stream.JsonReader.nextNonWhitespace(JsonReader.java:1393)
W/System.err:     at com.google.gson.stream.JsonReader.doPeek(JsonReader.java:482)
W/System.err:     at com.google.gson.stream.JsonReader.hasNext(JsonReader.java:414)
W/System.err:     at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:214)
W/System.err:     at retrofit2.converter.gson.GsonResponseBodyConverter.convert(GsonResponseBodyConverter.java:37)
W/System.err:     at retrofit2.converter.gson.GsonResponseBodyConverter.convert(GsonResponseBodyConverter.java:25)
W/System.err:     at retrofit2.ServiceMethod.toResponse(ServiceMethod.java:117)
W/System.err:     at retrofit2.OkHttpCall.parseResponse(OkHttpCall.java:211)
W/System.err:     at retrofit2.OkHttpCall.execute(OkHttpCall.java:174)
W/System.err:     at retrofit2.adapter.rxjava.RxJavaCallAdapterFactory$RequestArbiter.request(RxJavaCallAdapterFactory.java:171)
W/System.err:     at rx.Subscriber.setProducer(Subscriber.java:211)
W/System.err:     at rx.internal.operators.OnSubscribeMap$MapSubscriber.setProducer(OnSubscribeMap.java:102)
W/System.err:     at rx.Subscriber.setProducer(Subscriber.java:205)
W/System.err:     at retrofit2.adapter.rxjava.RxJavaCallAdapterFactory$CallOnSubscribe.call(RxJavaCallAdapterFactory.java:152)
W/System.err:     at retrofit2.adapter.rxjava.RxJavaCallAdapterFactory$CallOnSubscribe.call(RxJavaCallAdapterFactory.java:138)
W/System.err:     at rx.internal.operators.OnSubscribeLift.call(OnSubscribeLift.java:48)
W/System.err:     at rx.internal.operators.OnSubscribeLift.call(OnSubscribeLift.java:30)
W/System.err:     at rx.Observable.unsafeSubscribe(Observable.java:10142)
W/System.err:     at rx.internal.operators.OnSubscribeMap.call(OnSubscribeMap.java:48)
W/System.err:     at rx.internal.operators.OnSubscribeMap.call(OnSubscribeMap.java:33)
W/System.err:     at rx.internal.operators.OnSubscribeLift.call(OnSubscribeLift.java:48)
W/System.err:     at rx.internal.operators.OnSubscribeLift.call(OnSubscribeLift.java:30)
W/System.err:     at rx.Observable.unsafeSubscribe(Observable.java:10142)
W/System.err:     at rx.internal.operators.OnSubscribeMap.call(OnSubscribeMap.java:48)
W/System.err:     at rx.internal.operators.OnSubscribeMap.call(OnSubscribeMap.java:33)
W/System.err:     at rx.internal.operators.OnSubscribeLift.call(OnSubscribeLift.java:48)
W/System.err:     at rx.internal.operators.OnSubscribeLift.call(OnSubscribeLift.java:30)
W/System.err:     at rx.Observable.unsafeSubscribe(Observable.java:10142)
W/System.err:     at rx.internal.operators.OperatorSubscribeOn$1.call(OperatorSubscribeOn.java:94)
W/System.err:     at rx.internal.schedulers.ScheduledAction.run(ScheduledAction.java:55)
W/System.err:     at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:457)
W/System.err:     at java.util.concurrent.FutureTask.run(FutureTask.java:266)
W/System.err:     at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:301)
W/System.err:     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1162)
W/System.err:     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:636)
W/System.err:     at java.lang.Thread.run(Thread.java:764)

Here is the json data from stetho:

{"id":47,"credit_card":"","order_ordereditem_set":[{"id":36,"product_item":{"id":3,"category":{"id":1,"name":"Fruit"},"added_time":"2018-02-11 15:23:21","upc":"4321","desc":"Mandarin","price":150,"img_url":"http://15.32.134.74:8000/media/images/products/mandarin.jpg","not_sale":false,"is_hot":false,"is_recommended":false},"added_time":"2018-02-12 14:02:11","quantity":1,"order_id":47},{"id":37,"product_item":{"id":2,"category":{"id":1,"name":"Fruit"},"added_time":"2018-02-08 13:29:07","upc":"123456","desc":"Kiwi","price":500,"img_url":"http://15.32.134.74:8000/media/images/products/kiwi.jpg","not_sale":false,"is_hot":true,"is_recommended":false},"added_time":"2018-02-12 14:02:11","quantity":1,"order_id":47}],"added_time":"2018-02-12 14:02:11","payment":"0","total_quantity":2,"total_price":6.5,"user":1

But i checked wireshark, there is no missing of right brace. I also used postman, but there is no problem.

This issue comes out 30%.

Following is my code details:

private HttpUtil() {
        Gson gson = new GsonBuilder()
                .setDateFormat("yyyy-MM-dd HH:mm:ss")
                .create();

        Retrofit retrofit = new Retrofit.Builder()
                .addConverterFactory(GsonConverterFactory.create(gson))
                .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
                .client(getOkHttpClient())
                .baseUrl(SERVER_BASE_URL)
                .build();
        mService = retrofit.create(RestAPIService.class);
    }

    static public HttpUtil getHttpUtilInstance() {
        if (mHttpUtil == null) {
            synchronized (HttpUtil.class) {
                mHttpUtil = new HttpUtil();
            }
        }
        return mHttpUtil;
    }

    public RestAPIService getService() {
        return mService;
    }

    private OkHttpClient getOkHttpClient() {
        HttpLoggingInterceptor loggingInterceptor = new HttpLoggingInterceptor();

        if (GlobalConfig.CONFIG_DEBUG)
            loggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
        else
            loggingInterceptor.setLevel(HttpLoggingInterceptor.Level.NONE);

        OkHttpClient.Builder httpClientBuilder = new OkHttpClient
                .Builder();
        httpClientBuilder.addInterceptor(loggingInterceptor)
                .addInterceptor(mTokenInterceptor)
                .addNetworkInterceptor(new StethoInterceptor());

        //httpClientBuilder.addNetworkInterceptor(loggingInterceptor);
        return httpClientBuilder.build();
    }

    private boolean shouldAddToken(String url) {
        return !url.contains("api/login");
    }

    private boolean alreadyHasAuthorizationHeader(Request request) {
        return request.header("Authorization") != null;
    }

    // Interceptor used for inserting token into Header
    private Interceptor mTokenInterceptor = new Interceptor() {
        @Override
        public Response intercept(Chain chain) throws IOException {
            Request request;
            Request originalRequest = chain.request();

            //request = originalRequest.newBuilder().addHeader("Connection", "close").build();
            request = originalRequest;
            String token = UserDataRepository.getInstance().getToken();
            if (token == null || token.length() == 0 || alreadyHasAuthorizationHeader(originalRequest)) {
                return chain.proceed(originalRequest);
            }

            if (shouldAddToken(originalRequest.url().toString())) {
                request = request.newBuilder()
                        .header("Authorization", "JWT " + token)
                        .build();
            }
            return chain.proceed(request);
        }
    };




  @Headers({"Content-Type: application/json", "Accept: application/json"})
    @GET("api/order")
    Observable<List<Order>> getOrder();
Jone Yan
  • 11
  • 4
  • Check if the string is chunked when it is returned. If this is the problem, this one missing last character could be because you reached the chunk size. Because there seems to be something like this: https://github.com/square/retrofit/issues/1315 – Erhard Dinhobl Feb 12 '18 at 07:15
  • check your json is correct format – sasikumar Feb 12 '18 at 07:17
  • https://stackoverflow.com/a/28590064/7557205 So logging might be a problem – Aswin P Ashok Feb 12 '18 at 07:24
  • do like this -https://stackoverflow.com/questions/8888654/android-set-max-length-of-logcat-messages\ – Adil Feb 12 '18 at 07:54
  • @Erhard Dinhobl i checked wireshark log, chunked is not used in the HTTP msg – Jone Yan Feb 13 '18 at 02:18
  • Did you find the cause of your problem? I have the exact problem using retrofit2. Sometimes the closing brace is missing causing the error, but when sniffing with wireshark it is there all the time. – Nathan Q Nov 04 '19 at 15:54
  • @JoneYan so it seems i have the exact problem as you had – Nathan Q Nov 06 '19 at 22:53

1 Answers1

0

it's a bit late for the answer, but if someone has the same problem, I'll tell you how I solved it.

I was having the same error, the last character of the response was missing, but when testing the api with postman everything was fine, so I try the same code with other apis and it works.

The problem is the server, I was using laravel server so I installed and configured apache and the problem is solved.

bayu
  • 1
  • 1