我是 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
我是一名优秀的程序员,十分优秀!