Android学习第十六天----http请求

点击屏幕中的按钮,然后就获取到图片在页面中显示出来

<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"
    tools:context=".MainActivity" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="请输入地址" />

    <EditText
        android:id="@+id/editText1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="http://img1.cache.netease.com/cnews/2013/3/25/2013032508350944ddd.jpg" >
    </EditText>

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="点" />

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/ic_launcher" />

</LinearLayout>

java代码

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.ProtocolException;
import java.net.URL;



public class HttpService1 {
    
    public static byte[] readImage(String path) throws IOException
    {
        InputStream is=null;
        URL url = new URL(path); 
        
        HttpURLConnection conn= (HttpURLConnection) url.openConnection();
        
        conn.setConnectTimeout(5000);
        
        conn.setRequestMethod("GET");
        
        byte [] b =null;
        if(conn.getResponseCode()==200)
        {
            is=conn.getInputStream();
            long start=System.currentTimeMillis();
            byte b1[] = new byte[999999999];
            b= new byte[is.available()];
            is.read(b);
            is.close();
            long  finish=System.currentTimeMillis();
            
            System.out.println(finish-start);
        }
        return b;
    }
    
//    public static byte[] getByte(InputStream is) throws IOException
//    {
//        long start=System.currentTimeMillis();
//        ByteArrayOutputStream baos=new ByteArrayOutputStream();
//        byte [] b= new byte[1024];
//        int end=0;
//        while((end=is.read(b))!=-1)
//        {
//            baos.write(b);
//        }
//        is.close();
//        long  finish=System.currentTimeMillis();
//        
//        System.out.println(finish-start);
//        return baos.toByteArray();
//    }
    
    
}
import java.io.IOException;

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 cn.core.service.HttpService1;

public class MainActivity extends Activity {

    private EditText mEditText=null;
    private ImageView mImageView=null;
    private Button mButton=null;
    private Handler handler = new Handler(){
        public void handleMessage(Message msg) {
        byte[] b=null;
        try {
            b = HttpService1.readImage(mEditText.getText().toString());
        } catch (IOException e) {
            e.printStackTrace();
        }
        
        Bitmap bmp=BitmapFactory.decodeByteArray(b, 0, b.length);
        mImageView.setImageBitmap(bmp);
        };
    };
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        
        mButton=(Button) findViewById(R.id.button1);
        mEditText=(EditText)findViewById(R.id.editText1);
        mImageView=(ImageView)findViewById(R.id.imageView1);
        mButton.setOnClickListener(new OnClickListener() {
            
            @Override
            public void onClick(View v) {
                new Thread(new Runnable() {
                    
                    @Override
                    public void run() {
                    Message message = new Message();
                    handler.sendMessage(message);
                    }
                }).start();
            }
        });
    }
}

 

posted @ 2013-03-25 23:04  小三小山  阅读(153)  评论(0编辑  收藏  举报