我有一个ASP.NET MVC应用程序和一个带有get,put,update和delete的UsersController。 为了从我的互联网浏览器中调用它,我打电话:
http://localhost:46040/api/users
并以xml格式接收所有用户的列表。
我正在尝试使用简单的获取用户从我的Android应用程序访问它,没有任何运气。
我做错了什么?
从设备调试运行时我要调用什么IP?
如何激活" api / users"?
这是我的服务器UsersController获取代码:
public IEnumerable<UserModel> Get()
{
IQueryable<User> query;
IEnumerable<UserModel> results;
try
{
query = TheRepository.GetAllUsers();
results = query.ToList().Select(s => TheModelFactory.CreateModel(s));
}
catch (Exception ex)
{
Debug.WriteLine("Exception has been caught: " + ex.Message);
throw;
}
return results;
}
这是我的Android代码尝试:
//private final String url = "http://localhost:46040/Api/Users";//tried this from emulator and device, does not work
private final String url = "http://10.0.2.2:46040/Api/Users";//tried this from emulator, does not work
public void GetAllUsers()
{
Thread t = new Thread(){
@Override
public void run() {
HttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
HttpResponse resp = null;
try {
resp = httpClient.execute(httpPost);
HttpEntity entity = resp.getEntity();
String content = entity.getContent().toString();
Log.e("content", content);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
};
};
t.start();
}
任何帮助将不胜感激:)