Hi All,
Volley is a library that handles the processing and caching of network requests. It helps to download the imaged from web asynchronously. We do not worry about the Async Task to download the image from network because Volley library now can able to handle the image download asynchronously.
We already had known the famous library Universal Image Loder and Piccaso. Both are easy to handle to download the image in a thread pool and maintained the cache management.
Efficient network management Easy to use request managements Disk and memory cache management Easy to extend and customize to our needs - See more at: http://www.survivingwithandroid.com/2013/11/android-volley-tutorial-post-download.html#sthash.ATBAtGr2.dpuf
For download or clone of Volley library you have to run predefined command in your system, In your system should have installed the git.
Efficient network management Easy to use request managements Disk and memory cache management Easy to extend and customize to our needs - See more at: http://www.survivingwithandroid.com/2013/11/android-volley-tutorial-post-download.html#sthash.ATBAtGr2.dpuf
Efficient network management Easy to use request managements Disk and memory cache management Easy to extend and customize to our needs - See more at: http://www.survivingwithandroid.com/2013/11/android-volley-tutorial-post-download.html#sthash.ATBAtGr2.dpuf
Efficient network management Easy to use request managements Disk and memory cache management Easy to extend and customize to our needs - See more at: http://www.survivingwithandroid.com/2013/11/android-volley-tutorial-post-download.html#sthash.ATBAtGr2.dpuf
Efficient network management Easy to use request managements Disk and memory cache management Easy to extend and customize to our needs - See more at: http://www.survivingwithandroid.com/2013/11/android-volley-tutorial-post-download.html#sthash.ATBAtGr2.dpuf
For network operation I recommended to use this library instead of AsynTask.
Volley is a library that handles the processing and caching of network requests. It helps to download the imaged from web asynchronously. We do not worry about the Async Task to download the image from network because Volley library now can able to handle the image download asynchronously.
We already had known the famous library Universal Image Loder and Piccaso. Both are easy to handle to download the image in a thread pool and maintained the cache management.
Volley simplify everything and increases the app performances. - See more at: http://www.survivingwithandroid.com/2013/11/android-volley-tutorial-post-download.html#sthash.ATBAtGr2.dpuf
Volley have lot of advantages:- Automatic scheduling of network requests.
- Multiple concurrent network connections.
- Transparent disk and memory response caching
- 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.
- Application performance
For download or clone of Volley library you have to run predefined command in your system, In your system should have installed the git.
1. git
clone
https:
//android.googlesource.com/platform/frameworks/volley
2.
android update project -p .
3.
ant jar
Once you done this commond you can get the volley clone library in your directory.
Only thing you have to use the NetworkImageView in place of ImageView. I have share code for GridView image loader with volley that manage the cache management for network processing.
Volley simplify everything and increases the app performances. - See more at: http://www.survivingwithandroid.com/2013/11/android-volley-tutorial-post-download.html#sthash.ATBAtGr2.dpuf
Volley simplify everything and increases the app performances. - See more at: http://www.survivingwithandroid.com/2013/11/android-volley-tutorial-post-download.html#sthash.ATBAtGr2.dpuf
activity_main.xml
< RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="comsunil.volleygridexample.MainActivity" >
< /RelativeLayout>
grid_image_item.xml
< ?xml version="1.0" encoding="utf-8"?>
MainActivity.java
package comsunil.volleygridexample;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.NetworkImageView;
public class MainActivity extends ActionBarActivity {
ImageLoader mImageLoader;
NetworkImageView mNetworkImageView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mImageLoader = MyVolleySingleton.getInstance(this).getImageLoader();
final GridView gridView = (GridView) findViewById(R.id.gridView);
gridView.setAdapter(new MyImageAdapter());
}
static class ViewHolder {
ImageView imageView;
}
class MyImageAdapter extends BaseAdapter {
@Override
public int getCount() {
return ImageUrlArray.IMAGES.length;
}
@Override
public Object getItem(int position) {
return null;
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = convertView;
final ViewHolder gridViewImageHolder;
// check to see if we have a view
if (view == null) {
view = getLayoutInflater().inflate(R.layout.grid_image_item, parent, false);
gridViewImageHolder = new ViewHolder();
gridViewImageHolder.imageView = (ImageView) view.findViewById(R.id.networkImageView);
view.setTag(gridViewImageHolder);
} else {
gridViewImageHolder = (ViewHolder) view.getTag();
}
mNetworkImageView = (NetworkImageView) gridViewImageHolder.imageView;
mNetworkImageView.setDefaultImageResId(R.drawable.no_image);
mNetworkImageView.setErrorImageResId(R.drawable.error);
mNetworkImageView.setAdjustViewBounds(true);
mNetworkImageView.setImageUrl(ImageUrlArray.IMAGES[position], mImageLoader);
return view;
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
MyVolleySingleton.java
package comsunil.volleygridexample;
import android.content.Context;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.Volley;
/**
* copied from the official documentation
*/
public class MyVolleySingleton {
private static MyVolleySingleton mVolleyInstance;
private RequestQueue mRequestQueue;
private ImageLoader mImageLoader;
private static Context mContext;
private MyVolleySingleton(Context context) {
mContext = context;
mRequestQueue = getRequestQueue();
mImageLoader = new ImageLoader(mRequestQueue,
new MyLruBitmapCache(MyLruBitmapCache.getCacheSize(context))
);
}
public static synchronized MyVolleySingleton getInstance(Context context) {
if (mVolleyInstance == null) {
mVolleyInstance = new MyVolleySingleton(context);
}
return mVolleyInstance;
}
public RequestQueue getRequestQueue() {
if (mRequestQueue == null) {
// use the application context
mRequestQueue = Volley.newRequestQueue(mContext.getApplicationContext());
}
return mRequestQueue;
}
publicvoid addToRequestQueue(Request req) {
getRequestQueue().add(req);
}
public ImageLoader getImageLoader() {
return mImageLoader;
}
}
MyLruBitmapCache.java
package comsunil.volleygridexample;
import android.content.Context;
import android.graphics.Bitmap;
import android.support.v4.util.LruCache;
import android.util.DisplayMetrics;
import com.android.volley.toolbox.ImageLoader;
/**
* copied from official documentation
*/
public class MyLruBitmapCache extends LruCache
implements ImageLoader.ImageCache {
public MyLruBitmapCache(int maxSize) {
super(maxSize);
}
public MyLruBitmapCache(Context ctx) {
this(getCacheSize(ctx));
}
@Override
protected int sizeOf(String key, Bitmap value) {
return value.getRowBytes() * value.getHeight();
}
@Override
public Bitmap getBitmap(String url) {
return get(url);
}
@Override
public void putBitmap(String url, Bitmap bitmap) {
put(url, bitmap);
}
// Returns a cache size equal to approximately three screens worth of images.
public static int getCacheSize(Context ctx) {
final DisplayMetrics displayMetrics = ctx.getResources().
getDisplayMetrics();
final int screenWidth = displayMetrics.widthPixels;
final int screenHeight = displayMetrics.heightPixels;
// 4 bytes per pixel
final int screenBytes = screenWidth * screenHeight * 4;
return screenBytes * 3;
}
}
For network operation I recommended to use this library instead of AsynTask.
Here you can dwonload the source code VolleyGridview
package com.sunil.volleynetworkoperation;
import org.json.JSONObject;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.Volley;
public class MainActivity extends Activity {
private TextView txtDisplay;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
txtDisplay = (TextView) findViewById(R.id.txtDisplay);
RequestQueue queue = Volley.newRequestQueue(this);
String url = "http://echo.jsontest.com/key/value/one/two";
JsonObjectRequest jsObjRequest = new JsonObjectRequest(Request.Method.GET, url, null, new Response.Listener() {
@Override
public void onResponse(JSONObject response) {
// TODO Auto-generated method stub
txtDisplay.setText("Response => "+response.toString());
findViewById(R.id.progressBar1).setVisibility(View.GONE);
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
// TODO Auto-generated method stub
}
})
/*{ // if you have any key value request
@Override
protected MapgetParams()
{
Mapparams = new HashMap ();
params.put("name", "sunil");
params.put("domain", "http://sunil.info");
return params;
}
}*/;
queue.add(jsObjRequest);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
Update Contact :
No Wa/Telepon (puat) : 085267792168
No Wa/Telepon (fajar) : 085369237896
Email : Fajarudinsidik@gmail.com
No Wa/Telepon (puat) : 085267792168
No Wa/Telepon (fajar) : 085369237896
Email: Fajarudinsidik@gmail.com
atau Kirimkan Private messanger melalui email dengan klik tombol order dibawah ini :