How to Call a Local Web Service from an Android Mobile Application

How to call a local web service from an Android mobile application

Working with kSOAP2 can be challenging... i know. But it is a good library and I have been able to get it to work. I have written a little on working with kSOAP2 on Android. I discuss passing simple and complex parameters, receiving arrays, and working with JDBC WebRowSet objects over the wire.

http://roderickbarnes.com/blog/droid-chronicles-web-services-handling-complex-parameters

There are other blog articles on working with kSOAP2 on the site. I hope this helps.

Calling a web service from android locally

When you want to call a web service using Internet, you use the external ip and port of the server. But if you want to call your web service locally (without the use of Internet), you need to use the internal ip of your server. You can see your internal ip from Windows command ipconfig or linux command ifconfig

How to call java web service from android app?

You're trying to connect to localhost. Localhost is the machine the code is running on. But your server isn't on your phone (even if using an emulator, the emulator is a VM- it counts as a separate machine). So its not connecting, and not getting any data. Use the real url/ip address.

You also need to account for the case of a connection not working for some reason (for example, the server is down) and try to handle it more gracefully than a crash. Your problem here is that you depend on result being non-null in the return of doInBackground. You can fix this by moving return result.toString into the try block, and putting return null where you have it now.

Cannot connect to localhost API from Android app

If you are testing your application from a real android device then you need to put the IP address of your PC while you are trying to connect to your Django server through APIs. And yes, you need to be in the same network as well. So you need to check the following things.

  1. Make sure that the PC (where you are running the server) and the Android device (where you are testing your application) are in the same network (connected with same Wifi network maybe).
  2. Make sure you are connecting to the IP address of your PC where the server is running. For example, right now, the IP address of your PC is 192.168.0.100. Then, you need to connect to this IP address and call your API like the following.

    http://192.168.0.100:8000/api/update/1/
  3. Make sure you are accepting requests to the port 8000 in your PC. Check your Firewall configuration if it is blocking any incoming requests to the 8000 port. If it is found blocking, then please allow an incoming request to the 8000 port using the following.

    sudo ufw allow 8000/tcp

If there is nothing which is helping you, then please check your Android code to check if the API calling is okay. I would strongly recommend using Volley for API calls suggested in developers documentation.

Last, but not the least, please check if you have necessary permission in your AndroidManifest.xml file. You need to add the following permission to grant your application to use the internet.

<uses-permission android:name="android.permission.INTERNET" /> 
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />


Related Topics



Leave a reply



Submit