22 October 2014

Android Volley library json parse example

In this post i will describe how to use android volley library for your json request.First you have to download android volley library.I have uploaded this sample projcet to github so you can download volley.jar file from here(https://github.com/sajith4u/Android_volley_Sample/tree/master/Android_Volley/libs).Or you can download from here(https://drive.google.com/?tab=jo&authuser=0#folders/0B6boUJ62bUIXZUJQbUdoS08tMms).First You have to add AppController class to your package.Befor that make sure you have put volley.jar file in to your lib folder.
package com.example.Android_Volley;

import android.app.Application;

import android.text.TextUtils;

import com.android.volley.Request;

import com.android.volley.RequestQueue;

import com.android.volley.toolbox.Volley;


public class AppController extends Application {

    public static final String TAG = AppController.class.getSimpleName();

    private RequestQueue mRequestQueue;

    private static AppController mInstance;

    @Override

    public void onCreate() {
        super.onCreate();
        mInstance = this;
    }

    public static synchronized AppController getInstance() {
        return mInstance;
    }

    public RequestQueue getRequestQueue() {

        if (mRequestQueue == null) {
            mRequestQueue = Volley.newRequestQueue(getApplicationContext());
        }

        return mRequestQueue;

    }

    public <T> void addToRequestQueue(Request<T> req, String tag) {
        req.setTag(TextUtils.isEmpty(tag) ? TAG : tag);
        getRequestQueue().add(req);
    }


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

    public void cancelPendingRequests(Object tag) {

        if (mRequestQueue != null) {
            mRequestQueue.cancelAll(tag);
        }
    }

}

Then You have to Add this class to your manifest file.So inside the Application tag put the name of class.
<application android:label="@string/app_name"

                 android:icon="@drawable/ic_launcher"

                 android:name="com.example.Android_Volley.AppController">

        <activity android:name="MyActivity"

                  android:label="@string/app_name">

            <intent-filter>

                <action android:name="android.intent.action.MAIN"/>

                <category android:name="android.intent.category.LAUNCHER"/>

            </intent-filter>

        </activity>

    </application>

And also make shure add internet permission in to your application.
 <uses-permission android:name="android.permission.INTERNET"/>

Then inside main activity i have one button and when i click button it will read json object and show in to textview.I have used this public json call to show demo.(http://time.jsontest.com/) it will return date, time and other parameter inside json object.

date:- String
mili seconds :- Double
time:- String

when button clicked it called to this method.
   

    private void makeJsonObjectRequest() {

        showpDialog();

       JsonObjectRequest jsonObjReq = new JsonObjectRequest(Method.GET,

                urlJsonObj, null, new Response.Listener<JSONObject>() {

            @Override

            public void onResponse(JSONObject response) {

                try {                    

                 String time = response.getString("time");

                 Double mili = response.getDouble("milliseconds_since_epoch");

                 String date = response.getString("date");

                 jsonResponse = "";

                 jsonResponse += "Time: " + time + "\n\n";

                 jsonResponse += "seconds: " + mili + "\n\n";

                 jsonResponse += "Date: " + date + "\n\n";

                 txtResponse.setText(jsonResponse);

                } catch (JSONException e) {

                    e.printStackTrace();

                    Toast.makeText(getApplicationContext(),

                            "Error: " + e.getMessage(),

                            Toast.LENGTH_LONG).show();
                }

                hidepDialog();

            }

        }, new Response.ErrorListener() {

            @Override

            public void onErrorResponse(VolleyError error) {

            Toast.makeText(getApplicationContext(),

                   error.getMessage(), Toast.LENGTH_SHORT).show();                

                hidepDialog();
            }

        });        

        AppController.getInstance().addToRequestQueue(jsonObjReq);

    }


In this method hideDialog and showDialog methods used to add progressDialog.



No comments:

Post a Comment