改造 2 错误:java.net.SocketTimeoutException:10000 毫秒后无法连接到/192.168.86.1(端口 8080) [英] Retrofit 2 Error: java.net.SocketTimeoutException: failed to connect to /192.168.86.1 (port 8080) after 10000ms

查看:159
本文介绍了改造 2 错误:java.net.SocketTimeoutException:10000 毫秒后无法连接到/192.168.86.1(端口 8080)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 Retrofit 2 使我的应用程序连接到本地 Web 服务,但我总是收到此错误.我确定 Web 服务正在响应,因为我在 firefox 中使用了一个工具来发出 @GET 请求并且返回正常,返回正确的 JSON.

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.

这是我的主要活动:

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());
            }
        });                    
    }        
}

我的界面:

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

模型类(Contato):

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;
    }
}

模型类(位置):

public class Location{

}

推荐答案

问题解决了!问题是 API_BASE_URL,url 应该只有:

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

http://192.168.1.32:8080/

其余的 url 应该在界面上:

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

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

归结起来就是在URL_BASE中声明了URL的根,并且url目录访问必须在接口上.

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.

这篇关于改造 2 错误:java.net.SocketTimeoutException:10000 毫秒后无法连接到/192.168.86.1(端口 8080)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆