Android leverages volley asynchronous load data Full Detail example (i)

Source: Internet
Author: User

Mainactivity as follows:
Package;import Java.util.hashmap;import Org.json.jsonobject;import;import Android.content.context;import Android.os.bundle;import;import;import;import;import;import;import;import;/** * Demo Description: * A simple use of volley in a project last year, see: * * Recently Guolin wrote a few articles detailed introduction volley, So here's a more systematic way to learn. * In this example, mainly includes: * 1 implement GET request with volley * 2 implement POST request with Volley * 3 implement JSON data request with volley * * NOTE: * 1 Introduction JAR Package Volley.jar * 2 Network Permissions < Uses-permission android:name= "Android.permission.INTERNET"/> * * Learning materials: * /details/17482095 * Thank you very much * */public class Mainactivity extends Activity {private Requestqueue Mrequestqueue ;p rivate StringRequest mstringrequest;private Context mcontext;private jsonobjectrequest mjsonobjectrequest;        @Override public void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);        Setcontentview (R.layout.main);        Volley_get ();        Volley_post ();    Volley_json ();    }//Implement GET request with volley private void Volley_get () {mcontext=this;    String url= "";    1 Create Requestqueue Object Mrequestqueue=volley.newrequestqueue (Mcontext); 2 Create stringrequest Object mstringrequest = new Stringrequest (url,new response.listener<string> () {@Overridepublic void Onresponse (String response) {System.out.println ("Request result:" +response);}, New Response.errorliste NER () {@Overridepublic void Onerrorresponse (volleyerror error) {System.out.println ("Request errors:" +error.t Ostring ());});    /3 Add Stringrequest to Requestqueuemrequestqueue.add (mstringrequest); }//Use Volley to implement POST request private void volley_post () {String URL = "Http://"; mcontext = This;mrequestqueue = Volley.newrequestqueue (mcontext); mstringrequest = new Stringrequest (, Url,new Response.Listener< String> () {@Overridepublic void Onresponse (String response) {System.out.println ("request Result:" + response);}}, New Response.errorlistener () {@Overridepublic void Onerrorresponse (volleyerror error) {System.out.println ("Request errors:" + Error.tostring ());}) {//Carry parameters @overrideprotected hashmap<string, string> getparams () throws Authfailureerror {hashmap<string, string> HashMap = new hashmap<string, string> (), Hashmap.put ("un", "411111222"), Hashmap.put ("PW", "180034111" ); return hashMap;}; Mrequestqueue.add (mstringrequest);} Use volley to implement JSON data request private void Volley_json () {mcontext = this; String url = "";//1 Create requestqueue Object mrequestqueue = Volley.newrequestqueue (Mcontext);//2 Create jsonobjectrequest Object mjsonobjectrequest = new JsonobjEctrequest (URL, null,new response.listener<jsonobject> () {@Overridepublic void Onresponse (Jsonobject Response) {SYSTEM.OUT.PRINTLN ("request Result:" + response.tostring ())}}, new Response.errorlistener () {@Overridepublic void Onerrorresponse (volleyerror error) {SYSTEM.OUT.PRINTLN ("Request error:" + error.tostring ());});            /3 Add Jsonobjectrequest to Requestqueuemrequestqueue.add (mjsonobjectrequest);} }

Main.xml as follows:

<?xml version= "1.0" encoding= "Utf-8"? ><relativelayout xmlns:android= " Android "    android:layout_width=" fill_parent "    android:layout_height=" fill_parent "    android:o rientation= "vertical" >    <textview        android:layout_width= "wrap_content"        android:layout_height= " Wrap_content "        android:text=" learning Volley (i) "         android:layout_centerinparent=" true "     /></ Relativelayout>

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.