Course Content

Course Content

Volley Example - Main Coding

Example for Volley library

 

Now in this video we are going to see an example for Volley in detail.

First of all we will open the 'MainActivity.java' and inside that provide the URL which we want to load directly from Internet. And then we will also use one progress dialog which will help us to understand better that how much progress has been updated.

First of all we will open the 'MainActivity.java' and inside that provide the URL which we want to load directly from Internet. And then we will also use one progress dialog which will help us to understand better that how much progress has been updated,

String Url="http://restcountries.eu/rest/v1/alpha/my";

ProgressDialog PD;

Now we will add the main code to our MainActivity.java file,

protected void onCreate(Bundle savedInstanceState)

{

super.onCreate(savedInstanceState);

setContentView (R.layout.activity_main);

tv=(TextView)findViewById(R.id.tv);

PD=new ProgressDialog(this);

PD.setMessage("Loading...");

PD.setCancelable(false);

}

private void makejsonobjreq()

{

PD.show();

JsonObjectRequest jsonobjreq=new JsonObjectRequest(Method,GET,url,null,new Response,Listener<JSONObject>()

{

@Override

public void onResponse(JSONObject response)

{

try {

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

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

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

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

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

PD.dismiss();

}

catch(JSONException e)

{

e.printStackTrace();

}

}

new Response.ErrorListener()

{

@Override

public void onErrorResponse(VolleyError error)

{

PD.dismiss();

}

}};

Now we will create another class named as 'MyApplication.java' and extend it with Application for requesting a Queue,

public class MyApplication extends Application

{

private RequestQueue mRequestQueue;

private static MyApplication mInstance;

@Override

public void onCreate()

{

super.onCreate();

mInstance=this;

}

public state synchronized MyApplication getnInstance

{

return mInstance;

}

public RequestQueue getReqQueue()

{

if(mRequestQueue==null)

{

mRequestQueue=Volley.newRequestQueue(getApplicationContext()};

}

return mRequestQueue;

}

public void cancelPendingReq(Object tag)

{

if (mRequestQueue!=null)

{

mRequestQueue.cancelAll(tag);

}

}

Now we will add MyApplication class to our MainActivity class,

MyApplication.getInstance().addToReqQueue(jsonObjReq,"jreq");

Now our code is complete and we will execute it now with a real device. Our code is running successfully and the content which we had in URL we provided is shown.

FAQs

Recommended Courses

Share With Friend

Have a friend to whom you would want to share this course?

Download LearnVern App

App Preview Image
App QR Code Image
Code Scan or Download the app
Google Play Store
297K+ Downloads
App Download Section Circle 1
4.5 Rating
App Download Section Circle 2
10K+ Reviews
App Download Section Circle 3
  • Learn anywhere on the go
  • Get regular updates about your enrolled or new courses
  • Share content with your friends
  • Evaluate your progress through practice tests
  • No internet connection needed
  • Enroll for the webinar and join at the time of the webinar from anywhere