问题描述
我有1的EditText 1Button和1TextView,当我输入网址EDITTEXT,点击按钮,TextView的将是从我键入URL中的EditText网站上显示的HTML。我想通过URL来从网页的HTML。
问题
当我使用这个code在(AVD目标版本2.3.3)。 AndroidManifest(的minSdkVersion =10targetSdkVersion =10),我也改变targetSdkVersion =15)都是工作正确。但是当我将其更改为运行(AVD目标版本4.0.3),它无法正常工作。 Y'这是我的code
最后的EditText等=(EditText上)findViewById(R.id.editText1);
最后键b =(按钮)findViewById(R.id.button1);
最后的TextView电视=(TextView中)findViewById(R.id.textView1);
b.setOnClickListener(新OnClickListener(){
公共无效的onClick(视图v){
尝试 {
网址URL = NULL;
URL =新的URL(et.getText()的toString());
URLConnection的康恩= url.openConnection();
BufferedReader中的buff =新的BufferedReader(新的InputStreamReader(conn.getInputStream()));
串线=;
而((行= buff.readLine())!= NULL){
tv.append(线);
}
}赶上(例外五){
}
你得到一个 NetworkOnMainThreadException
,则不能使用时,访问在UI线程网络蜂窝或更高版本。你需要做你的工作在AsycnTask。请参阅this问题获取更多的信息。
I have 1 EditText 1Button and 1TextView, when I type url in Edittext and click button, the textView will be show the Html from website that i type the url in edittext. I want to get html from web by using url.
Problem
When I using this code in ( AVD Target version 2.3.3). AndroidManifest (minSdkVersion="10" targetSdkVersion="10") and I also change targetSdkVersion="15") both are work correct. but when I change it to run in (AVD target version 4.0.3) it's not work. Y? This is my code
final EditText et = (EditText) findViewById(R.id.editText1);
final Button b = (Button) findViewById(R.id.button1);
final TextView tv = (TextView) findViewById(R.id.textView1);
b.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
try {
URL url = null;
url = new URL(et.getText().toString());
URLConnection conn = url.openConnection();
BufferedReader buff = new BufferedReader(new InputStreamReader(conn.getInputStream()));
String line ="";
while((line = buff.readLine())!= null){
tv.append(line);
}
} catch (Exception e) {
}
You're getting a NetworkOnMainThreadException
, you can't access the network on the UI thread when using Honeycomb or later. You need to do your work in an AsycnTask. See this question for more info.
这篇关于错误时将HTML从网页中的机器人的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!