android studio之简单使用HttoURLConnection

  • Post author:
  • Post category:其他


1.首先什么是HttoURLConnection,看它名字就知道和Http协议有关,我们都知道如果我们的app要嵌入一个搜索网站,只需要一个webview即可,其实webview是一个封装好的Http,并且还帮我们解释了一系列的html,而HttoURLConnection则是一个裸的Http,可以让我们更直观的理解http协议,其实http协议也就是向服务器发送一条请求,然后服务器再返回一系列的Html而已,在这个过程中我还学到一个BufferedReader读取字符串的神器= =(萌新)。
package com.example.gdzc.networktest;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

public class MainActivity extends AppCompatActivity implements View.OnClickListener{
    TextView textView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Button button=(Button)findViewById(R.id.button);
        textView=(TextView)findViewById(R.id.response);
        button.setOnClickListener(this);
    }
    public void onClick(View view)
    {
        if(view.getId()==R.id.button)
        {
            sendRequest();
        }
    }
    private void sendRequest()//发送请求
    {
        new Thread(new Runnable() {
            @Override
            public void run() {
                HttpURLConnection connection=null;
                BufferedReader reader=null;
                try{
                    URL url=new URL("https://www.baidu.com");
                    connection=(HttpURLConnection)url.openConnection();
                    connection.setRequestMethod("GET");
                    connection.setConnectTimeout(8000);
                    connection.setReadTimeout(8000);
                    InputStream in=connection.getInputStream();
                    reader=new BufferedReader(new InputStreamReader(in));
                    StringBuilder response=new StringBuilder();
                    String line;
                    while ((line=reader.readLine())!=null)
                    {
                        response.append(line);
                    }
                    showResponse(response.toString());
                }catch (Exception e)
                {
                    e.printStackTrace();
                }finally {
                    {
                        if(reader!=null){
                            try{
                                reader.close();
                            }catch (IOException e)
                            {
                                e.printStackTrace();
                            }
                        }
                        if(connection!=null)
                        {
                            connection.disconnect();
                        }
                    }
                }
            }
        }).start();
    }
    private void showResponse(final String response)
    {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                textView.setText(response);
            }
        });
    }
}



版权声明:本文为fbher原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。