Calling web service from android device
Asked Answered
L

2

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

Lateral answered 19/2, 2014 at 11:29 Comment(2)
In place of local host put your machine ip addressBaptize
@user88 already tried that...but it doesn't show me anything ....as not even page not found error...just keeps on loading...Lateral
I
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.

Ironhanded answered 19/2, 2014 at 11:34 Comment(9)
is WIFI compulsory..? I have connected device using USB in teathering mode...? Should it not work..?Lateral
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/somethingIronhanded
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.Ironhanded
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..?Lateral
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 thatip:5000/toi/somethingIronhanded
@y.dixit were you able to access your app?Ironhanded
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....Lateral
I even found the similar question here and tried soln given by Zulkifli fasilkom01 but even that dint worked for me....Lateral
let us continue this discussion in chatIronhanded
C
0

My problem solved by turning off windows firewall.

Currier answered 26/9, 2016 at 14:6 Comment(1)
You have 50 poit you can make a comment, this can be a comment not an answer!Durance

© 2022 - 2024 — McMap. All rights reserved.