Retrofit call does executed but not trigger onResponse or onFailure - android

My call is executed but does not trigger the onResponse or onFailure method
The Json object that is returned has an image inside
This is the code in my interactor class
private ExamApi initiateRetrofit() {
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.build();
return retrofit.create(ExamApi.class);
}
public void getExamImage(String user, String token,String exId, String exDocId) {
if (examApi == null) initiateRetrofit();
Call<ResponseBody> call = examApi.getExamImage(user,token,exId,exDocId);
call.enqueue(new Callback<ResponseBody>() {
#Override
public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
listener.onImageSuccess(response.body());
}
#Override
public void onFailure(Call<ResponseBody> call, Throwable t) {
//TODO ONFAILURE
//listener.onImageFailure();
listener.onFailure();
}
});
}
this is my examApi
#GET("mobile/getExamImage")
Call<ResponseBody> getExamImage(
#Query("user") String username,
#Query("token") String token,
#Query("exid") String exId,
#Query("edid") String exDocId
);

It is happening because you are making an asynchronous call on the retrofit. You are trying to capture the data even before the network call happens. This gives you a null result. Try Broadcasting the result using broadcast receiver or third party library like EventBus which will make your result available after the network call has happened.

Related

retrofit call the second method when the first method is finished

I have two methods the first is named first() and the second is named second();
The first is a retrofit call :
private void first() {
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(apiConge.BASE_URL)
.addConverterFactory(ScalarsConverterFactory.create())
.build();
apiConge api = retrofit.create(apiConge.class);
Call<String> call = api.getCongeByUserId("Bearer " + token_data, id_data);
//finally performing the call
call.enqueue(new Callback<String>() { ...
And the second is also a retrofit call :
private void second() {
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(apiConge.BASE_URL)
.addConverterFactory(ScalarsConverterFactory.create())
.build();
apiConge api = retrofit.create(apiConge.class);
Call<String> call = api.getCongeByUserId("Bearer " + token_data, id_data);
//finally performing the call
call.enqueue(new Callback<String>() {
In my case i need to execute the second method when the first method call is completely finished.
You need to call second method when first call is done.
call.enqueue(new Callback<String>() {
#Override
public void onResponse(Response<String> response) {
second();
}
#Override
public void onFailure(Throwable t) {
}
});

I keep connection failed error in my "onFailure" method

I'm using retrofit object to call my API but I keep getting
"java.net.ConnectException: Failed to connect to
api.backtory.com/185.105.185.244:443"
in my onFailure method
I have set the title using #Headers
I have added INTERNET permission to my manifests
I have added the gson-converter library and retrofit as well
public class RegisterUserController {
public void start(User user){
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(ChatRoomAPI.BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.build();
ChatRoomAPI chatRoomAPI = retrofit.create(ChatRoomAPI.class);
Call<User> call = chatRoomAPI.registerUser(user);
call.enqueue(new Callback<User>() {
#Override
public void onResponse(Call<User> call, Response<User> response) {
Log.d("TAG" , "onResponse" + response.code());
}
#Override
public void onFailure(Call<User> call, Throwable t) {
Log.d("TAG" , "onFailure" + t.getCause()); //
}
});
}
}

Retrofit Can't return Data

i'm probebly doing something wrong,
i'm trying to figure out how to use retrofit, so for now i'm calling back just a general ResponseBody, and not yet parsing anything, (just a simple http get)
but retrofit can't get the data, what am i doing wrong ? >
my Retrofit API >
public interface retrofitApi {
String baseUrl = "http://localhost:3003/";
#GET("api/radBox/getDegrees")
Call<ResponseBody> getCallData();
class Factory {
private static retrofitApi service;
public static retrofitApi getInstance() {
if (service == null) {
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(baseUrl)
.addConverterFactory(GsonConverterFactory.create())
.build();
service = retrofit.create(retrofitApi.class);
return service;
} else {
return service;
}
}
}
}
and in my main Activity i put >
retrofitApi.Factory.getInstance().getCallData().enqueue(new Callback<ResponseBody>() {
#Override
public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
Log.d("myLogs", "log: " + response);
}
#Override
public void onFailure(Call<ResponseBody> call, Throwable t) {
Log.d("myLogs", "failed to Retrive Data");
}
});
I think your problem is caused by using "localhost". Looks like you are using your phone to connect to phone's 3003 port. Exchange the localhost to your Server IP to give a try.
I copy all your code in my retrofit project, I exchange the URL, everything is working well on my side, meaning your retrofit code has no problem.
Try use instead ResponseBody your model (for me it WeatherModel) which must will return from server.
Like this
String baseUrl = "http://api.openweathermap.org/";
#GET("data/2.5/weather")
Call<WeatherModel> getCallData(#Query("q") String q, #Query("lang") String lang,
#Query("appid") String appid);
and put in MainActivity like this
retrofitApi.Factory.getInstance()
.getCallData("Taganrog,ru", "ru", "bde41abf61e82b6209a544a5ea2ddb76")
.enqueue(new Callback<WeatherModel>() {
#Override
public void onResponse(Call<WeatherModel> call, Response<WeatherModel> response) {
Log.d("myLogs", "log: " + response);
}
#Override
public void onFailure(Call<WeatherModel> call, Throwable t) {
Log.d("myLogs", "log: " + "failed to Retrive Data");
}
});
It's works fine for me

Retrofit cancel request

I'm using Retrofit 2.0. To get some data from RESTFull services I use procedures like this:
public Call downloadUser() {
// Create RetrofitService
Call<User> call = service.getUser();
call.enqueue(new Callback<User>() {
#Override
public void onResponse(Response<User> response, Retrofit retrofit) {
// Do some operations with User obj if response.isSuccess()
}
#Override
public void onFailure(Throwable t) {
// Failure
}
});
return call;
}
In some cases, I need to cancel my request. I use call.cancel(), but even if I call this procedure Callback.onResponse(...) or Callback.onFailure(...) triggered anyway, so using Call.cancel() doesn't cancel my request and it keeps on going until failure or response.
To know if a call was canceled or if it was truly successful you'll need to two a few things.
First it looks like the version of Retrofit2 you are using needs to be updated
Second you can check if a call was canceled based on the code below. Note that this will deal with cancels from the Call<> or the Dispatcher.class in OKHttp3
#Override
public void onResponse(Response<User> response, Response response) {
if(response != null && response.isSuccessful()) {
//Do Stuff with the response
}
}
#Override
public void onFailure(Call<User> user, Throwable t) {
if(user.isCanceled() || "Canceled".equals(t.getMessage())) {
//Call was canceled
} else {
//Call failed
}
}

Retrofit2 POST request with Body

I need make POST request with parameters "guid=1" in Body. i use Retrofit2
I try :
#POST("/api/1/model")
Call<ApiModelJson> getPostClub(#Body User body);
User Class:
public class User {
#SerializedName("guid")
String guid;
public User(String guid ) {
this.guid = guid;
}
MailActivity:
User user =new User ("1");
Call<ApiModelJson> call = service.getPostClub(user);
call.enqueue(new Callback<ApiModelJson>() {
#Override
public void onResponse(Response<ApiModelJson> response) {
}
#Override
public void onFailure(Throwable t) {
dialog.dismiss();
}
How make this request?
you have to call call.enqueue, providing an instance of Callback< ApiModelJson>, where you will get the response. enqueue executes your backend call asynchronously. You can read more about call.enqueue here
With code below, you can make the request synchronously:
ApiModelJson responseBody = call.execute();
If you want it to be asynchronous:
call.enqueue(new Callback<ApiModelJson>() {
#Override
public void onResponse(Response<ApiModelJson> response, Retrofit retrofit) {
}
#Override
public void onFailure(Throwable t) {
}
});

Categories

Resources