Alex Wesley
Alex Wesley

Reputation: 63

Retrofit call always goes to onFailure

I am trying to get information from a backend service and I am using Retrofit to get the response.

Here's my retrofit singleton class, i used a random api for an example. I use my own when i start the service.

import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class BaseRetrofit {
    private static Retrofit retrofitInstance = null;
    private BaseRetrofit() {};

    public static Retrofit getRetrofitInstance() {

        if (retrofitInstance == null) {
            retrofitInstance = new Retrofit.Builder()
                    .baseUrl("http://192.168.1.155:5000")
                    .addConverterFactory(GsonConverterFactory.create())
                    .build();
        }

        return retrofitInstance;
    }
}

In my main class i initialize the retrofit and make the call.

EndPoints myEndPoints = BaseRetrofit.getRetrofitInstance().create(EndPoints.class);
Call<List<JobItem>> jobs = myEndPoints.getJobs();

jobs.enqueue(new Callback<List<JobItem>>() {
    @Override
    public void onResponse(Call<List<JobItem>> call, Response<List<JobItem>> response) {
        Log.d("SUCCESS", "LOADED JSON " + response.body().get(0).getJobType());
    }

    @Override
    public void onFailure(Call<List<JobItem>> call, Throwable t) {
        Log.d("ERROR", "ERROR LOADING JSON");
    }
});

This is my EndPoints interface.

import java.util.List;

import retrofit2.Call;
import retrofit2.http.GET;

public interface EndPoints {

    @GET("/getjobs/hardware")
    Call<List<JobItem>> getJobs();
}

The URL that is needed after the backend service is started is http://my_ip_address:5000/getjobs/hardware for example http://192.168.1.155:5000/getjobs/hardware and this is what the JSON is

[
    {
        "date": "2021-05-22T23:45:27.8696343+02:00",
        "host": "172.217.169.164",
        "count": 3,
        "packetSize": 100,
        "jobPeriod": 120,
        "jobType": "PING"
    }
]

I can't figure out why the call always goes to onFailure

Upvotes: 1

Views: 2132

Answers (1)

arun
arun

Reputation: 368

If the service which the app is calling is running on your local machine (or local host or 127.0.0.1) then you need to use 10.0.2.2 as the IP address if you are using an emulator to call it. So your URL would look like http://10.0.2.2:5000

Also, since the protocol is http you would need to add allow clear text in manifest. For this the best way is to create an xml directory in your res directory and add a network-security-config.xml file as below:

<?xml version="1.0" encoding="utf-8"?>
<network-security-config>
    <domain-config cleartextTrafficPermitted="true">
        <domain includeSubdomains="true">10.0.2.2</domain>
    </domain-config>
</network-security-config>

Then, add it to your manifest as below:

<application
....
android:networkSecurityConfig="@xml/network_security_config">
.....
.....
</application>

Also try to print out the throwable exception to know more if the error happens again.

Happy coding!

Upvotes: 5

Related Questions