android原生http,安卓原生HTTP协议访问网络

1xml内容

xmlns:app="http://schemas.android.com/apk/res-auto"

xmlns:tools="http://schemas.android.com/tools"

android:layout_width="match_parent"

android:layout_height="match_parent">

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="Hello World!"

app:layout_constraintBottom_toBottomOf="parent"

app:layout_constraintHorizontal_bias="0.047"

app:layout_constraintLeft_toLeftOf="parent"

app:layout_constraintRight_toRightOf="parent"

app:layout_constraintTop_toTopOf="parent"

app:layout_constraintVertical_bias="0.066"

android:id="@+id/response_text"/>

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:id="@+id/button_http"

android:text="按钮啊"

/>

java内容

package com.example.helloworld;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;

import android.util.Log;

import android.view.View;

import android.webkit.WebView;

import android.webkit.WebViewClient;

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.MalformedURLException;

import java.net.URL;

public class Main3Activityextends AppCompatActivityimplements View.OnClickListener{

TextViewresponseText;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main3);

Button sendRequest = (Button) findViewById(R.id.button_http);

responseText = (TextView) findViewById(R.id.response_text);

sendRequest.setOnClickListener(this);

//        WebView webView = (WebView) findViewById(R.id.web_view);

//        webView.getSettings().setJavaScriptEnabled(true);

//        webView.setWebViewClient(new WebViewClient());

//        webView.loadUrl("Https://www.baidu.com");

}

public void onClick(View v) {

if (v.getId()==R.id.button_http){

Log.d("dean","点击事件");

sendRwq();

}

}

private void sendRwq(){

Log.d("dean","发送事件");

new Thread(new Runnable(){

public void run() {

HttpURLConnection connection =null;

BufferedReader reader =null;

try {

URL url =new URL("https://www.baidu.com");

connection = (HttpURLConnection) url.openConnection();

connection.setConnectTimeout(5000);

connection.setReadTimeout(5000);

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 (MalformedURLException e) {

e.printStackTrace();

}catch (IOException e) {

e.printStackTrace();

}

}

}).start();

}

private void showResponse(final String response){

Log.d("dean","response");

runOnUiThread(new Runnable() {

@Override

public void run() {

responseText.setText(response);

}

});

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值