Retrofit 2 Error: java.net.SocketTimeoutException: failed to connect to /192.168.86.1 (port 8080) after 10000ms
Asked Answered
D

1

6

I'm trying to make my App connect to a local web service using Retrofit 2 but i'm always getting this error. I'm sure the web service is responding because i'm using a tool in firefox that make the @GET request and the return is OK, returns me the correct JSON.

In android it doesn't even connect.

This is my MainActivity:

public class MainActivity extends AppCompatActivity {

    private String API_BASE_URL="http://192.168.1.32:8080/economarket-restService/resources/androidTest/";           

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        OkHttpClient.Builder httpClient = new OkHttpClient.Builder();            

        Retrofit.Builder builder = new Retrofit.Builder()
                .baseUrl(API_BASE_URL)
                .addConverterFactory(GsonConverterFactory.create());

        Retrofit retrofit = builder.client(
                httpClient.build()
        ).build();

        ContatoService contato = retrofit.create(ContatoService.class); 

        Call<Contato> repos = contato.listRespos(); //EconomarketService                 

        repos.enqueue(new Callback<Contato>() {
            @Override
            public void onResponse(Call<Contato> call, Response<Contato> response) {
                Contato contato = response.body();
                Toast.makeText(getBaseContext(), "Return" + contato.getName(), Toast.LENGTH_LONG).show();
                Log.d("Retorno",response.toString());
            }

            @Override
            public void onFailure(Call<Contato> call, Throwable t) {
                Toast.makeText(getBaseContext(), "Return" + t.toString(), Toast.LENGTH_LONG).show();
                Log.d("Retorno",t.toString());
            }
        });                    
    }        
}

My Interface:

public interface ContatoService {
    @GET("retorna/")
    Call<Contato>  listRespos();
}

Model classes (Contato):

public class Contato  {

    @SerializedName("name")
    private String name;

    @SerializedName("phone")
    private int phone;

    @SerializedName("likes")
    private int likes;

    @SerializedName("location")
    private Location location;


    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getPhone() {
        return phone;
    }

    public void setPhone(int phone) {
        this.phone = phone;
    }

    public int getLikes() {
        return likes;
    }

    public void setLikes(int likes) {
        this.likes = likes;
    }



    public Location getLocation() {
        return location;
    }

    public void setLocation(Location location) {
        this.location = location;
    }
}

Model Class (Location):

public class Location{

}
Danika answered 18/6, 2017 at 16:48 Comment(6)
Use the phone browser, not a Firefox tool on the server itself. If you are unable to connect, it's a firewall problemGaut
Ok, I did it. I've tested on phone browser and it's also returned the json correctly.Danika
In the title of your post... failed to connect to /192.168.86.1. That is not the same URL shown in your codeGaut
Yes, in my IPV4 config on windows I have both ip's number's 192.168.86.1 and 192.168.1.32. Both works to get the JSON in web browser. I've changed only to test. Any other ideia?Danika
Those are different network interfaces entirely. I don't know what to tell you if the phone browser works and Retrofit doesn't connect. Did you remember to allow internet permissions in your app? Can you please add the full logcat to the post?Gaut
Problem solved! The problem was the API_BASE_URL, thi url should have only "http://192.168.1.32:8080/" and the rest of the url should be on the interface: @GET("economarket-restService/resources/androidTest/retorna/"). I'm thankful for support.Danika
D
1

Problem solved! The problem was the API_BASE_URL, the url should have only:

http://192.168.1.32:8080/

And the rest of the url should be on the interface:

@GET("economarket-restService/resources/androidTest/retorna/‌​").

It all boils down to declare the root of the URL in the URL_BASE and the url directory access must be on the interface.

Danika answered 19/6, 2017 at 11:1 Comment(1)
The @GET is simply appended to the base url, so I'm surprised this is the fixGaut

© 2022 - 2024 — McMap. All rights reserved.