目标效果:
点击按钮,在TextView中显示百度页面的源码。
1.activity_main.xml页面放置Button控件和TextView控件。
activity_main.xml页面:
<LinearLayout 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"
android:orientation="vertical"
tools:context=".MainActivity" >
<Button
android:id="@+id/btRequest"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="click"
android:text="Send Request" />
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:id="@+id/tvResponse"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />
</ScrollView>
</LinearLayout>
2.MainActivity.java页面创建子线程发送请求并在主线程为TextView赋值。
MainActivity.java页面:
package com.example.webview;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.TextView;
public class MainActivity extends Activity {
private TextView tvResponse;
public static final int SHOW_RESPONSE=1;
/*TextView是在主线程定义,所以修改操作也必须在主线程中,而获取内容是在子线程,所以当子线程获取内容后需要给主线程发送信息,主线程再对TextView的文本内容进行修改*/
private Handler handler=new Handler(){
public void handleMessage(android.os.Message msg){
switch (msg.what) {
case SHOW_RESPONSE://根据子线程编号判断是哪个子线程发来的信息
String content=(String) msg.obj;
tvResponse.setText(content);
break;
default:
break;
}
};
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tvResponse=(TextView) findViewById(R.id.tvResponse);
}
public void click(View view){
sendRequest();
}
private void sendRequest() {
/*需要新建子线程进行访问*/
new Thread(){
public void run(){
HttpURLConnection httpURLConnection=null;
try {
URL url=new URL("http://www.baidu.com");
httpURLConnection=(HttpURLConnection) url.openConnection();//获取到httpURLConnection的实例
httpURLConnection.setRequestMethod("GET");//设置HTTP请求所使用的方法,GET表示希望从服务器那里获取数据,而POST则表示希望提交数据给服务器
httpURLConnection.setReadTimeout(6000);//设置读取超时的毫秒数
InputStream is=httpURLConnection.getInputStream();//获取到服务器返回的输入流
BufferedReader br=new BufferedReader(new InputStreamReader(is));//对获取到的输入流进行读取
String s;
StringBuilder sb=new StringBuilder();
while((s=br.readLine())!=null){
sb.append(s);
}
Message msg=new Message();
msg.what=SHOW_RESPONSE;//封装子线程编号
msg.obj=sb.toString();//封装获取到的内容
handler.sendMessage(msg);//发送信息
} catch (Exception e) {
e.printStackTrace();
}finally{
httpURLConnection.disconnect();//将HTTP连接关闭掉
}
};
}.start();
}
}
3.AndroidManifest.xml页面添加网络权限,在</manifest>标签前边添加
<uses-permission android:name="android.permission.INTERNET"/>
添加权限也可以在AndroidManifest.xml页面的Permissions中添加。
4.因为在子线程中不能对UI进行操作,所以更改TextView文本信息需要提交到主线程中,定义Handler进行获取。

该博客主要介绍了如何在Android应用中使用WebView加载百度页面,并通过Handler将网页源码显示在TextView上,实现点击按钮后显示源码的功能。
3009

被折叠的 条评论
为什么被折叠?



