Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Volley Tutorial

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 394
    Comment on it

    Here is a simple implementation of volley library in android ,

    Create a calls and extend Application class, implements the volley request queue in it also initialize the static class object in it.

    @Override
    public void onCreate() {
        super.onCreate();
        context = this;
        requestQueue = Volley.newRequestQueue(getApplicationContext());
    }
    

    Now create a static method that will returns the context of the class

    public static synchronized AppInitializer getContext() {
            return context;
        }
    

    Create a method that returns the singleton request queue

    /**
         * returns singleton request queue
         * @return
         */
        public RequestQueue getVolleyRequestQueu(){
            return requestQueue;
        }
    

    now in your mainifest add name to the application to the class name created here.

    <application
                android:allowBackup="true"
                android:name=".AppInitializer"
                android:icon="@mipmap/ic_launcher"
                android:label="@string/app_name"
                android:theme="@style/AppTheme" >
    

    Now create a class which will do network request using volley library and add the following method to it

    public void requestJSON() {
    
            JsonObjectRequest jsonObjReq = new JsonObjectRequest(Request.Method.GET,
                    url, null,
                    new Response.Listener<JSONObject>() {
    
                        @Override
                        public void onResponse(JSONObject response) {
                            Log.d(TAG, response.toString());
    
                            /**
                             * todo add your code which will be exicuted on resoponse
                             * or you can send as it is json to the method that called it
                             * like
                             */
    
                        }
                    }, new Response.ErrorListener() {
    
                @Override
                public void onErrorResponse(VolleyError error) {
                    VolleyLog.d(TAG, "Error: " + error.getMessage());
                    // hide the progress dialog
                    /**
                     * todo send call back some error message to show that an error occured while making request.
                     */
    
    
                }
            });
    
            // add this request to the volley queue to be executed.
    
            AppInitializer.getContext().getVolleyRequestQueu().add(jsonObjReq);
        }
    

    Call this method from where ever you want it will do the network request for you and fetch data in case of error it will go to the error block.

    Last but not the least add internet permission in the manifest file.

     <uses-permission android:name="android.permission.INTERNET" />
    

    Example files and volley library jar file is included along this blog.

    Do let me know if i forgot anything in comments

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: