Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/213.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
无法连接到localhost/127.0.0.1 android_Android_Retrofit_Androidhttpclient - Fatal编程技术网

无法连接到localhost/127.0.0.1 android

无法连接到localhost/127.0.0.1 android,android,retrofit,androidhttpclient,Android,Retrofit,Androidhttpclient,我是android开发新手,尝试通过改造库在android中调用本地.NETWebAPI服务。在IIS上启动web api后,我发现此错误,无法连接到localhost/127.0.0.1 android 当我按照建议做同样的事情时,它工作得很好,但我的本地主机服务并没有从android上调用 我的服务url是, 它也在浏览器中以XML格式输出 我也试着用 public interface gitapi { @GET("/api/Values/GetProduct/{id}")

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

当我按照建议做同样的事情时,它工作得很好,但我的本地主机服务并没有从android上调用

我的服务url是,

它也在浏览器中以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();
            }
        });
}
公共接口gitapi{ @GET(“/api/Values/GetProduct/{id}”)//这是另一个url部分。最好的方法是开始使用/ public void getFeed(@Path(“id”)int-id,回调响应); } 公共类模型{ 公共int学生; 公共字符串studentName; 公共字符串学生地址; } 字符串API=”http://10.0.2.2:52511"; 公共void调用服务(视图){ RestAdapter RestAdapter=new RestAdapter.Builder().setEndpoint(API.build(); gitapi git=restAdapter.create(gitapi.class); int-id=5; getFeed(id,new Callback()){ @凌驾 public void成功(gitmodel,gitmodel,Response-Response){ Toast.makeText(getApplicationContext(),“Success”,Toast.LENGTH_LONG.show(); } @凌驾 公共无效失败(错误){ Toast.makeText(getApplicationContext(),“Errors”,Toast.LENGTH_LONG.show(); } }); } 但是没有运气

请告诉我需要在哪里换车才能正常工作

我在浏览器中得到的回应是

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

瓦尔萨德
1.
基瓦尔
海得拉巴
2.
霍尼

在您的案例中,与改装库无关

这是关于你的网络设置, 您的手机和IIS服务器必须是同一局域网

您可以按照下面的步骤操作

  • 在IIS服务器上启动AP
  • 将AP与手机连接

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

    我遇到了与您相同的问题(无法连接到localhost/127.0.0.1)。 首先,我建议您阅读以下内容: (这幅图的主要部分↓)

    我有所有这些,是指A、B和C。作为C(仿真器),我使用了Genymotion。 我通过改变网络配置解决了我的问题

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

    让我们看看: 你应该点击设置→ 网络→ 选中“使用HTTP代理”→ 输入您的IP地址和端口(在我的情况下是8099)→ 关闭。查看有关图像的更多详细信息↓


    您可以在
    androidmanifest.xml

    android:usesCleartextTraffic="true"
    

    不要使用
    127.0.0.1:
    使用本地IP

    实际上,在同一台Android设备(Android 9)上使用两个应用程序时,这会有所帮助


    您的Android设备将无法连接到localhost。您需要使用可通过LAN访问的ip。请确保您可以从手机上的浏览器访问该地址。感谢回复,请推荐任何有关在设备上访问localhost和其他配置的文章。您使用的是
    10.0.2.2
    。这将打开如果你的应用程序在模拟器上运行,则无法正常工作。你没有告诉你的应用程序在何处运行。真实设备?是的,我想在真实设备上运行应用程序。但在模拟器上也出现了错误消息,15000毫秒后无法连接到/10.0.2.2(端口52511)。对于真实设备,我将关注本文“”,但并没有运气,直到我还并没有解决这个问题,但答案告诉我正确的方法:)浏览器中的工作url是,我的IPV4地址是:196.168.1.2。所以,我使用url:但它显示了错误的请求。这样地
    android:usesCleartextTraffic="true"