UI文件
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.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"
tools:context=".MainActivity"
tools:layout_editor_absoluteY="81dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<EditText
android:id="@+id/et_url"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<Button
android:id="@+id/bt_look"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="click"
android:text="查看源代码" />
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/tv_src"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</ScrollView>
</LinearLayout>
</android.support.constraint.ConstraintLayout>
逻辑代码
package com.example.administrator.logincase;
import android.Manifest;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;
import java.io.BufferedInputStream;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import static android.content.pm.PackageManager.PERMISSION_DENIED;
import static android.content.pm.PackageManager.PERMISSION_GRANTED;
public class MainActivity extends AppCompatActivity {
private Handler handler = new Handler() {
@Override
public void handleMessage(Message msg) {
TextView srcView = findViewById(R.id.tv_src);
String strSrc = (String) msg.obj;
srcView.setText(strSrc);
super.handleMessage(msg);
}
};
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
switch (requestCode) {
case 0x11: {
if (grantResults[0] == PERMISSION_GRANTED) {
Toast.makeText(this, "网络权限请求成功",
Toast.LENGTH_SHORT).show();
return;
} else {
Toast.makeText(this, "网络权限请求失败",
Toast.LENGTH_SHORT).show();
return;
}
}
default:
break;
}
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (checkSelfPermission(Manifest.permission.INTERNET) == PERMISSION_DENIED) {
requestPermissions(new String[]{Manifest.permission.INTERNET}, 0x11);
}
}
public void click(View v) {
new Thread() {
@Override
public void run() {
EditText editText = findViewById(R.id.et_url);
String strUrl = editText.getText().toString().trim();
try {
URL url = new URL(strUrl);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
conn.setReadTimeout(5000);
if (conn.getResponseCode() != 200) {
Toast.makeText(MainActivity.this, "http请求错误",
Toast.LENGTH_SHORT).show();
return;
}
InputStream in = new BufferedInputStream(conn.getInputStream());
ByteArrayOutputStream baos = new ByteArrayOutputStream();
int nLen = -1;
byte[] buffers = new byte[1024];
while ((nLen = in.read(buffers)) != -1) {
baos.write(buffers, 0, nLen);
}
Message msg = new Message();
msg.obj = new String(baos.toByteArray());
handler.sendMessage(msg);
} catch (Exception e) {
e.printStackTrace();
}
}
}.start();
}
}
效果