android studio无法连接到本地主机WCF服务
问题描述:
我已经在本地主机上使用IIS发布了我的Restful WCF服务。我可以从我的系统上的任何浏览器访问此Web服务,使用url http://localhost/mobservice/MobSrv.svcandroid studio无法连接到本地主机WCF服务
getCityList是webGet方法在此服务中返回城市列表。我在Android Studio上完成了这项服务。但是,在我的Android程序它显示错误:
java.io.FileNotFoundException:http://10.0.2.2/mobservice/MobSrv.svc/getCityList
我的Android代码如下:
String serviceUrl="http://10.0.2.2/mobservice/MobSrv.svc/getCityList";
URL urlToRequest = new URL(serviceUrl);
urlConnection = (HttpURLConnection)
urlToRequest.openConnection();
int statusCode = urlConnection.getResponseCode();
if (statusCode == HttpURLConnection.HTTP_UNAUTHORIZED) {
// handle unauthorized (if service requires user login)
} else if (statusCode != HttpURLConnection.HTTP_OK) {
// handle any other errors, like 404, 500,..
}
InputStream in = new BufferedInputStream(urlConnection.getInputStream());
的StatusCode返回值404,和的URLConnection .getInputStream()如上所述给出异常java.io.FileNotFoundException。
ADB集成已在android Studio中启用。
答
使用ipconfig for Windows和ifconfig for Linux查找您的计算机IP地址。它将显示为IPv4地址。
然后在请求URL中使用该IP地址代替10.0.2.2。
http://computer_ip_address/mobservice/MobSrv.svc/getCityList
其中节目的线示出它的例外。你能否提供你得到的异常层次结构信息? – SachinSarawgi
我编辑了一些细节。 – Admin
@SachinSarawgi我GOOGLE了一下,才知道,要访问本地主机使用10.0.2.2,但它不工作 – Admin