问题
I am using intentService for making http calls with Ion. On Wifi it works well but when I switch to data network things start breaking.
Ion.with(Application.getApplicationContext())
.load(url)
.as(new TypeToken<Object>() {})
.withResponse()
.setCallback(new FutureCallback<Response<Object>>() {
@Override
public void onCompleted(Exception e, Response<Object> response) {
if(e == null) {
Bundle bundle = new Bundle();
bundle.putSerializable("result", (Serializable) response.getResult());
rec.send(response.getHeaders().code(), bundle);
}
}
});
Above is the code which i am using in IntentService I am getting "java.io.IOException:non 2xx status line:HTTP/1.1 500 internal server error."
回答1:
Since you are using an intent service, as soon the onHandleIntent method completes, the service will terminate. Ion's request will detect the service context is terminated and cancel the request.
However, it seems you are using the main application context, rather than the service context, in which case it should work. In any case, you are getting a correct response (500 is still successful request and response). The 500 indicates the issue is server side.
来源:https://stackoverflow.com/questions/33569473/making-http-call-using-ion-with-in-intent-service-gives-error-on-data-network