Android uses Volley to load data asynchronously. Complete example (1)

Source: Internet
Author: User

MainActivity is 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 several articles detailing Volley, so here we will learn more systematically. * In this example, it mainly includes: * 1 using Volley to implement Get requests * 2 using Volley to implement Post requests * 3 using Volley to implement Json data requests ** note: * 1 introduce the jar package volley. jar * 2 network Permissions
 ** Learning materials: * http://blog.csdn.net/guolin_blog/article/details/17482095 * Thank you very much **/public class MainActivity extends Activity {private RequestQueue mRequestQueue; 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 ();} // use Volley to implement the private void volley_get () {mContext = this; string url = "http://www.baidu.com/"; // 1 create the RequestQueue object mRequestQueue = Volley. newRequestQueue (mContext); // 2 create the StringRequest object mStringRequest = new StringRequest (url, new Response. listener
 
  
() {@ Overridepublic void onResponse (String response) {System. out. println ("request Result:" + response) ;}, new Response. errorListener () {@ Overridepublic void onErrorResponse (VolleyError error) {System. out. println ("request error:" + error. toString () ;}}); // 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.p Hp? Action = login "; mContext = this; mRequestQueue = Volley. newRequestQueue (mContext); mStringRequest = new StringRequest (Method. POST, url, new Response. Listener
  
   
() {@ Overridepublic void onResponse (String response) {System. out. println ("request Result:" + response) ;}, new Response. errorListener () {@ Overridepublic void onErrorResponse (VolleyError error) {System. out. println ("request error:" + error. toString () ;}}) {// carry the parameter @ Overrideprotected HashMap
   
    
GetParams () throws AuthFailureError {HashMap
    
     
HashMap = new HashMap
     
      
(); HashMap. put ("un", "411111222"); hashMap. put ("pw", "180034111"); return hashMap ;}}; mRequestQueue. add (mStringRequest);} // use Volley to implement a Json data request private void volley_json () {mContext = this; String url = "http://m.weather.com.cn/data/101010100.html "; // 1 create the RequestQueue object mRequestQueue = Volley. newRequestQueue (mContext); // 2 create the JsonObjectRequest object mJsonObjectRequest = new JsonObjectRequest (url, null, new Response. listener
      
        () {@ 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 is as follows:

 
     
  
 


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.