简体   繁体   中英

android studio unable to connect to localhost WCF service

I have published my Restful WCF service in localhost using IIS. I can access this web service from any browser on my system, using url http://localhost/mobservice/MobSrv.svc

getCityList is webGet method in this service which returns city list. I am consumig this service in Android Studio . But in my android program it is showing error :

java.io.FileNotFoundException: http://10.0.2.2/mobservice/MobSrv.svc/getCityList

My Android Code is like :

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 returns value 404, and urlConnection.getInputStream() giving exception java.io.FileNotFoundException as described above.

ADB Integration is Enabled in android Studio.

Find your computer IP address using ipconfig for Windows and ifconfig for Linux. It will be shown as IPv4 address.

Then in the request URL use that IP address in place of 10.0.2.2.

http://computer_ip_address/mobservice/MobSrv.svc/getCityList

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM