Android Volley is a very useful third-party open Source network communication framework on Android platforms. Use simple answer, powerful.
Android Volley library jar package Volley.jar released for everyone to download and use, download connection address: Volley download
The downloaded Volley.jar is added directly to the project's libs and can be used
Use the following code:
Activity_main.xml:
1 <LinearLayoutxmlns:android= "Http://schemas.android.com/apk/res/android"2 Xmlns:tools= "Http://schemas.android.com/tools"3 Android:layout_width= "Match_parent"4 Android:layout_height= "Match_parent"5 android:orientation= "vertical"6 Tools:context= "Com.zzw.testvolley.MainActivity" >7 8 <TextView9 Android:id= "@+id/textview"Ten Android:layout_width= "Match_parent" One Android:layout_height= "Wrap_content" A Android:layout_weight= "1" - Android:scrollbars= "vertical" - Android:singleline= "false" the Android:text= "@string/hello_world" /> - - <ImageView - Android:id= "@+id/imageview" + Android:layout_width= "Match_parent" - Android:layout_height= "Wrap_content" + Android:layout_weight= "1" /> A at </LinearLayout>
Activity_main.xml
Mainactivity.java:
1 PackageCom.zzw.testvolley;2 3 ImportCom.android.volley.RequestQueue;4 ImportCom.android.volley.Response.ErrorListener;5 ImportCom.android.volley.Response.Listener;6 ImportCom.android.volley.VolleyError;7 Importcom.android.volley.toolbox.ImageRequest;8 Importcom.android.volley.toolbox.StringRequest;9 ImportCom.android.volley.toolbox.Volley;Ten One Importandroid.app.Activity; A ImportAndroid.graphics.Bitmap; - ImportAndroid.graphics.Bitmap.Config; - ImportAndroid.os.Bundle; the ImportAndroid.text.method.ScrollingMovementMethod; - ImportAndroid.view.Menu; - ImportAndroid.view.MenuItem; - ImportAndroid.widget.ImageView; + ImportAndroid.widget.TextView; - ImportAndroid.widget.Toast; + A Public classMainactivityextendsActivity { at - @Override - protected voidonCreate (Bundle savedinstancestate) { - Super. OnCreate (savedinstancestate); - Setcontentview (r.layout.activity_main); -TextView TextView =(TextView) Findviewbyid (R.id.textview); in //Setting the drop-down - Textview.setmovementmethod (Scrollingmovementmethod.getinstance ()); to +ImageView ImageView =(ImageView) Findviewbyid (R.id.imageview); - theUsevolleygetstring ("http://www.cnblogs.com/zzw1994", TextView); *Usevolleygetimage ("Http://pic.cnblogs.com/avatar/822717/20151120000857.png", ImageView); $ Panax Notoginseng } - the Private voidusevolleygetstring (String URL, TextView TextView) { + A FinalTextView Mtextview =TextView; the + //first step, get volley request -Requestqueue Requestqueue =Volley.newrequestqueue (Getapplicationcontext ()); $ $ //Step two, get stringrequest. -Stringrequest stringrequest =NewStringrequest (URL,NewListener<string>() { - //data settings returned after a successful request the @Override - Public voidOnresponse (String response) {Wuyi Mtextview.settext (response); the } -},NewErrorlistener () { Wu //data settings returned after a request fails - @Override About Public voidonerrorresponse (volleyerror error) { $Toast.maketext (Getapplicationcontext (), "Failed to load", 1). Show (); - } - }); - A //step three, add to Requestqueue + Requestqueue.add (stringrequest); the } - $ /* the * Loading images suggested using glide the * Glide:http://www.cnblogs.com/zzw1994/p/4978312.html the */ the - Private voidusevolleygetimage (String URL, ImageView ImageView) { in the FinalImageView Mimageview =ImageView; the About //first step, get volley request theRequestqueue Requestquene =Volley.newrequestqueue (Getapplicationcontext ()); the the //Step two, get imagerequest. +Imagerequest imagerequest =Newimagerequest (URL, - //data settings returned after a successful request the NewListener<bitmap>() {Bayi @Override the Public voidOnresponse (Bitmap response) { the Mimageview.setimagebitmap (response); - } -}, 0, 0, config.rgb_565,NewErrorlistener () { the //data settings returned after a request fails the @Override the Public voidonerrorresponse (volleyerror error) { theToast.maketext (Getapplicationcontext (), "Failed to load", 1). Show (); - } the }); the the //step three, add to Requestquene94 Requestquene.add (imagerequest); the } the}
Finally, don't forget to add permissions in Androidmanifest.xml:
1 < android:name= "Android.permission.INTERNET"/>
Powerful network communication framework--third party open source--volley