Tuesday 24 April 2018

Volley Json Request for api call Demo

Hi first of all add dependency on gradle file

//Volley libsimplementation 'com.android.volley:volley:1.0.0'implementation 'org.apache.httpcomponents:httpcore:4.4.1'

Add the below part on app build.gradle inside android { } block,

//Volley add linesuseLibrary 'org.apache.http.legacy'packagingOptions {
    exclude 'META-INF/DEPENDENCIES.txt'    exclude 'META-INF/LICENSE.txt'    exclude 'META-INF/NOTICE.txt'    exclude 'META-INF/NOTICE'    exclude 'META-INF/LICENSE'    exclude 'META-INF/DEPENDENCIES'    exclude 'META-INF/notice.txt'    exclude 'META-INF/license.txt'    exclude 'META-INF/dependencies.txt'    exclude 'META-INF/LGPL2.1'
}

-> Create a LruBitmapCache.java class 

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

import com.android.volley.toolbox.ImageLoader.ImageCache;

public class LruBitmapCache extends LruCache<String, Bitmap> implements ImageCache {
   public static int getDefaultLruCacheSize() {
      final int maxMemory = (int) (Runtime.getRuntime().maxMemory() / 1024);
      final int cacheSize = maxMemory / 8;

      return cacheSize;
   }

   public LruBitmapCache() {
      this(getDefaultLruCacheSize());
   }

   public LruBitmapCache(int sizeInKiloBytes) {
      super(sizeInKiloBytes);
   }

   @Override   protected int sizeOf(String key, Bitmap value) {
      return value.getRowBytes() * value.getHeight() / 1024;
   }

   @Override   public Bitmap getBitmap(String url) {
      return get(url);
   }

   @Override   public void putBitmap(String url, Bitmap bitmap) {
      put(url, bitmap);
   }
}

-> Create MyApplication class

import android.app.Application;
import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.Signature;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.text.TextUtils;
import android.util.Base64;
import android.util.Log;

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

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;


/** * Created by msp on 6/7/16. */public class MyApplication extends Application {
   private static MyApplication appInstance;

   private static Context mContext;

   private static final String KEY_REMEBER = "REMEMBER";
   public static final String TAG = MyApplication.class.getSimpleName();
   private static final String KEY = "eCommerce";
   private RequestQueue mRequestQueue;
   private ImageLoader mImageLoader;

   @Override   public void onCreate() {
      super.onCreate();
      appInstance = this;
      MyApplication.mContext = getApplicationContext();

   }

   public static boolean isConnectivityAvailable(Context context) {
      ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);

      NetworkInfo activeNetwork = connectivityManager.getActiveNetworkInfo();
      return activeNetwork != null && activeNetwork.isConnectedOrConnecting();
   }

   public static Context getContext() {
      return mContext;
   }

   public static void setContext(Context mctx) {
      mContext = mctx;
   }



   public static synchronized MyApplication getInstance() {
      return appInstance;
   }




   // for get getRequestQueue object   public RequestQueue getRequestQueue() {
      if (mRequestQueue == null) {
         mRequestQueue = Volley.newRequestQueue(getApplicationContext());
      }

      return mRequestQueue;
   }

   // for get imageloader object   public com.android.volley.toolbox.ImageLoader getImageLoader() {
      getRequestQueue();
      if (mImageLoader == null) {
         mImageLoader = new com.android.volley.toolbox.ImageLoader(this.mRequestQueue, new LruBitmapCache());
      }
      return this.mImageLoader;
   }

   // for add request to queue   public <T> void addToRequestQueue(Request<T> req, String tag) {
      // set the default tag if tag is empty      req.setTag(TextUtils.isEmpty(tag) ? TAG : tag);
      getRequestQueue().add(req);
   }

   // for add request queue   public <T> void addToRequestQueue(Request<T> req) {
      req.setTag(TAG);
      getRequestQueue().add(req);
   }

   //for cancle pending request   public void cancelPendingRequests(Object tag) {
      if (mRequestQueue != null) {
         mRequestQueue.cancelAll(tag);
      }
   }

   //for get keyhash of facebook   public void printKeyHash() {
      try {
         PackageInfo info = getPackageManager().getPackageInfo("com.weddingapp", PackageManager.GET_SIGNATURES);
         for (Signature signature : info.signatures) {
            MessageDigest md = MessageDigest.getInstance("SHA");
            md.update(signature.toByteArray());
            Log.e("NightLife", Base64.encodeToString(md.digest(), Base64.DEFAULT));
         }
      } catch (PackageManager.NameNotFoundException e) {

      } catch (NoSuchAlgorithmException e) {

      }
   }
}

-- Register application class in android Manifest file
ex - 

    android:name=".volley.MyApplication"


-> Create custom_dialog.xml

<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:background="#00000000"    android:orientation="vertical" >

    <ProgressBar        android:id="@+id/progressBar1"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_centerHorizontal="true"        android:layout_centerVertical="true" />

</RelativeLayout>

-> Create