android studio之简单使用HttoURLConnection

本文介绍了如何在Android Studio中使用HttpURLConnection进行基本的网络请求操作,包括建立连接、设置请求方法、发送数据及接收响应。通过实例代码详细讲解了网络请求的步骤,适合初学者学习。

    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);
            }
        });
    }
}

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值