URLConnection

URL的openConnection()方法将返回一个URLConnection对象,该对象表示应用程序和URL之间的连接;
程序可以通过该URLConnection实例
【1】向URL发送请求
【2】读取URL引用的资源
步骤如下
【1】调用URL对象openConnection()得到URLConnection对象
【2】设置URLConnection的参数和请求属性
【3】如果只是发送GET请求,建立连接即可;POST方式请求,需要获取URLConnection实例对应的输出流来发送参数
【4】远程资源变为可用,程序可以访问远程资源的头字段,或通过输入流获取资源;

举例获取输入流对象,读取网络;

public class MainActivity extends Activity implements View.OnClickListener {

    private static final int URL_CONNECT=0x2154;

    private TextView textView;
    private Button button_connection;

    private Handler handler=new Handler(){

        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what){


                case URL_CONNECT:
                    String message=msg.obj.toString();
                    textView.setText(message);

                    break;
                default:
                    break;
            }

        }
    };



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        button_connection= (Button) findViewById(R.id.button_connection);
        button_connection.setOnClickListener(this);

        textView= (TextView) findViewById(R.id.textview);
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.button_connection:
                //新开线程
                new Thread(){
                    @Override
                    public void run() {
                        connectServerlet();

                    }
                };

                break;
            default:
                break;

        }

    }

    private void connectServerlet() {

        try {
            //生成url对象
            URL url = new URL("http://www.360.com");
            //通过url的openConnection()方法得到URLConnection对象
            URLConnection connection=url.openConnection();
            //getInputStream()方法获取输入流
            InputStream is=connection.getInputStream();
            BufferedReader br=new BufferedReader(new InputStreamReader(is));

            String line=br.readLine();
            StringBuffer buffer=new StringBuffer();
            while (line!=null){
                buffer.append(line);
                line=br.readLine();
            }
            //通过handler传递信息
            Message msg=handler.obtainMessage();
            msg.what=URL_CONNECT;
            msg.obj=buffer.toString().trim();
            handler.sendMessage(msg);
            br.close();
            is.close();

        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值