parent
a295b4c140
commit
b2d354400a
@ -0,0 +1,100 @@
|
||||
package com.example.ceshi;
|
||||
|
||||
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;d
|
||||
|
||||
import android.app.Activity;
|
||||
import android.os.Bundle;
|
||||
import android.os.Handler;
|
||||
import android.os.Message;
|
||||
import android.view.View;
|
||||
import android.widget.TextView;
|
||||
|
||||
public class get extends Activity {
|
||||
// textview
|
||||
private TextView mTextView;
|
||||
// handler线程
|
||||
private Handler mHandler = new Handler() {
|
||||
public void handleMessage(android.os.Message msg) {
|
||||
mTextView.setText((String) msg.obj);
|
||||
};
|
||||
};
|
||||
|
||||
@Override
|
||||
protected void onCreate(Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
setContentView(R.layout.get);
|
||||
// 绑定id
|
||||
mTextView = (TextView) findViewById(R.id.a);
|
||||
|
||||
}
|
||||
|
||||
// 联网
|
||||
private void getHttp() {
|
||||
InputStream stream = null;
|
||||
InputStreamReader re = null;
|
||||
BufferedReader reader = null;
|
||||
try {
|
||||
// 找水源
|
||||
URL url = new URL("http://www.taobao.com");
|
||||
// 建立总闸
|
||||
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
|
||||
conn.setRequestMethod("GET");
|
||||
// 建立水道
|
||||
stream = conn.getInputStream();
|
||||
// 捕鱼
|
||||
// 小渔网
|
||||
re = new InputStreamReader(stream);
|
||||
// 大渔网
|
||||
reader = new BufferedReader(re);
|
||||
|
||||
// 捞鱼
|
||||
// 大水桶
|
||||
String len = "";
|
||||
// 小水桶
|
||||
String temp = "";
|
||||
while ((temp = reader.readLine()) != null) {
|
||||
len += temp;
|
||||
}
|
||||
// 更新UI
|
||||
Message sg = new Message();
|
||||
sg.obj = len;
|
||||
mHandler.sendMessage(sg);
|
||||
|
||||
} catch (MalformedURLException e) {
|
||||
e.printStackTrace();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
} finally {
|
||||
// 晒网,关闭资源
|
||||
try {
|
||||
if (reader != null) {
|
||||
reader.close();
|
||||
}
|
||||
if (re != null) {
|
||||
re.close();
|
||||
}
|
||||
if (stream != null) {
|
||||
stream.close();
|
||||
}
|
||||
|
||||
} catch (Exception e2) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 按钮监听
|
||||
public void aaa(View v) {
|
||||
//新建一个线程
|
||||
new Thread() {
|
||||
public void run() {
|
||||
getHttp();
|
||||
};
|
||||
}.start();
|
||||
}
|
||||
}
|
@ -1,9 +1,23 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
xmlns:tools="http://schemas.android.com/tools"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent">
|
||||
|
||||
<WebView
|
||||
android:id="@+id/web_view"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent"/>
|
||||
android:layout_height="match_parent"
|
||||
tools:layout_editor_absoluteX="61dp"
|
||||
tools:layout_editor_absoluteY="86dp">
|
||||
|
||||
<Button
|
||||
android:id="@+id/button8"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_x="307dp"
|
||||
android:layout_y="666dp"
|
||||
android:background="@android:color/holo_orange_light"
|
||||
android:text="收藏" />
|
||||
</WebView>
|
||||
</androidx.constraintlayout.widget.ConstraintLayout>
|
@ -0,0 +1,27 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
xmlns:tools="http://schemas.android.com/tools"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent"
|
||||
>
|
||||
<Button
|
||||
android:id="@+id/a12"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:text="获取"
|
||||
android:onClick="aaa"
|
||||
/>
|
||||
|
||||
<ScrollView
|
||||
android:layout_below="@id/a12"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent" >
|
||||
|
||||
<TextView
|
||||
android:id="@+id/a"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:text="获取网址内容" />
|
||||
</ScrollView>
|
||||
|
||||
</RelativeLayout>
|
Loading…
Reference in new issue