Use the open source framework volley to download text and images.

Source: Internet
Author: User

Android Volley is a very useful third-party open Source network communication framework on Android platforms. Easy to use and powerful.

Download Connection address: http://download.csdn.net/detail/zhangphil/9053413

How to use: After downloading Volley.jar, put it directly to the Eclipse Project's libs directory, refresh, and then you can call Android volley.

The code is as follows:

1  PackageCom.lixu.load_txt_image;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.widget.ImageView; - ImportAndroid.widget.TextView; - ImportAndroid.widget.Toast; +  -  Public classMainactivityextendsActivity { +     PrivateTextView TV; A     PrivateImageView IV; at  - @Override -  -     protected voidonCreate (Bundle savedinstancestate) { -         Super. OnCreate (savedinstancestate); - Setcontentview (r.layout.activity_main); in  -TV =(TextView) Findviewbyid (r.id.tv); to         //to set the scroll bar for TextView + Tv.setmovementmethod (Scrollingmovementmethod.getinstance ()); -  theIV =(ImageView) Findviewbyid (R.ID.IV); *         //Web address for text and pictures $String URL1 = "http://www.baidu.com";Panax Notoginseng  -String url2 = "https://ss3.bdstatic.com/70cFv8Sh_Q1YnxGkpoWK1HF6hhy/it/u=1183223528,3058066243&fm=116&gp= 0.jpg "; the  + Loadtxt (URL1); A  the LoadImage (URL2); +  -     } $  $     Private voidloadtxt (String url1) { -         //get the Requestqueue object from Volley -Requestqueue queue =Volley.newrequestqueue (Getapplicationcontext ()); the  -         //Create StringrequestWuyistringrequest sr =NewStringrequest (URL1,NewListener<string>() { the             //Get String type text after success - @Override Wu              Public voidOnresponse (String response) { - Tv.settext (response); About             } $},NewErrorlistener () { -             //get into after failure - @Override -              Public voidonerrorresponse (volleyerror error) { AToast.maketext (Getapplicationcontext (), "Error!! ", 0). Show (); +             } the         }); -         //add stringrequest to Requestqueue $ Queue.add (SR); the     } the  the     Private voidLoadImage (String url2) { theRequestqueue queue =Volley.newrequestqueue (Getapplicationcontext ()); -  inImagerequest IR =NewImagerequest (URL2,NewListener<bitmap>() { the             //Get a picture of the bitmap type after success. 0, 0, set picture size to actual size, argb_8888, set picture format for HD large image the @Override About              Public voidOnresponse (Bitmap response) { the Iv.setimagebitmap (response); the             } the}, 0, 0, config.argb_8888,NewErrorlistener () { +             //get into after failure - @Override the              Public voidonerrorresponse (volleyerror error) {BayiToast.maketext (Getapplicationcontext (), "Error!! ", 0). Show (); the             } the         }); -         //add stringrequest to Requestqueue - Queue.add (IR); the     } the  the}

XML file:

1 <LinearLayoutxmlns:android= "Http://schemas.android.com/apk/res/android"2 Xmlns:tools= "Http://schemas.android.com/tools"3 Android:id= "@+id/linearlayout1"4 Android:layout_width= "Match_parent"5 Android:layout_height= "Match_parent"6 android:orientation= "vertical" >7 8     <ScrollView9         Android:layout_width= "Match_parent"Ten Android:layout_height= "Match_parent" One Android:layout_weight= "1" > A  -         <TextView -             Android:id= "@+id/tv" the Android:layout_width= "Match_parent" - Android:layout_height= "Match_parent" /> -     </ScrollView> -  +     <ImageView -         Android:id= "@+id/iv" + Android:layout_width= "Match_parent" A Android:layout_height= "Match_parent" at Android:layout_weight= "1" /> -  - </LinearLayout>

Run:

Use the open source framework volley to download text and images.

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.