android - 无法连接到 localhost/127.0.0.1 android

标签 android retrofit androidhttpclient

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

但没有运气。

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

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

<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

关于android - 无法连接到 localhost/127.0.0.1 android,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31623872/

相关文章:

android - 如何使用从一个 Activity 到另一个 Activity 的变量

java - 无法在 BlackBerry Playbook 上设置音量

android - javax.net.ssl.SSLPeerUnverifiedException : No peer certificate on HttpClient but works fine with HttpUrlConnection

android - 手机关机期间的 HTTP 调用

Android:无法取消异步任务

android - 如何使用 Twitter 4j 检索 Twitter 用户名

java - if 和 else 语句

android - 2.3.4 上的授权 header 收到 400 错误请求

java - For Loop 遍历数组中的 JSON 对象

android - 使用 Retrofit 的 API 响应对于某些不为 null 的参数返回 null