4

I have written a web service locally using python flask framework which runs on localhost:5000 , I run this web service successfully from my computers browser by writing http://localhost:5000/toi/something

Now I am trying to call same service from the browser of my android device by connecting it with the machine. I am doing this for the first time so I am not sure whether it works this way or I am doing something wrong but it does not show me the output on my android device. Once this is done I will be calling the web service programatically in my android application.

Thank You ifconfig image ip for which I am getting expected result on machine ip for which I am not getting result on machine android device using 1st IP android device using 2nd IP

Yogesh D
  • 1,663
  • 2
  • 23
  • 38

2 Answers2

5

I don't think that will work, if you´ve got the phone connected to the same Wifi network than your computer you should access it this way:

http://ipofyourcomputer:5000/toi/something/

Once this works out you can try it with some of the libraries available on Android like:

OkHttp - https://github.com/square/okhttp

android-async-http - http://loopj.com/android-async-http/

Volley - https://android.googlesource.com/platform/frameworks/volley

Or if you're building a REST service you can use:

Retrofit - http://square.github.io/retrofit/

Hope it helps.

Community
  • 1
  • 1
reixa
  • 6,903
  • 6
  • 49
  • 68
  • is WIFI compulsory..? I have connected device using USB in teathering mode...? Should it not work..? – Yogesh D Feb 19 '14 at 11:39
  • 2
    Wifi it´s just a scenario, if device and pc are connected to same Wifi network for example, the router will give both different IP address so localhost stands for the IP of each one. ie: android ip (1.1.1.1) and pc ip (2.2.2.2) localhost:5000/toi/something will resolve to 1.1.1.1:5000/toi/something and not 2.2.2.2:5000/toi/something – reixa Feb 19 '14 at 11:48
  • In your case I think that if the mobile works as a router this one will have a different IP from the PC. That's because for the inner network that your mobile creates, every device connected should have different IP. – reixa Feb 19 '14 at 11:50
  • Does that mean I cannot call the web service just by connecting the device to my machine using USB I need to have them over same WIFI network..? – Yogesh D Feb 19 '14 at 11:56
  • Nope, that means you cannot call localhost:5000/toi/something on your mobile and get to your computer. You will have to use ipconfig (windows) or ifconfig (Mac/Linux) on your computer and make the call from you Android browser to http://thatip:5000/toi/something – reixa Feb 19 '14 at 11:58
  • @y.dixit were you able to access your app? – reixa Feb 19 '14 at 14:56
  • I tried connecting android device to machine using USB teathering and then tried to access the web service using, `192.168.42.154:5000/toi/input parameters` but I get `could not connect to error 192.168.42.154:5000` .....I later even tried using localhost:5000 and 127.0.0.1:5000 (both are actually same)...but that also dint work.... – Yogesh D Feb 20 '14 at 04:04
  • I even found the similar question [here](http://stackoverflow.com/questions/6176609/connect-an-android-device-to-a-web-service-on-local-host) and tried soln given by Zulkifli fasilkom01 but even that dint worked for me.... – Yogesh D Feb 20 '14 at 04:14
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/47926/discussion-between-axierjhtjz-and-y-dixit) – reixa Feb 20 '14 at 09:02
0

My problem solved by turning off windows firewall.

Ramin PK
  • 338
  • 1
  • 5
  • 16