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) {
}
});
Related
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()); //
}
});
}
}
I'm using Retrofit 2.0, the code below will log:
"insertListingImages, SocketOutOfTimeException:TimeOut timeout".
Why is it causing a timeout exception? how do I resolve it? The odd thing is that if the retrofit call, hi() isn't inside the retrofit call, insertListingImages() everything would work fine.
retrofit = new Retrofit.Builder()
.baseUrl("http://IP_ADRESS/")
.addConverterFactory(GsonConverterFactory.create())
.build().create(DatabaseInterface.class);
retrofit.insertListingImages(imageListingRequest).enqueue(new Callback<Void>() {
#Override
public void onResponse(Call<Void> call, Response<Void> response) {
Log.d("insertListingImages", "Success");
retrofit.hi().enqueue(new Callback<Void>(){
#Override
public void onResponse(Call<Void> call, Response<Void> response){
Log.d("hiTest", "Success");
}
#Override
public void onFailure(Call<Void> call, Throwable t) {
Log.d("hiTest", "fail: " + t.toString() + " " + t.getMessage());
}
});
}
#Override
public void onFailure(Call<Void> call, Throwable t) {
Log.d("insertListingImages", "fail: " + t.toString() + " " + t.getMessage());
}
});
You can change the connection time out and read timeout by this code. you have to set the custom client for the retrofit
HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
OkHttpClient client = new OkHttpClient.Builder().connectTimeout(120, TimeUnit.SECONDS).readTimeout(120, TimeUnit.SECONDS).addInterceptor(interceptor).build();
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(Constants.url)
.addConverterFactory(GsonConverterFactory.create())
.client(client)
.build();
Here in the above code set the time out as 120 sec, you can change that according to your need
Needed to add below line in the build.gradle file
compile 'com.squareup.okhttp3:logging-interceptor:3.3.1'
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.
I have an issue trying to use Okhttp with retrofit. I seem not to understand what I am doing wrong.
It gives showing up error: 'Anonymous class derived from Callback' must either be declared abstract or implement abstract method 'onResponse(Response<T>, Retrofit)' in 'Callback'
In my MainActivity I have this:
OkHttpClient httpClient = new OkHttpClient();
httpClient.interceptors().add(new Interceptor() {
#Override
public Response intercept(Interceptor.Chain chain) throws IOException {
Request original = chain.request();
Request request = original.newBuilder()
.header("User-Agent", "Your-App-Name")
.header("Content-Type", "application/json")
.header("Authorization","authorization_code")
.method(original.method(), original.body())
.build();
return chain.proceed(request);
}
});
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.client(httpClient)
.build();
TestInterface service = retrofit.create(TestInterface.class);
Call<TestData> call = service.getPost();
/* It keeps pointing at this line below:
'Callback' must either be declared abstract" */
call.enqueue(new Callback<TestData>() {
#Override
public void onResponse(Response<TestData> response, Retrofit retrofit) {
// Get result Repo from response.body()
// response.isSuccess() is true if the response code is 2xx
int statusCode = response.code();
if (response.isSuccess()) {
System.out.println("Success: ");
} else {
}
}
#Override
public void onFailure(Throwable t) {
System.out.println("failed: "+t);
}
});
In my TestData Interface,I have this
public interface TestInterface {
#POST("/paths_to_web_directory")
Call<TestData>
getPost();
}
This is the way i see it done in other examples, So maybe i'm implementing it the wrong way. Kindly correct me. Thanks
The retrofit github project has a sample with a different method signature. Try this:
call.enqueue(new Callback<TestData>() {
#Override public void onResponse(Call<TestData> call, Response<TestData> response) {
}
#Override public void onFailure(Call<TestData> call, Throwable t) {
}
});
I have this Interface:
public interface InterfazAguaHttp {
#FormUrlEncoded
#POST("/")
Call<String> saveContador(#Field("contador") Long contador, Callback<String> callBack);
}
The rest of the code is this:
Retrofit builder = new Retrofit.Builder()
.baseUrl(ValoresGlobales.urlServlet)
.addConverterFactory(GsonConverterFactory.create())
.build();
InterfazAguaHttp interfaz = builder.create(InterfazAguaHttp.class);
Call<String> respuesta = interfaz.saveContador(93847597L, new Callback<String>() {
#Override
public void onResponse(Response<String> response, Retrofit retrofit) {
//Some logging
}
#Override
public void onFailure(Throwable t) {
//Some logging
}
});
This is all inside a try-catch block. In the catch, I am receiving this error:
Error: No Retrofit annotation found. (parameter #2) for method InterfazAguaHttp.saveContador
How could I get rid of this error, and still have my callback?
Thank you.
change your interface method to this
public interface InterfazAguaHttp {
#FormUrlEncoded
#POST("/")
Call<String> saveContador(#Field("contador") Long contador);
}
and the rest of the code like this
Retrofit builder = new Retrofit.Builder()
.baseUrl(ValoresGlobales.urlServlet)
.addConverterFactory(GsonConverterFactory.create())
.build();
InterfazAguaHttp interfaz = builder.create(InterfazAguaHttp.class);
Call<String> respuesta = interfaz.saveContador(93847597L);
respuesta.enqueue(new Callback<String>() {
#Override
public void onResponse(Response<String> response, Retrofit retrofit) {
//Some logging
}
#Override
public void onFailure(Throwable t) {
//Some logging
}
});
Link for reference