Android Neutron thread Network viewer with handler message Processor

Source: Internet
Author: User

XML File Code section

<linearlayout 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" android:orientation= "vertical"  >     <imageview        android:id= "@+id/image"          android:layout_width= "Fill_parent"          android:layout_height= "0dip"         android:layout_weight= "1"          />    <LinearLayout          android:layout_width= "Fill_parent"          android:layout_height= "Wrap_content"          android:orientation= "Horizontal"         >        < Edittext             android:id= "@+id/edit"             android:layout_width= "0dip"              android:layout_height= "Wrap_content"              android:layout_weight= "1"              android:singleline= "true"              android:text= "http://e.hiphotos.baidu.com/image/pic/item/ D52a2834349b033b6764dedb17ce36d3d439bdc6.jpg "             />        <Button              aNdroid:id= "@+id/go"             android:layout_ Width= "Wrap_content"             android:layout_ height= "Wrap_content"             android:text= "Go"             android:textsize= "20SP"              />    </LinearLayout> </LinearLayout>

Activity

package com.example.android01;import java.io.inputstream;import java.net.httpurlconnection; import java.net.url;import android.app.activity;import android.graphics.bitmap;import  android.graphics.bitmapfactory;import android.os.bundle;import android.os.handler;import  android.os.message;import android.view.view;import android.view.view.onclicklistener;import  android.widget.button;import android.widget.edittext;import android.widget.imageview;import  android.widget.toast;public class mainactivity extends activity implements  Onclicklistener {private imageview image;private edittext edit;private button  but;private final int success=0;private handler handler=new handler () {@ Overridepublic void handlemessage (android.os.message msg)  {bitmap b= (Bitmap) msg.obj; if (b!=null) {if (msg.what==success) {image.seTimagebitmap (b);}} Else{toast.maketext (Getapplicationcontext (),  "Get picture Error",  0). Show ();}};};      @Override     protected void oncreate (bundle  savedinstancestate)  {        super.oncreate (savedInstanceState);         setcontentview (R.layout.activity_main);         image= (ImageView)  findviewbyid (r.id.image);         edit= (EditText)  findviewbyid (r.id.edit);         but= (Button)  findviewbyid (r.id.go);         But.setonclicklistener (this);    &nbsp @Overridepublic  void onclick (view v)  {final string url=edit.gettext (). toString (); New thread (new runnable ()  {@ Overridepublic void run ()  {bitmap bit=getimagefromNet (URL);//image.setimagebitmap (bit); Message msg=new message (); Msg.obj=bit;msg.what=success;handler.sendmessage (msg);}}). Start ();} Private bitmap getimagefromnet (string url) { httpurlconnection conn=null;try {url  murl=new url (URL);//Create a URL connection  conn= (httpurlconnection)  murl.openconnection ();// Get a Connection object Conn.setrequestmethod ("get");//Set Request method is Getconn.setconnecttimeout (10000);// Set the connection timeout time conn.setreadtimeout (5000);//Set the exception Conn.connect () in the read process; Int responsecode=conn.getresponsecode ();// Get response code, 404,500,200IF (responsecode==200) {//Access succeeded Inputstream in=conn.getinputstream (); Bitmap bitmap=bitmapfactory.decodestream (in);//Convert the stream fetched from the server into a Bitmap bitmap return bitmap;} Else{toast.maketext (this,  "Get Picture Failed",  0). Show ();}}  catch  (exception e)  {e.printstacktrace ();} Finally{if (conn!=null) {conn.disconnect ();}} Return null;}   }


This article is from the "Java White Battlefield" blog, be sure to keep this source http://8023java.blog.51cto.com/10117207/1665704

Android Neutron thread Network viewer with handler message Processor

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.