溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

Android中子線程網(wǎng)絡(luò)查看器與Handler消息處理器

發(fā)布時間:2020-07-23 15:24:37 來源:網(wǎng)絡(luò) 閱讀:401 作者:Java大白 欄目:移動開發(fā)

xml文件代碼部分

<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/p_w_picpath"
        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="https://cache.yisu.com/upload/information/20200311/46/199580.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 p_w_picpath;
	private EditText edit;
	private Button but;
	private final int success=0;
	private Handler handler=new Handler(){
		@Override
		public void handleMessage(android.os.Message msg) {
			Bitmap b=(Bitmap)msg.obj;
			if(b!=null)
			{
				if(msg.what==success)
				{
			p_w_picpath.setImageBitmap(b);
				}
			}else{
				Toast.makeText(getApplicationContext(), "獲取圖片錯誤", 0).show();
			}
			
		};
	};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        p_w_picpath=(ImageView) findViewById(R.id.p_w_picpath);
        edit=(EditText) findViewById(R.id.edit);
        but=(Button) findViewById(R.id.go);
        but.setOnClickListener(this);
    }

	@Override
	public void onClick(View v) {
		final String url=edit.getText().toString();
		new Thread(new Runnable() {
			
			@Override
			public void run() {
				Bitmap bit=getImageFromNet(url);
//				p_w_picpath.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);//創(chuàng)建一個URL連接
			 conn=(HttpURLConnection) mURL.openConnection();//得到一個connection對象
			conn.setRequestMethod("GET");//設(shè)置請求方法為GET
			conn.setConnectTimeout(10000);//設(shè)置連接超時時間
			conn.setReadTimeout(5000);//設(shè)置讀取過程中的異常
			conn.connect();
			int responseCode=conn.getResponseCode();//獲取響應(yīng)碼,404,500,200
			if(responseCode==200){
				//訪問成功
				InputStream in=conn.getInputStream();
			Bitmap bitmap=	BitmapFactory.decodeStream(in);//將從服務(wù)器獲取的流變成Bitmap位圖
			return bitmap;
			}else{
				Toast.makeText(this, "獲取圖片失敗", 0).show();
			}
			
			
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			if(conn!=null)
			{
				conn.disconnect();
			}
		}
		return null;
	}

  
}


向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI