Android---Using android-async-http Open source project to implement network picture Viewer

Source: Internet
Author: User

1. Guide Package: Import the latest version of the android-async-http Open Source project Package

2. Simple to build a Web Image Viewer interface

The relevant interface building code:

<relativelayout xmlns:android= "http://schemas.android.com/apk/res/android" xmlns:tools= "http// Schemas.android.com/tools "android:layout_width=" match_parent "android:layout_height=" Match_parent "Tools:context = "${packagename}.${activityclass}" > <imageview android:id= "@+id/iv_img" android:layout_width= "wrap _content "android:layout_height=" 300DP "android:layout_alignparentleft=" true "Android:layout_alignpar Entright= "true" android:layout_alignparenttop= "true" android:src= "@drawable/ic_launcher"/> <editte        XT android:id= "@+id/et_url" android:layout_width= "match_parent" android:layout_height= "Wrap_content" Android:layout_alignparentbottom= "true" android:layout_alignparentleft= "true" Android:layout_marginbo Ttom= "60DP" android:inputtype= "Texturi" android:text= "@string/et_url"/> <button android:id= " @+id/btn_img "Android:layout_width=" MaTch_parent "android:layout_height=" Wrap_content "android:layout_alignparentbottom=" true "Android:layo Ut_alignparentleft= "true" android:onclick= "sendgetimg" android:text= "@string/btn_img"/></relativelayo Ut>


This is just a way to use an absolute layout, or to lay out a relative layout.

3. Set Network permissions

<uses-permission android:name= "Android.permission.INTERNET"/>

4. Creating an Asynchronous client object

5. Get the URI network path

6. Execute a GET request

a) if the factory object is created successfully

b) The Decodebytearray of the factory object converts bytes into bitmap objects

c) set the picture

7. Project Analysis:

Click on the Web image to view a picture of the address you have filled out.

Related program code:

Package Www.csdn.net.lesson05_02;import Org.apache.http.header;import Android.app.activity;import Android.graphics.bitmap;import Android.graphics.bitmapfactory;import Android.os.bundle;import Android.view.View; Import Android.widget.edittext;import Android.widget.imageview;import Android.widget.toast;import Com.loopj.android.http.asynchttpclient;import Com.loopj.android.http.asynchttpresponsehandler;public Class Mainactivity extends Activity {private EditText et_url;private ImageView iv_img; @Overrideprotected void OnCreate (Bundle Savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (R.layout.activity_main); Et_url= (EditText ) Findviewbyid (R.id.et_url); iv_img= (ImageView) Findviewbyid (r.id.iv_img);} public void sendgetimg (View v) {int id=v.getid (), switch (ID) {Case r.id.btn_img://gets the network path string Url=et_url.gettext (). ToString ();//Create client Object Asynchttpclient client=new asynchttpclient (); Client.get (URL, new Asynchttpresponsehandler () {@ overridepublic void onsuccess (int statusCode, header[] headers, byte[] responsebody) {if (statuscode==200) {bitmapfactory factory=new bitmapfactory (); Bitmap Bitmap=factory.decodebytearray (responsebody, 0, responsebody.length); Iv_img.setimagebitmap (Bitmap);}} @Overridepublic void onfailure (int statusCode, header[] headers,byte[] responsebody, throwable error) { Error.printstacktrace ();}}); Break;default:break;}}}

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.