Wednesday, December 25, 2013

Singleton Pattern in Volley library

Singleton Pattern in Volley library

Google recommends that you use a common RequestQueue object for your app in the form of a Singleton. 


import android.content.Context;
import android.graphics.Bitmap;
import android.support.v4.util.LruCache;

import com.android.volley.RequestQueue;
import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.Volley;

public class VolleySingleton {
private static VolleySingleton mInstance = null;
private RequestQueue mRequestQueue;
private ImageLoader mImageLoader;

private VolleySingleton(Context context){
mRequestQueue = Volley.newRequestQueue(context);
mImageLoader = new ImageLoader(this.mRequestQueue, new ImageLoader.ImageCache() {
private final LruCache<String, Bitmap> mCache = new LruCache<String, Bitmap>(10);
public void putBitmap(String url, Bitmap bitmap) {
mCache.put(url, bitmap);
}
public Bitmap getBitmap(String url) {
return mCache.get(url);
}
});
}

public static VolleySingleton getInstance(Context context){
if(mInstance == null){
mInstance = new VolleySingleton(context);
}
return mInstance;
}

public RequestQueue getRequestQueue(){
return this.mRequestQueue;
}

public ImageLoader getImageLoader(){
return this.mImageLoader;
}

}

use this
RequestQueue queue = VolleySingleton.getInstance(context).getRequestQueue();

//or


mImageLoader = VolleySingleton.getInstance(context).getImageLoader();

Handle image in Android from Url

Setting up the Android Google Volley ImageLoader for NetworkImageView
-----------------------------------------------------------

<com.android.volley.toolbox.NetworkImageView
android:id="@+id/twitter_avatar"
android:layout_width="48dp"
android:layout_height="48dp"
android:layout_alignParentBottom="true"
android:layout_alignParentTop="true"
android:layout_marginRight="6dip"
/>

NetworkImageView avatar = (NetworkImageView)view.findViewById(R.id.twitter_avatar);
avatar.setImageUrl("http://someurl.com/image.png",mImageLoader);

Or

mRequestQueue = Volley.newRequestQueue(context);
mImageLoader = new ImageLoader(mRequestQueue, new ImageLoader.ImageCache() {
private final LruCache<String, Bitmap> mCache = new LruCache<String, Bitmap>(10);
public void putBitmap(String url, Bitmap bitmap) {
mCache.put(url, bitmap);
}
public Bitmap getBitmap(String url) {
return mCache.get(url);
}
});
NetworkImageView avatar = (NetworkImageView)view.findViewById(R.id.twitter_avatar);

avatar.setImageUrl("http://someurl.com/image.png",mImageLoader);

Spring for Android

                       Spring for Android




add the below code in you gradle file in android studio 
-----
dependencies {
compile 'org.springframework.android:spring-android-rest-template:1.0.1.RELEASE'
compile 'org.springframework.android:spring-android-auth:1.0.1.RELEASE'
compile 'org.springframework.android:spring-android-core:1.0.1.RELEASE'
}
add this in your activity
The following example illustrates how to search Google:

"services/search/web?v=1.0&q={query}";

// Create a new RestTemplate instance
RestTemplate restTemplate = new RestTemplate();

// Add the String message converter
restTemplate.getMessageConverters().add(new StringHttpMessageConverter());

// Make the HTTP GET request, marshaling the response to a String

String result = restTemplate.getForObject(url, String.class, "Android");

Android – Volley Library

                           Android – Volley Library


Volley is a library that makes networking for Android apps easier and most importantly, faster.

Advantages of using Volley:
                         Volley automatically schedule all network requests. It means that Volley will be taking                                  care of all the network requests your app executes for fetching response or image                                     from web.
                        Volley provides transparent disk and memory caching.
                      Volley provides powerful cancellation request API. It means that you can cancel a single                             request or you can set blocks or scopes of requests to cancel.
                     Volley provides powerful customization abilities.
                    Volley provides Debugging and tracing tools

How to get started?
    Clone the Volley project
       Import the code into your project
       Clone the Volley project: