1xml内容
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
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="按钮啊"
/>
</androidx.constraintlayout.widget.ConstraintLayout>
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);
}
});
}
}
网友评论