无法连接到 localhost/127.0.0.1 android

Kev*_*tel 10 android androidhttpclient retrofit

我是 android 开发的新手,并试图通过改造库在 android 中调用本地 .NET web api 服务。在 IIS 上启动我的 web api 后,我收到此错误无法连接到 localhost/127.0.0.1 android。

当我按照建议的http://themakeinfo.com/2015/04/retrofit-android-tutorial/做同样的事情时,它工作正常,但我的本地主机服务没有从 android 调用

我的服务网址是, 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<gitmodel> 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<gitmodel>() {
            @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();
            }
        });
}
Run Code Online (Sandbox Code Playgroud)

但没有运气。

请告诉我我需要在哪里更改才能使其正常工作。?

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

<ArrayOfstudents xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://schemas.datacontract.org/2004/07/APICall.Controllers">
<students>
<studentAddress>valsad</studentAddress>
<studentId>1</studentId>
<studentName>Keval</studentName>
</students>
<students>
<studentAddress>Hyderabad</studentAddress>
<studentId>2</studentId>
<studentName>Honey</studentName>
</students>
</ArrayOfstudents>
Run Code Online (Sandbox Code Playgroud)

Dew*_*rld 13

而不是使用127.0.0.1:<PORT>使用您的本地 IP

  • http://192.168.1.5:3000 这样 (2认同)

Xia*_*zou 6

您的情况与改造库无关。

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

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

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

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

  • 浏览器中的工作网址是http://localhost:52511/api/Values/GetProduct/5,我的IPV4地址是:196.168.1.2。所以,我使用的网址:http://192.168.1.2:52511/api/Values/GetProduct/5 但它显示错误的请求。 (4认同)

ahu*_*kyi 5

我遇到了和你一样的问题(无法连接到 localhost/127.0.0.1)。 \n首先我建议你阅读以下内容:\n Android 开发人员指南(图像的主要部分\xe2\x86 \x93)\n在此输入图像描述

\n\n

我拥有所有这些,即 A、B 和 C。作为 C(模拟器),我使用了 Genymotion。\n我通过更改网络配置解决了我的问题解决了我的问题。

\n\n

重要提示:我在 baseURL 中的地址是:“ localhost:8099/

\n\n

让我们看看:\n你应该点击设置\xe2\x86\x92网络\xe2\x86\x92检查使用HTTP代理\xe2\x86\x92输入你的IP地址和端口(在我的情况下是8099) \xe2\x86\x92 关闭。在图像 \xe2\x86\x93 上查看更多详细信息

\n\n

在此输入图像描述

\n\n

在此输入图像描述

\n