How to call REST Web Service from Android application?


Last Updated:

  1. Ankit2502

    Ankit2502 Member This Topic's Starter

    Joined:
    Jan 6, 2010
    Messages:
    13
    Likes Received:
    0
    Hello Friends,
    I am new to Android and i want to call REST Web Service from application.Anybody can tell me what's the procedure or any sample code.
     

    Advertisement
  2. Waldorfian

    Waldorfian New Member

    Joined:
    Dec 30, 2011
    Messages:
    4
    Likes Received:
    0
    bump!

    I'm looking for the same information. I'm having trouble figuring out where to place my code so as not to get a NetworkOnMainThreadException. Can anyone point the flashlight in the right direction for us?
     
  3. jiminaus

    jiminaus Well-Known Member

    Joined:
    Oct 27, 2011
    Messages:
    191
    Likes Received:
    36
    The goal should be do to the blocking networking communication on a background worker thread. You'd also do any processing of the response data on the background worker thread. You'd process the data so that it's immediately ready to be set into the UI on the main UI thread.

    Look at the Processes and Threads framework topic. Particularly look at AsyncTask. The class overview of AsyncTask will tell you all the details of how to use it.

    In this case, if you use AsyncTask, you'd do the network communication and response processing in doInBackground(Params...) and then you'd update the UI in doPostExecute(Result).
     
    Waldorfian likes this.
  4. Waldorfian

    Waldorfian New Member

    Joined:
    Dec 30, 2011
    Messages:
    4
    Likes Received:
    0
    OK thanks for that. What I'm really trying to do is take the login userid (from my main activity) and query a MySQL database on another server via a web service which I've already created. Once I get the query data back, I want to populate some preferences (which I've already created in preferences.xml locally) so I can pre-populate EditText fields in another activity.

    All the documentation and suggestions from my Google searches show code fragments but not how they fit into the framework.

    All this is easy to do in PHP but I don't understand how it works in Android yet. Any further suggestions/directions/ideas would be greatly appreciated.
     
  5. jiminaus

    jiminaus Well-Known Member

    Joined:
    Oct 27, 2011
    Messages:
    191
    Likes Received:
    36
    There are a few ways to approach this depending on how you want the login experience to be for your user. Here's one way just to illustrate how the parts could fit together.

    Start by having a login credentials activity where users enter their credentials. This activity could be the main activity, started by the main activity, or perhaps started by clicking a login button somewhere.

    In the onClick handler for the login credentials activity's login button, start a login activity. This login activity will present some kind of logging in message to the user, perhaps with an indeterminate progress feature and a cancel button. Start the login activity using startActivityForResult(Intent, int). See Starting an activity for a result in the Android Dev Guide.

    The login activity will return the success status of the login to the login credentials activity. If the login was not successful, the login credentials activity can display an alert dialog before allow the user to try again with different credentials. If the login was successful, then the login credentials activity can start the home UI activity for your app or return to the activity that start it.

    In login activity's onCreate(), initialise an instance of an AsyncTask subclass. This AsyncTask subclass will do the network communication for logging in and downloading the preferences. It will do all of this in its doInBackground(Params...) method. The AsyncTask subclass's constructor will include an parameter for passing in the login activity. In the AsyncTask subclass's onPostExecute(Result) method, it calls login activity's setResult(int) or setResult(int, Intent) method and then the finish() method.

    This will cause a cascade of the login activity finishing and then login credential's onActivityResult(int, int, Intent) method. You'd override this method to react depending on the result code.


    Hopefully I've mentioned all the pieces and haven't left anything out. I'm still a bit groggy from New Year's Eve. Feel free to ask more questions.
     
  6. Waldorfian

    Waldorfian New Member

    Joined:
    Dec 30, 2011
    Messages:
    4
    Likes Received:
    0
    I really appreciated your responses on this. I have setup a login activity (actually the main activity) that asks for a userid to be entered and a login button. They have to enter something to get to the menu. This part works fine. I can get the userid that is entered and I save it to a shared preference so I can use it elsewhere in the app.

    OK, I've been pouring over Google seaches the last few days on this after realizing I couldn't do this in the main thread. I have created an AsyncTask subclass with a doInBackground(Params...) method.

    This is where I'm stuck now. I suppose I need to pass the URL of my web service and the userid as a parameter at the end, but I can't figure out how it works. I created a web service in PHP and placed it on my web server that has the MySql database that holds the data for preferences, etc. I think I'm getting close!

    It's New Years day here in Canada now, so I'm a bit groggy myself! :)
     
  7. jiminaus

    jiminaus Well-Known Member

    Joined:
    Oct 27, 2011
    Messages:
    191
    Likes Received:
    36
    If the URL of your web service is always the same, you could just put in as a constant (static final) field within your AsyncTask subclass. If it's not always the same, then you could pass the URL in as part of the constructor.

    BTW AsyncTask is generic type. If you've never dealt with generics before (superficially equivalent to templates in C++), you might want to quickly go through the first 3 steps of the Generics Java Tutorial. Presumably you'd invoke AsyncTask with Params being String.

    In regards to the userid (and presumably the password), you pass them when you call execute(Params...) on your AsyncTask. Then you receive them in doInBackground(Params...).

    BTW You're dealing with vararg methods here. If you've never dealt with varargs in Java, see the varargs page of the Java SE documentation.
     
  8. Waldorfian

    Waldorfian New Member

    Joined:
    Dec 30, 2011
    Messages:
    4
    Likes Received:
    0
    OK, I figured out how to structure my AsyncTask...finally. I Got it so my web service is being called but not getting anything back. I know the web service works because I run it in the browser and get the proper JSON output. Getting JSONException when I try to parse the response. It can't parse nothing! That's my next challenge!

    Thanks for your help to get me this far.
     

Share This Page

Loading...