I have a web service which has two different methods like 'returnInt' and 'returnImg'.
Is it possible to call these two methods in my single android activity?
I am using kSOAP for this and I have a java WebService.
Well in my application;
I have a Python XML WebService on the server side. On the client side I use Android-xmlrpc library which is a very thin and open source library and works great for my case.
In the client application, it is possible to call more than one method of the WebService within the same activity. But I suggest to, issue webService calls on the separate threads. Also be sure that your web server is multi-threaded and capable of concurrent calls.
yes its possible to call any number of WS from an acitivty. Just make sure that each WS call is running in a different thread and if one WS call needs to precede the other then proper handling using message queues is done.
Related
I am working on an android app in which I want all http request sent by android phone .Is it possible in android by creating a service?
You can create a service to do so, but it will make your code much more difficult, because services have different lifecycles than activities and fragments, they can exist even if the UI is not running. I give you the advice to handle it in your application code. One good library is OkHttp or if you want something more abstract you can go with Retrofit
I'm a beginner in android development and I'm trying to implement an android udp client, which connects to a java server and sends/receives some packets from it.In this process it collects some data (like round-trip delay etc), which is used to measure the QoS of that particular network. I have tried implementing the connection and sending/receiving data using Java Threads, but the application crashes, and hangs if i try to use more than 2 threads. So I'm looking for alternatives. While going through this site as well as some other links I found that in android multiple threads can be implemented using AsyncTask, Handler etc. Also I found that the Service class also helps to run a background service in an app. Please suggest which approach among these would be the best to achieve my purpose.
Thanks in advance.
You can use AasyncTask to do this and as you mentioned service may be useful too, where u can let your application do whatever it wants in background , if user needs to use application by its interface then AsyncTask must be used to avoid Crashing
There is not one right answer that can be applied as a broad stroke to how to do Android multi-threading. There are a few different ways to approach it based on what your specific needs are.
Any long running, blocking call, in Android will result in the application crashing.
The most common solution is to use an AsyncTask though. For example, when I want to make a call out to a web API endpoint for some XML data within an Activity I would in this case use an AsyncTask and kick off the calls from within doInBackground.
This is not an appropriate solution though if the wait time is longer, or possibly an unknown wait time. Or in a situation where there will always be waiting such as a message queuing service. In this type of situation it may be best to write a separate app based on extending the Service class. Then you can send/receive notifications to/from the service from your primary application in a similar manner to how you would communicate with a web service.
I'm having problems trying to find the best way to do two-way server communications, asynchronously. I am able to do the server communications hard coded to a server (no encapsulation, async calls), but I want to know what the best way to do it is. There's a protocol for logging in, registering, changing location, etc.
The server communications are using JSON and HTTPPost. The app would be sending JSON data and receiving it and putting it into a database.
I thought of making a ServerCommuncations class which holds data such as username, password, etc. and instantiates the HTTPRequest objects and other common objects. This could be inside a Bound service.
Now I could use a Bound Service with an AsyncTask, however multiple Activities in the app will need to do server communications so it would mean binding the service to multiple Activities.
Is this possible?
Is there a better way?
If I was to use the bound service, would it be best to use Messengers or extend Binder class?
What are your thoughts?
A possibility is to use IntentService for handling asynchronous code.
Maybe you could use a real Thread and pass it a handler which is a singleton. you could then use the handler in all your activites.
I want to create an app wich connects to one server with an https connection from different activities. So I thought, it would make sense to use a service which holds the https connection and i would call it from different activities to get different sites from that server... But after searching a long time (and reading an android book) I didn't find out, how to call different methods of a service from an activity. (only way that seemed possible was aidl with .asInterface, but that never worked for me, as one can see in my other question)
Is there a good tutorial, how I can do this?
Thanks in advance,
Tyde
The way I would do this is to have one package that does the http calls, internally it may use HTTPUrlConnection or AndroidHTTPClient, and throughout the codebase I will just call the helper methods (which I will write, basically wrapper methods) of this class/package to make the calls. Note that for me the http connections are either handled as subclasses of DataHandler to be run on seperate threads, or as async tasks, the point being we dont lock the main UI thread with it.
On a side note look at this blog post too. Android developer blog have good posts on this topic.
I havent seen http calls managed by a service, the Async task/Data Handler mechanism seems more simpler to me. Hope this helps.
I'm writing an android app that will connect to a REST/JSON webservice. Users will be retrieving information, uploading comments, downloading and uploading images etc.
I know that I shouldn't keep all this network communication in the Activity/UI thread, as it will cause ANRs. What I'm confused about is whether I should use an AsyncTask or a Service with "manual" threading to accomplish this;
With a Service, I'd simply have a public method for each method in the webservice's API. I'd then implement threading within each of these methods.
If I used an AsyncTask, I would create a helper class that defined AsyncTasks for each method in the webservice's API.
Which method is preferred? Interaction with the webservice will only happen while the user is in the Activity. Once they switch to another application, or exit the program, there is no need for communication with the webservice.
I recommend you go for the AsyncTask solution. It is an easy and straightforward way of running requests or any other background tasks of the UI-thread.
It's also easy to implement e.g. onProgressUpdate if you need to show a progress bar of some sort while running your requests.
I recommend IntentService, it is not much more complex to implement and is definitely more robust because it is not tied so closely on the ActivityLifecycle (in particular to onConfigurationChange())
This library provides an async wrapper to Apache httpclient available in Android.
http://loopj.com/android-async-http/