连接本地主机/127.0.0.1 Android 失败。

34 浏览
0 Comments

连接本地主机/127.0.0.1 Android 失败。

我是android开发新手,正在尝试通过retrofit库在android中调用本地的.NET web api服务。在我启动我的web api服务后,我在android上连接localhost/127.0.0.1时遇到了连接失败的错误。

当我像http://themakeinfo.com/2015/04/retrofit-android-tutorial/建议的那样做时,它可以正常工作,但我的localhost服务从android上没有被调用。

我的服务URL是,

http://localhost:52511/api/Values/getAllStudents/5

并且它在浏览器中也以XML格式给我输出。

我还尝试使用以下方式调用:

public interface gitapi {
    @GET("/api/Values/GetProduct/{id}")      //here is the other url part.best way is to start using /
    public void getFeed(@Path("id") int id, Callback response);
}
public class gitmodel {
    public int studentId;
    public String studentName;
    public String studentAddress;
}
String API = "http://10.0.2.2:52511";
public void CallService(View view){
        RestAdapter restAdapter = new RestAdapter.Builder().setEndpoint(API).build();
        gitapi git = restAdapter.create(gitapi.class);
        int id = 5;
        git.getFeed(id, new Callback() {
            @Override
            public void success(gitmodel gitmodel, Response response) {
                Toast.makeText(getApplicationContext(), "Success", Toast.LENGTH_LONG).show();
            }
            @Override
            public void failure(RetrofitError error) {
                Toast.makeText(getApplicationContext(), "Errors", Toast.LENGTH_LONG).show();
            }
        });
}

但没有成功。

请告诉我我需要在哪里更改以使其工作。

我在浏览器中收到的响应是,

valsad
1
Keval


Hyderabad
2
Honey


admin 更改状态以发布 2023年5月21日
0
0 Comments

在您的情况下,与Retrofit库无关。

这与您的网络设置有关,
您的手机和IIS服务器必须在同一局域网中。

您可以按以下方式进行操作。

  1. 在您的IIS服务器上启动一个AP;
  2. 将AP与您的手机连接。

有时您需要关闭IIS服务器上的安全防火墙。

0
0 Comments

不要使用127.0.0.1:,而是使用本地IP地址

0