The enqueue call is made on a background thread and after that onResponse is not triggered.
but okHttp shows the request is completed successfully.
call.enqueue(new Callback<Settings>() {
@Override
public void onResponse(Response<Settings> response, Retrofit retrofit) {
Log.d(LOG_TAG, "Is response success? " + response.isSuccess());
if (response.isSuccess()) {
...
}
}
@Override
public void onFailure(Throwable t) {
}
});
Stack used:
retrofit:converter-gson:2.0.0-beta2
retrofit:retrofit:2.0.0-beta2
okhttp:logging-interceptor:2.7.0
Any ideas?
Failure is being called?
On Sat, Dec 26, 2015, 8:16 PM Gaurav Chaudhary [email protected]
wrote:
The enqueue call is made on a background thread and after that onResponse
is not triggered
but okHttp shows the request is completed successfullycallenqueue(new Callback
() {
@Override
public void onResponse(Responseresponse, Retrofit retrofit) {
Logd(LOG_TAG, "Is response success? " + responseisSuccess());
if (responseisSuccess()) {} } @Override public void onFailure(Throwable t) { } });Any ideas?
—
Reply to this email directly or view it on GitHub
https://github.com/square/retrofit/issues/1410.
I actually have this EXACT same problem. And to answer your question @JakeWharton, onFailure IS called and onResponse is not. Let me know if you want any more information.
Api.uploadImage(response.body().getUrl(), media).enqueue(new Callback<Response>() {
@Override
public void onResponse(retrofit.Response<Response> response, Retrofit retrofit) {
Toast.makeText(getContext(), "Success", Toast.LENGTH_SHORT).show();
}
@Override
public void onFailure(Throwable t) {
Toast.makeText(getContext(), "Failure", Toast.LENGTH_SHORT).show();
}
});
But the OKHttp logger returns the following:
12-28 21:14:52.778 22752-23518/com.lifeisgreaterthan.checkblocks D/OkHttp: <-- HTTP/1.1 200 (521ms)
12-28 21:14:52.778 22752-23518/com.lifeisgreaterthan.checkblocks D/OkHttp: OkHttp-Selected-Protocol: h2
12-28 21:14:52.778 22752-23518/com.lifeisgreaterthan.checkblocks D/OkHttp: x-guploader-uploadid: AEnB2UprsshsKClNDTy43w-gfdc_sLfQZoBUPEuN64qCLpAYZyCjZQNxgbPonbr_idJj87pjF7Zqy3DAbPzGVIhieSxg-4Guiw
12-28 21:14:52.778 22752-23518/com.lifeisgreaterthan.checkblocks D/OkHttp: x-appengine-estimated-cpm-us-dollars: $0.000004
12-28 21:14:52.778 22752-23518/com.lifeisgreaterthan.checkblocks D/OkHttp: x-appengine-resource-usage: ms=6 cpu_ms=37
12-28 21:14:52.778 22752-23518/com.lifeisgreaterthan.checkblocks D/OkHttp: date: Tue, 29 Dec 2015 02:14:53 GMT
12-28 21:14:52.778 22752-23518/com.lifeisgreaterthan.checkblocks D/OkHttp: pragma: no-cache
12-28 21:14:52.778 22752-23518/com.lifeisgreaterthan.checkblocks D/OkHttp: expires: Fri, 01 Jan 1990 00:00:00 GMT
12-28 21:14:52.778 22752-23518/com.lifeisgreaterthan.checkblocks D/OkHttp: cache-control: no-cache, must-revalidate
12-28 21:14:52.778 22752-23518/com.lifeisgreaterthan.checkblocks D/OkHttp: content-length: 36
12-28 21:14:52.778 22752-23518/com.lifeisgreaterthan.checkblocks D/OkHttp: server: UploadServer
12-28 21:14:52.778 22752-23518/com.lifeisgreaterthan.checkblocks D/OkHttp: content-type: text/html; charset=UTF-8
12-28 21:14:52.778 22752-23518/com.lifeisgreaterthan.checkblocks D/OkHttp: alternate-protocol: 443:quic,p=1
12-28 21:14:52.778 22752-23518/com.lifeisgreaterthan.checkblocks D/OkHttp: alt-svc: quic=":443"; ma=604800; v="30,29,28,27,26,25"
12-28 21:14:52.778 22752-23518/com.lifeisgreaterthan.checkblocks D/OkHttp: OkHttp-Sent-Millis: 1451355292256
12-28 21:14:52.779 22752-23518/com.lifeisgreaterthan.checkblocks D/OkHttp: OkHttp-Received-Millis: 1451355292777
12-28 21:14:52.779 22752-23518/com.lifeisgreaterthan.checkblocks D/OkHttp: Uploaded Media! Blobs uploaded: file
12-28 21:14:52.779 22752-23518/com.lifeisgreaterthan.checkblocks D/OkHttp: <-- END HTTP (36-byte body)
Also, I can validate that it IS successful.
What's the exception?
@JakeWharton
D/RETROFIT: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was STRING at line 1 column 1 path $
Your model object does not match the response and Gson is throwing an error. Looks like you want ResponseBody not Response as the response parameter type.
@JakeWharton Sure enough! That fixed it. I am trying to find the answer on my own, but I am having trouble. If you have the time, can you explain how I know whether I should use Response or ResponseBody? From what I understand, Response is the raw HTTP response that should be used when you don't have a specific object to map to.
@danmikita It's ResponseBody that you want for that case. Retrofit will always provide a Response<T> object with metadata about the request. Only the format of the body must be specified (whether known or unknown).
I also created https://github.com/square/retrofit/pull/1418 to hopefully prevent others from falling into this trap.
Callback is not triggered where I can see the response by using HttpLoggingInterceptor and my scenario is
When a fragment is resumed from backstack and I call a service then sometimes callback is not triggered in Retrofit 2.1.0 and okhttp:3.0.1 otherwise all the model classes are fine and its working fine.
Hi,
I am facing a exact issue with POST request. onResponse method is getting called on failure (i.e., response status != 200), onResponse method is not called on success(i.e., response status = 200). onFailure method is never called on either success or failure.
compile 'com.squareup.retrofit2:retrofit:2.1.0'
compile 'com.squareup.okhttp:okhttp:2.4.0'
compile 'com.google.code.gson:gson:1.7.2'
compile 'com.squareup.retrofit2:converter-gson:2.0.0'
compile 'com.squareup.okhttp3:logging-interceptor:3.3.1'
final Retrofit retrofit = new Retrofit.Builder()
.baseUrl("http://test.ngrok.io/api/")
.addConverterFactory(GsonConverterFactory.create())
.client(httpClient.build())
.build();
Api.Business business = retrofit.create(Api.Business.class);
Call<ResponseBody> call = business.createAccount(reg);
call.enqueue(new Callback<ResponseBody>() {
@Override
public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
try {
if (response.isSuccessful()) {
// dont work
Log.d("API success", response.message());
} else {
// works on failure
Log.d("API failure", response.message());
Converter<okhttp3.ResponseBody, APIError> converter = retrofit
.responseBodyConverter(APIError.class, new Annotation[0]);
APIError error = null;
try {
error = converter.convert(response.errorBody());
Toast.makeText(getApplicationContext(), error.getFailure().toString(), Toast.LENGTH_LONG);
} catch (IOException e) {
}
}
} catch (Exception e) {
Log.e("API : Exception", "Exception");
e.printStackTrace();
}
}
@Override
public void onFailure(Call<ResponseBody> call, Throwable t) {
}
});
}
Can you help with this.
Thanks,
Aashish
Hello, I didn't find any particular solution of this problem but I had following scenario which was blocking response.
When I resumed to previous fragment then I almost hit 2 services at same time then I didn't receive any response then I hold a service until other service response is received hence my problem solved.
@farazahmed I am using only one service.
Actually I don't know how to produce it in your case but I am using following configurations.
compile 'com.squareup.retrofit2:retrofit:2.1.0'
compile 'com.squareup.retrofit:converter-gson:2.0.0-beta2'
compile 'com.squareup.okhttp3:okhttp:3.0.1'
compile 'com.squareup.okhttp:logging-interceptor:2.6.0'
@JakeWharton Hi, you wrote 29.12.15 about response and responseBody, i have the same problem and actually i don't understand what i should do. Can you explain or make an example that would be work?
JakeWharton commented on 29 Dec 2015
Your model object does not match the response and Gson is throwing an error. Looks like you want ResponseBody not Response as the response parameter type.
@NosovPavel
Leaving this for other people.
Change your signature for:
public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response)
Most helpful comment
Your model object does not match the response and Gson is throwing an error. Looks like you want
ResponseBodynotResponseas the response parameter type.