Android uses volley concrete examples to show the complete asynchronous load data (i)

Source: Internet
Author: User

Mainactivity such as the following:
Package Cc.cn;import Java.util.hashmap;import Org.json.jsonobject;import android.app.activity;import Android.content.context;import Android.os.bundle;import Com.android.volley.authfailureerror;import Com.android.volley.requestqueue;import Com.android.volley.response;import Com.android.volley.volleyerror;import Com.android.volley.request.method;import Com.android.volley.toolbox.jsonobjectrequest;import Com.android.volley.toolbox.stringrequest;import com.android.volley.toolbox.volley;/** * Demo Description: * Last year a project simple use of volley, see: * http://blog.csdn.net/lfdfhl/article/details/12223345 * Recent Guolin wrote a few articles specifically introduced volley, So here's a more systematic way to learn. * In this demo sample, 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 &L T;uses-permission android:name= "Android.permission.INTERNET"/> * * Learning materials: * http://blog.csdn.net/guolin_blog/ article/details/17482095 * Thank you very much * */public class Mainactivity extends Activity {private Requestqueue Mreque Stqueue;private 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= "http://www.baidu.com/";    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); }//implement POST request with volley private void VoLley_post () {String URL = "Http://hxyj.cwwic.cn/wap/api_cs/user.php?action=login"; mcontext = This;mrequestqueue = Volley.newrequestqueue (mcontext); mstringrequest = new Stringrequest (method.post, 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 the number of @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 = "http://m.weather.com.cn/data/101010100.html";//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 such as the following:

<?xml version= "1.0" encoding= "Utf-8"?

><relativelayout xmlns:android= "http://schemas.android.com/apk/res/android" android:layout_width= " Fill_parent " android:layout_height=" fill_parent " android:orientation=" vertical "> <textview android:layout_width= "wrap_content" android:layout_height= "wrap_content" android:text= "learning Volley (a ) " android:layout_centerinparent=" true " /></relativelayout>



Copyright notice: This article Bo Master original articles, blogs, without consent may not be reproduced.

Android uses volley concrete examples to show the complete asynchronous load data (i)

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: info-contact@alibabacloud.com 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.