18

I am new to android development, and trying to call local .NET web api service in android via retrofit library. After starting my web api on IIS I am getting this error failed to connect to localhost/127.0.0.1 android.

When I did same thing as suggested http://themakeinfo.com/2015/04/retrofit-android-tutorial/, It's working fine, But my localhost service is not calling up from android

My service url is, http://localhost:52511/api/Values/getAllStudents/5

and it is giving me output in XML format in browser too.

I have also try to call it with,

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

but no luck.

Please tell me where do I need to change to make it work. ?

Response I am getting in browser is,

<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>
Keval Patel
  • 925
  • 4
  • 24
  • 46
  • 4
    Your Android device won't be able to connect to localhost. You need to use an ip that's accessible over the LAN. Make sure you can access the address from the browser on your phone. – Will Calderwood Jul 25 '15 at 07:05
  • Thanks for reply, Please suggest any article which tells about accessing localhost on device and other configurations. – Keval Patel Jul 25 '15 at 07:13
  • You are using `10.0.2.2`. That will only work if your app runs on an emulator. You did not tell where your app runs on. A real device? – greenapps Jul 25 '15 at 13:37
  • 1
    Yes, I want to run app on real device. But on emulator too is giving error with message, failed to connect to /10.0.2.2 (port 52511) after 15000ms. – Keval Patel Jul 25 '15 at 14:11
  • For real device I am following this article "https://developer.chrome.com/devtools/docs/remote-debugging#port-forwarding", but no luck – Keval Patel Jul 25 '15 at 14:12
  • Working url in browser is, localhost:52511/api/Values/GetProduct/5 and my IPV4 address is : 196.168.1.2. So, I am using url : 192.168.1.2:52511/api/Values/GetProduct/5 But it shows bad request. – Keval Patel Jul 25 '15 at 17:03
  • You should try 192.168.1.2:52511 in a browser on your device. – greenapps Jul 25 '15 at 17:17
  • Bad Request - Invalid Hostname (400) – Keval Patel Jul 25 '15 at 17:29
  • You can try this once and check after firewall settings. (https://stackoverflow.com/questions/45940861/android-8-cleartext-http-traffic-not-permitted) – Guy-Michel NDUWIMANA Jun 22 '21 at 10:07
  • Does this answer your question? [How to connect to my http://localhost web server from Android Emulator](https://stackoverflow.com/questions/5806220/how-to-connect-to-my-http-localhost-web-server-from-android-emulator) – volodya7292 Jan 20 '22 at 19:25

6 Answers6

22

Instead of using 127.0.0.1:<PORT> use your local IP

Dewsworld
  • 13,367
  • 23
  • 68
  • 104
5

There's nothing to do with retrofit library in your case.

It's about your network settings, your phone and IIS server must be the same LAN.

You can follow as below.

  1. Launch an AP on you IIS server;
  2. Connecting the AP with your phone.

Sometimes you need to close security firewall on your IIS server.

Xiaozou
  • 1,655
  • 1
  • 15
  • 29
  • Still I am not done with this issue, but answer shows me correct way :) – Keval Patel Jul 25 '15 at 11:57
  • 4
    Working url in browser is, http://localhost:52511/api/Values/GetProduct/5 and my IPV4 address is : 196.168.1.2. So, I am using url : http://192.168.1.2:52511/api/Values/GetProduct/5 But it shows bad request. – Keval Patel Jul 25 '15 at 17:03
5

I had the same problem as u (failed to connect to localhost/127.0.0.1). First of all i recommend u to read this: android developer's guide (main part of this on the image↓) enter image description here

I had all of this, mean A, B and C. As C(emulator) i used a Genymotion. And i solved my problem by changing Network Configurations on it.

IMPORTANT: my adress in baseURL were: "localhost:8099/"

Let's see it: U should click on SettingsNetwork → check Use HTTP Proxy → input your IP adress and Port(in my situation it was 8099) → close. See it more details on image ↓

enter image description here

enter image description here

ahuminskyi
  • 211
  • 2
  • 8
0

I have the same problem, and I resolve it by changing the IP address instead of 'localhost':

  1. open cmd(if you are using windows): type 'ipconfig' and see your computer's ip.(make sure that your computer and your android device connect to the same network).
  2. remove 'localhost' in the url and use the ip(ip of your computer in step 1).
  3. run the project again and check
-3

Actually this helped when using 2 apps on the same Android device (Android 9)

android:usesCleartextTraffic="true"
-8

You can try this in androidmanifest.xml

android:usesCleartextTraffic="true"
ihaydinn
  • 199
  • 4
  • 16