本文介绍了无法连接到localhost/127.0.0.1 android的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Android开发的新手,正在尝试通过改造库在android中调用本地.NET Web API服务.在IIS上启动我的Web API后,出现此错误,无法连接到localhost/127.0.0.1 android.

I am new to android development, and trying to call local .NET web api service in android via retrofit library. After starting my web api on IIS I am getting this error failed to connect to localhost/127.0.0.1 android.

当我执行建议的相同操作时 http://themakeinfo.com/2015/04/retrofit-android-tutorial/,它工作正常,但是我的本地主机服务未从android调用

When I did same thing as suggested http://themakeinfo.com/2015/04/retrofit-android-tutorial/, It's working fine, But my localhost service is not calling up from android

我的服务网址是 http://localhost:52511/api/Values/getAllStudents/5

它也给了我浏览器XML格式的输出.

and it is giving me output in XML format in browser too.

我也尝试用它来称呼它,

I have also try to call it with,

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

但没有运气.

请告诉我在哪里需要更改以使其起作用.?

Please tell me where do I need to change to make it work. ?

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

Response I am getting in browser is,

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

推荐答案

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

Instead of using 127.0.0.1:<PORT> use your local IP

这篇关于无法连接到localhost/127.0.0.1 android的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-17 11:20
查看更多