Android Volley Examples
Posted / Публикувана 2013-05-20 in category / в категория: Android
|
Warning: count(): Parameter must be an array or an object that implements Countable in /home/bolyarco/www-ikratko/ogrelab/wp-content/plugins/microkids-related-posts/microkids-related-posts.php on line 645
In brief: here is the git project with the Volley examples/samples
In details:
Volley was announced just few days ago at the I/O 2013. It is a library/framework that helps the developers to create more easily applications that use network requests, and more precisely REST requests. I was very exited to give it a try but unfortunately I found that there are no examples and tutorials. Now, after experimenting with it, I will try to fill to some extend that gap.
Features of Volley:
- Nice and easy API for executing REST requests;
- unifies the mechanism for REST requests by abstracting the "bottom" layer, i.e. you don't care any more about HttpClient or HttpUrlConnection. On lower versions (<= GINGERBREAD) Volley uses AndroidHttpClient and no higher it uses HttpUrlConnection;
- it is very fast. In the I/O presentation the guy states that they tested it at Google with several different apps and Volley was clear winner in all categories;
- it is extensible -- you can use it as a basis to create your own custom type of requests (and response handling/parsing)
You can find the project with the examples at Github:
git clone git://github.com/ogrebgr/android_volley_examples.git
During the import of the project you may need to change the project name to "Volley Examples" otherwise you will end up with a project called "Act_Main".
You will need also the Volley itself:
git clone https://android.googlesource.com/platform/frameworks/volley
After importing Volley you may need to mark it as a "Library project" in order to include it in the example project.
Currently there are 6 examples:
- Simple request -- request against web server and fetches the body of the response. Uses
StringRequest
. - Using Volley with GET and POST parameters
- JSON request -- request against web server and parse the response as JSON. Uses
JsonObjectRequest
- Gson request -- request against web server and returns Java object that was created using Gson (i.e. converting the result string into JSON and then into Java object.). Uses "custom" made
GsonRequest
class. - Image loading -- request that loads image from server using
ImageLoader
. - Using cookies
- Using Volley with newer external HttpClient (4.2.3)
- Using Android Volley With Self-Signed SSL Certificate
- Dynamic ListView with NetworkImageView, "endless" paginatio, JSON parsing, etc.
In the next days I will add more examples.
All comments will be welcomed. If you wish to provide samples/examples that you want to be added to this project -- feel free to fork or write me an email (address is here).
|
May 29th, 2013 at 12:07:37
Excellent post, very helpful, looking forward for new examples
May 29th, 2013 at 11:37:43
Thanks for great examples. Looking forward for new ones.
Is it possible to reconnect to running request in Volley? I have an activity which is populated via webservice. It fires new request onCreate but I dont want to discard and fire new request during orientation change. I cannot put these data in cache as well. I just need to survive orientation change and listen to result of original request.
May 30th, 2013 at 08:29:23
@Eman
Usually the result from the first request will be cached automatically for you so on recreate second request will get the data from the cache.
As alternative you may declare your activity (in the manifest) to intercept the orientation change and handle it itself, i.e. to avoid recreation.
Yet another alternative is to use fragments, fire the request from the fragment, and then after rotation to reconnect to the fragment (where meanwhile the result from the request is populated). I think that is the best solution.
May 30th, 2013 at 09:46:27
Thanks for your answer. I think that the last approach is the best one and most suitable for my use case.
I used robospice before and there was possible to add listeners to running requests. It is possible to recreate this behavior in your Simple Request example. When you click on the button and change orientation, result is not delivered.
May 30th, 2013 at 03:53:07
Strange question, but how use Volley inside Fragment??
mRequestQueue = Volley.newRequestQueue(getActivity());
not working.
May 30th, 2013 at 04:00:45
@none
Is there a chance that you are calling getActivity() from a method that is before onActivityCreated() in the lifecycle of the fragment (like in onCreateView())? If yes: activity is not created yet, so getActivity() returns null. You will need to move it to onActivityCreated().
May 31st, 2013 at 01:27:48
You was right.
May 31st, 2013 at 02:29:16
I am getting this error
The method newRequestQueue(Context) in the type Volley is not applicable for the arguments (Act_SsSslHttpClient, ExtHttpClientStack)
May 31st, 2013 at 02:57:11
@suri
You need to pull latest volley library. Older one does not have the appropriate constructor.
June 5th, 2013 at 01:44:36
How can we use volley, the way this lib is used http://loopj.com/android-async-http/?
Like, I want to define my base url then only append the endpoints of my web-service?