When to use Volley for network operations? - android

I am developing an Android application which has 3 main types of network operations :-
1) OAuth type login authentication which is a 2 step process - first one is getting the access token and second is getting user details.
2) Post operations when user clicks a button only working with text data (no images)
3) Background sync operations where multiple databases will be updated on the client and server and most of these tables are not related to one other. So they can be performed in parallel.
So for these network operations should I use Volley or custom async http implementations?
What if I have images to upload and download in these operations.

Volley is much better that custom async operations and handles much more cases. The question would be more which http client library to use. There quite a few out there but Volley does a good job.
for point 1) you need to apply you custom flow and requests here but it is not difficult as Volley is quite modular
for uploading and downloading images you will also have to do some more work because it does not come out-of-the-box.
I would also suggest to try jus. it is based on volley but including many handy features, which will help you in your particular case as well.

Related

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.

Networking app architecture

I'm building an android app similar to a facebook app, aimed to display various information stored in a database.
I'm using on the server-side a REST API, which returns responses based on various POST requests, with a facebook token authentification.
And on the client-side, I'm using the volley library to deal with network requests. I've tried numerous possibities but I'm searching for the most elegant way of communicating with the server, and since this is a trivial case, I thought you could maybe help me with this one...
Since I'm always checking fb tokens, and making similar POST requests, I considered adding a Connexion objet, which creates a volley request when prompted with an execute(POST parameters...); method, and calls a callback method when the response has arrived.
But I'm struggling to decide whether I should create a SessionManager object (Singleton or not ?) which can process ALL the data from session related responses (like check login, login...) based maybe on codes (for example Error 5xx for every type of response), and DO the intents.
Or I should process these responses in every activity, and do the intents there. Knowing they can maybe repetitive.
In short, I'm looking for a best practice to apply when the app has to process common responses, and not so uncommon responses for example.
Keep all the logic in the activities ? Create objects ?
Don't hesitate to post your opinion on the subject !
Thank you very much.
EDIT : Ended up using a Connexion object to process all the requests (with volley). As for the Intents, I kept all that logic in the activities and haven't used another controller. The result was not ugly. Mainly because I used a secondary route which does the authentification, so the server ALWAYS responds with a big error if you're an evil hacker.
First of all I am not working with Volley but with the Apache HttpClient, this should not matter for your question though.
Having the code which handles the Post Requests in your Activities is a bad solution. See:
Single Responsibility Principle
Your idea with creating a SessionManager is really good though. You can handle all the stuff in the SessionManager and not bother with it in your Activities. Additionally you should add classes for different purposes than managing the session. If you get all friends from a specific user you should create a FriendsController or FriendsManager.
Processing the answers can be done in a single class too. I assume that you receive JSON from your API as response. Create one class that takes the response and returns a JSONObject.
Although it is far from perfect feel free to take a look at my app. I am currently learning Android / Java so it might not be as perfect as one might expect. The classes to handle POSTs are called YYYController and not Manager. But this is just a naming convention I use:
My Android Project
Your calls can return an enum in which you store the different Callback types:
class Enum Callbacks {
SUCCESS,
CREATED,
UNPROCESSABLE_ENTITY
}
In your application you can use them like this:
Callbacks response = SessionManager.login();
if (response == Callbacks.SUCCESS) {
//your login logic here
}

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.

posting data from android to rails using Redis server

We want to achieve simple but efficient system to post concurrent data from android application to rails server. The data sent [post] from multiple android devices, would be more than 300 at same time.
To achieve this, sidekiq could be best option. https://github.com/mperham/sidekiq/wiki/FAQ#how-do-i-push-a-job-to-sidekiq-without-ruby
I'm trying to post a data through android app to redis to process it asynchronously & then send it to rails application & database.
Does anyone know the exact procedure to achieve this ? Is it the best solution to handle requests asynchronously ?
handling the request asynchronously is usually done when you expect something slow to happen sometimes inside the request (like calling a 3rd party service). In your case, all you will do is insert a record into the database, so you don't really need to handle anything asynchronously unless you're going to use a distributed database system or something very complex. A normal SQL server will easily handle a few thousands of inserts per second (unless you have large and complex indexes affecting them), so this shouldn't be a problem at all.

best practices for "data layer" in android client apps

Here is one design/ best practices question..
I'm new to android development, and basically new to web/mobile solutions.
So, my question is - what are best practices when organizing structure of android application that get data from the remote server?
Should request to server go into one class that does communication with server (get and post requests), or should I look at my requests as data source, meaning that every data class manages it for itself?
or should I have more levels of abstraction - one level for acquiring data, other for model that uses some interfaces without knowing from what source data come from?
I'm curious how experienced android developers approach to these design issues...
Virgil Dobjanschi presentation is a good resource as pointed earlier, which basically tells you to run your requests from a background service so the activity does not get destroyed and to store your data in the database as early as possible.
For more technical details, the way I am doing it is to divide the app into three components:
1- Library to encapsulate the handling of the HTTP request and response (with ApacheHTTP), which can handle simple request/response and advanced features that might involve cookies (can be necessary for login) and modifying the HTTP header.
2- Marshal/Unmarsha layer, where I parse the server data (e.g. XML or JSON) and convert it to objects (i.e. models) that the rest of my app will deal with.
3- Persistence layer.
As per Dobjanschi's presentation, I usually make data requests run in a service not in a thread worker inside the activity.
Use one of the 3 models presented at this Google I/O talk. It provides you suggestions that will help you out on the whole process of definition of your app architecture. It'll also prevent you from making common mistakes beginners use to make:
http://www.youtube.com/watch?v=xHXn3Kg2IQE
This post will also help you out:
Need sample Android REST Client project which implements Virgil Dobjanschi REST implementation pattern

Categories

Resources