Student
Student

Reputation: 571

How to connect android phone to a web service run on local server?

I had tried on my emulator, it can connect to the java web service I created. But when I try to access from my android device, it does not work. Is anyone know how to get through this?

Another question is, by doing this way, is that possible 2 android devices access to the same database?

I am using Tomcat server and MySQL database. The web service is actually use to get the data from the MySQL database and pass to my android phone.

Thank you in advance.

Upvotes: 4

Views: 4123

Answers (6)

Shobhit Puri
Shobhit Puri

Reputation: 26017

You can get a public URL for your server running on a specific port on localhost.

At my work place I could access the local server by using the local IP address of my machine in the app, as most of the other answers suggest. But at home I wasn't able to do that for some reason. After trying many answers and spending many hours, I came across https://ngrok.com. It is pretty straight forward. Just download it from within the folder do:

ngrok portnumber

( from command prompt in windows)

./ngrok portnumber

(from terminal in linux)

This will give you a public URL for your local server running on that port number on localhost. You can include in your app and debug it using that URL.

You can securely expose a local web server to the internet and capture all traffic for detailed inspection. You can share the URL with your colleague developer also who might be working remotely and can debug the App/Server interaction.

Hope this saves someone's time someday.

Upvotes: 0

stuckedunderflow
stuckedunderflow

Reputation: 3777

When it works on emulator then it should work on real device. Perhaps it is something about proxy or network issue.

For your second question. Do you use local database for client like SQLite? If yes then each device has its own DB which you need to sync to server DB (MySQL) through web services.

Upvotes: -1

Nhật Minh Ôn
Nhật Minh Ôn

Reputation: 21

you can try use KSOAP2 library to access WebService, google it for now ;)

Upvotes: 0

Philip Sheard
Philip Sheard

Reputation: 5825

Have you tried IP address 10.0.2.2? That is localhost on the emulator.

Upvotes: 0

rogermushroom
rogermushroom

Reputation: 5586

Firstly you need to make sure your web service is visible through the IP of the machine that is hosting it. You could test this by trying to access the web server from another desktop using the IP and whatever port number it might be listening on.

If it is possible then it should just be a case of connecting your phone to the same network segment (router) the machine hosting the web server is on and use the IP (and port number) of the web server.

There should be no problem at all 2 android devices accessing the same server assuming the database is setup correctly and you are using the correct client code.

Upvotes: 1

Prashant Gami
Prashant Gami

Reputation: 1580

Can you run the service on your LAN IP instead of as localhost and connect the phone through wifi?

It can also be possible to connect via setting proxy.

Upvotes: 1

Related Questions