Mainactivity as follows:
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: * A simple use of volley in a project last year, see: * http://blog.csdn.net/lfdfhl/article/details/12223345 * 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: * http://blog.csdn.net/guolin_blog/article /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= "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); }//Use Volley to implement POST request 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 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 = "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 as follows:
<?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:o rientation= "vertical" > <textview android:layout_width= "wrap_content" android:layout_height= " Wrap_content " android:text=" learning Volley (i) " android:layout_centerinparent=" true " /></ Relativelayout>