moumenShobakey
moumenShobakey

Reputation: 426

Why am i getting " org.json.JSONException: End of input at character 0 of" although the string isn't empty?

I am getting response of a server, when i log the response body i get the access_token right, but when i try to convert it to a jsonObject it says the string is empty!

   apiInterface.Login(Email,Pass).enqueue(new Callback<ResponseBody>() {
        @Override
        public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
            if(response.isSuccessful()){

                try {
                    Log.d("PONSEMONSE",response.body().string());
                    JSONObject jsonObject = new JSONObject(response.body().string());
                    Log.d("LONO",jsonObject.getString("access_token"));
                } catch (IOException | JSONException e) {
                    e.printStackTrace();
                }
                
              /*  Intent intent = new Intent(LoginActivity.this,MainActivity.class);
                startActivity(intent);*/

            }
        }

The first log output ("PONSEMONSE"):

04-15 14:25:55.019 14235-14235/com.example.lambdaroom D/PONSEMONSE: {"access_token":"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJlbWFpbCI6InNodWJha3kuMDA3QHlhaG9vLmNvbSIsInN1YiI6Mywicm9sZSI6IlNUVURFTlQiLCJ2ZXJpZmllZCI6ZmFsc2UsImlhdCI6MTYxODQ4OTU1NiwiZXhwIjoxNjE4NTc1OTU2fQ.bZT-GOQiixLH0ZqdmRnh1khU6JMfp41RvjW0YrjK9IY"}

the error:

04-15 14:25:55.029 14235-14235/com.example.lambdaroom W/System.err: org.json.JSONException: 
End of input at character 0 of 
04-15 14:25:55.029 14235-14235/com.example.lambdaroom W/System.err:     at 
org.json.JSONTokener.syntaxError(JSONTokener.java:449)
  04-15 14:25:55.029 14235-14235/com.example.lambdaroom W/System.err:     at 
org.json.JSONTokener.nextValue(JSONTokener.java:97)
04-15 14:25:55.029 14235-14235/com.example.lambdaroom W/System.err:     at 
org.json.JSONObject.<init>(JSONObject.java:156)
04-15 14:25:55.029 14235-14235/com.example.lambdaroom W/System.err:     at 
org.json.JSONObject.<init>(JSONObject.java:173)
04-15 14:25:55.029 14235-14235/com.example.lambdaroom W/System.err:     at 
com.example.lambdaroom.LoginActivity$2.onResponse(LoginActivity.java:90)
04-15 14:25:55.029 14235-14235/com.example.lambdaroom W/System.err:     at    
retrofit2.ExecutorCallAdapterFactory$ExecutorCallbackCall$1$1.
run(ExecutorCallAdapterFactory.java:68)
04-15 14:25:55.029 14235-14235/com.example.lambdaroom W/System.err:     at 
android.os.Handler.handleCallback(Handler.java:739)
04-15 14:25:55.029 14235-14235/com.example.lambdaroom W/System.err:     at 
android.os.Handler.dispatchMessage(Handler.java:95)
04-15 14:25:55.029 14235-14235/com.example.lambdaroom W/System.err:     at 
android.os.Looper.loop(Looper.java:148)
04-15 14:25:55.029 14235-14235/com.example.lambdaroom W/System.err:     at 
android.app.ActivityThread.main(ActivityThread.java:7223)
04-15 14:25:55.029 14235-14235/com.example.lambdaroom W/System.err:     at 
java.lang.reflect.Method.invoke(Native Method)
04-15 14:25:55.029 14235-14235/com.example.lambdaroom W/System.err:     at 
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
04-15 14:25:55.029 14235-14235/com.example.lambdaroom W/System.err:     at 
com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)

Upvotes: 0

Views: 211

Answers (2)

Stephen C
Stephen C

Reputation: 719239

You haven't included the import statement, so we can't be 100% sure. However it looks like the ResponseBody that you are using is actually okhttp.okhttp3.ResponseBody. If so, the okhttp documentation states that:

  • the body can only be consumed once, and
  • the string() method that consumes the body.

The reasoning behind this behavior is that it means that ResponseBody is not forced to buffer the entire response somewhere (e.g. in memory) just in case it is needed. Considering that the response could be extremely large, this is a good thing in general.

Reference:

Upvotes: 2

moumenShobakey
moumenShobakey

Reputation: 426

Looks like the reponse.body.string() can be called only once otherwise it's empty :/

Upvotes: 0

Related Questions