安卓网络编程——使用HttpURLConnection

来源:互联网 发布:梦幻西游mac安装打不开 编辑:程序博客网 时间:2024/05/16 06:41

HttpURLConnection

它是URLConnection的子类,可见HttpURLConnection在父类的基础上做了改进,从名称也就可以看出增加了一些用于操作Http资源的便捷方法。

实例-访问网络图片

1,效果图:
这里写图片描述

2,xml布局,包括ImageView 。Button。EditText控件

3,开发步骤:

1,获取控件对象2,button按钮设置监听器,方法体中启动线程(必须使用线程访问网络)。3,在线程run方法体中,获取并创建URL对象,判断不为空。        不为空,则取得HttpURLContent对象,在判断响应吗为200,若是则用输入流读取图片并封装到Bitmap对象。4,若上步成功,则使用handler通知界面主线程中ImageView设置此网络图片。

编程代码:

public class MainActivity extends Activity {    private Button button =null;    private ImageView image =null;    private EditText edit =null;    private Bitmap bitmap =null;//读取的网络图片    private Handler handler =null;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        //初始化控件        InitView();        //添加按钮监听器        button.setOnClickListener(new OnClickListener() {            @Override            public void onClick(View v) {                // TODO Auto-generated method stub                //启动线程和网络资源交互,并使用handler与主线程界面交互                new Thread(new httprun()).start();            }        });        //Handler        handler =new Handler(){            @Override            public void handleMessage(Message msg) {                // TODO Auto-generated method stub                if(msg.what ==1){                    image.setImageBitmap(bitmap);                }            }        };    }    private void InitView() {        // TODO Auto-generated method stub        button=(Button)findViewById(R.id.btnView);        image=(ImageView)findViewById(R.id.ivImage);        edit=(EditText)findViewById(R.id.etImageUrl);    }    @Override    public boolean onCreateOptionsMenu(Menu menu) {        // Inflate the menu; this adds items to the action bar if it is present.        getMenuInflater().inflate(R.menu.main, menu);        return true;    }    //访问网络图片的线程    class httprun implements Runnable{        @Override        public void run() {            // TODO Auto-generated method stub            String str_url =edit.getText().toString().trim();            if(str_url!=null) {                try {                    URL url =new URL(str_url);                    HttpURLConnection hConnection =(HttpURLConnection) url.openConnection();//获得网络连接对象                    hConnection.setConnectTimeout(5000);                    hConnection.setRequestMethod("GET");                    if(hConnection.getResponseCode() ==200){                        InputStream in=hConnection.getInputStream();//获得输入流对象                        //通过bitmap来封装图片                        bitmap =BitmapFactory.decodeStream(in);                        //通过handler通知主界面显示图片                        handler.sendEmptyMessage(1);                    }else {                        Toast.makeText(MainActivity.this, "读取图片失败!", 1000).show();                    }                } catch (MalformedURLException e) {                    // TODO Auto-generated catch block                    e.printStackTrace();                } catch (IOException e) {                    // TODO Auto-generated catch block                    e.printStackTrace();                }            }else {                Toast.makeText(MainActivity.this, "url不能为空!", 3000).show();            }        }    }}
0 0
原创粉丝点击