Android: Service to communicate with server API - android

I'm using AsyncHTTPClient to communicate with my server API.
I've read that I have to use services to communicate with a server.
1) Is AsyncHTTPClient already a service? Or do I still need to put all methods which use AsyncHTTPClient into my own services? Or is it OK to call AsyncHTTPClient methods directly from my Activities?
2) I can't understand how services should be properly used. Let's say I don't use AsyncHTTPClient but I have few different actions which make a call to server API (get realities, get users, post user, etc.). Do I have to put each action into a separate service? If not, how can I call different methods from one same service? All examples I have seen always show that there's just one action which is automatically being called on service start up by Activity.
3) I found another Android REST library Retrofit - does it have any advantages over AsyncHTTPClient? Do I need to put it into services?

At first, I would recommend you to use Retrofit. I've already used AsynHttp, but now I am using Retrofit, and I am very satisfied with it.
About the topics in your question:
I've read that I have to use services to communicate with a server.
Where do you read this? I don't create Service to my requests, and all of then works pretty well. The main point when creating your requests is to make them in a background thread. So if you don't use some lib like AsyncHttp or Retrofit, you have to put your requests in an AsynTask class, for example.
Is AsyncHTTPClient already a service?
No. It is a library to make asynchronous HTTP requests. In the site of the lib, they say:
All requests are made outside of your app’s main UI thread and You can also use it in Service or background thread.
That is, you can use it in a Service, they do not say you have to use it in a Service.
The example in the site teaches how to make a RestClient with this library. It is a good example. You only need to create a static class and put the methods to make your posts, gets etc
I can't understand how services should be properly used.
Services are used for long-running operations in the background. A possible way to use a Service with your lib (AsyncHttp or Retrofit) is when you want to make requests to the server, even when the user is not using your app. So you can create a Service with a timer that will call the server at the intervals defined by you.
Retrofit - does it have any advantages over AsyncHTTPClient?
Backing to Retrofit, again you don't have to put it in a Service. Only if you need this. From threir site, they say: Retrofit turns your REST API into a Java interface. This make your client very simple, and self documented because each call to your server API will be a method in this interface, and I think this is a big advantage. It is simple to use Retrofit. As I said, now I changed to Retrofit, and I can say, the lib has a good documentation and support, it has many users. So it is easy to find solutions to some bug. Other advantage is that it already has the lib GSON, which is very useful to parse the JSONs that you will receive from your server API.
But of course talking about vantages is dangerous because you can find many personal opinions. See this question and its answers. I think some answers from there can also help you with your choice.
To finish, here are some links with tutorials about Retrofit and how to create your requests with this powerful library.
Durable Android REST Clients
Consuming APIs with Retrofit
A smart way to use Retrofit

Related

Easy android https request?

I know, there are much posts and answers on this topic and it seems, I have read them all... But I just want to know how to do an easy https request in my Android app. Is there a easy class which make such an request?
I want to call something like
String response = new EasyHttpRequest().execute(myUrl).get();
in my MainActivity
For information I can't just set the response Text to an TextView in the onPostExecute() method, I have to work with this data and save it or something else.
The best library I know so far for network requests is Retrofit. It's kind of a standard for Android apps. If you want something more low level you can use Okhttp.
Both libraries can work together, and they are more than production ready. Of course, they support https and many other security features as ssl certificate pinning.
They support running the request on the same thread you are or in a background queue provided by the library, up to you if then you want to wrap it on an asyncTask, simple thread or service, those libraries leave you some freedom in that matter.
You can use this repo . it's very easy to use.
okhttp-utils

Interacting with the server API from android application

Can anyone explain for me or guide me through the basics that i have to know on how to interact with the server API from Android Application?
Any good website that teaches beginners because am new to this thing and am quite lost
If it is a restful API that you will be interacting with, I would highly recommend using RetroFit.
http://square.github.io/retrofit/
Square have excellent documentation and the examples should be easy to follow. If you get stuck you can always post on here under the Retrofit tag which has a fairly large following.
Volley is also an option.
Use Volley networking library http://developer.android.com/training/volley/index.html
This example shows how to make simple client-server app right now.
Here and here you can read description during making client-server app.
I found those links very useful for me when I started to learn this topic.
Hope it helps.
To get you started I've have a sample app Link which uses Github's REST and fetch the result and display it in android List. It is a basic one without any authentication. Hope this helps.
Recently android has introduced a library to parse json. It is called volley.
Volley offers the following benefits:
Automatic scheduling of network requests.
Multiple concurrent network connections.
Transparent disk and memory response caching with standard HTTP cache
coherence.
Support for request prioritization.
Cancellation request API. You can cancel a single request, or you can
set blocks or scopes of requests to cancel.
Ease of customization, for example, for retry and backoff.
Strong ordering that makes it easy to correctly populate your UI with
data fetched asynchronously from the network.
Debugging and tracing tools.
Tutorial which I used is Volley tutorial for json parsing. Another link is here
Hope this helps you.

Design pattern for network connections and requests

Im developing an app that have alot of web requests. Such as download or upload files , REST requests and etc.
I want to save all of this functions in a class like a helper and only just import the class and call functions that i need them in my activities.
something like a custom library for web requests i mean.
Is there any Design pattern for this?
(I hope i explain my idea well)
Consider using these libraries:
Robospice - for asynchronous network calls
Retrofit - for REST calls
OkHttp - good HTTP/SPDY client
Picasso - for image loading and caching
Jackson - to work with JSON
You can use custom IntentService class to hanle all networking there, starting this service from UI and passing apropriate ACTION to perform.
And of course i would suggest you watch this video from Google I/O 2010 and use REST Pattern A described there in EVERY network app you make.
You could use the Chain of Responsibility pattern for building up requests and then executing them. See some details here.
Of course, using just this one pattern would not be enough. It should be used in conjunction with an Observer, Factory, Proxy and maybe some others. Just start developing with SOLID in mind.

Use POST function from Android, using Drupal as back end

as the title said,
I'm looking for a method to handle a POST request made from my Android front end to my Drupal back end.
I've already seen a lot of documentation and reference, like this one: http://tylerfrankenstein.com/code/build-mobile-app-sell-products-with-drupal
but I can't figure it out.
Anyone can help me?
Use Services
It sounds like you need to setup an API using something like a REST service. Drupal has a great module for this called Services.
Essentially you can setup your Android app to use the POST method to push to your Drupal configured endpoint.
EG: https://www.mysite.com/api/android/
Services is extremely flexible and you can have it work through basic CRUD operations for nodes or you can use hooks and use custom logic for a particular service you want to use and write custom PHP code to do whatever you want.
How about using this library for native Android applications to communicate with Drupal web servers?
Feature list:
Synchronous and asynchronous requests
Flexible object serialization / deserialization
API can calculate object differences to perform patch requests
Simple entities request

Is an Android Service a good idea when backing up an app's data using a web service?

I'd like to add the ability for my android app to save changes to data (stored in its SQLite database) to a web server. The upload will be via HTTP POST, using JSON in the body of the request to describe the changes.
I'm wondering if I should use an android Service for this. I'd like the user to be able to continue interacting with the app while it's generating the JSON, making the call to the server, and waiting for the server to complete its work and return a response.
Thanks much!
Even better would be an IntentService
Edit: Now that the compatibility package is out I think the new Loader class may be the best option, possibly using the ASyncTask version or a variation of it.
Yes it is a very good idea to move all the webservice code into a service. The Google IO talk Developing Android REST client applications talks about many reasons why this is a good idea. It also covers other important considerations which relate to your problem which is effectively syncing your database to the cloud.

Categories

Resources