Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Retrofit invokes failure() method even when the headers status code is 200

I am using retrofit for my backend communication and below it the snippet of my retrofit call:

serverObject.createEvent(Utils.getAuthHeader(), params, new Callback<CreateEventResponse>() {
        @Override
        public void success(CreateEventResponse outputObj, retrofit.client.Response response) {

            Log.d(TAG, outputObj.getTitle() + " is successfully created.");
            setResult(Activity.RESULT_OK);
            finish();
        }

        @Override
        public void failure(RetrofitError retrofitError) {

            //Header status code
            Log.e("failure", String.valueOf(retrofitError.getResponse().getStatus()));
            Log.e("failure", String.valueOf(retrofitError.getResponse().getBody()));

        }
    });

The above code prints this in the Logcat:

04-16 16:26:11.751  25131-25131/com.android.myapp.app E/failure﹕ 200
04-16 16:26:11.751  25131-25131/com.android.myapp.app E/failure﹕ null

who is this possible?

Can any body please help why is this happening.

Also I have set setLogLevel(RestAdapter.LogLevel.FULL); and so i can see every values in my logcat. My response is coming correct from the server but why is failure() getting called?

Please help!

Thanks in advance.

like image 306
AabidMulani Avatar asked Apr 16 '14 11:04

AabidMulani


1 Answers

Probably retrofit throws an exception which call failure method. Use:

retrofitError.getCause()

or make some debugging. You register callback with CreateEventResponse so when body is null you could have caught parse exception.

like image 160
piobab Avatar answered Oct 06 '22 01:10

piobab