android中从URL获取HTML源代码的Java代码不起作用
请注意下面的代码,我只是为了检索url的页面源信息而编写的。当我按下go!按钮,edittext应该显示url的页面源,在本例中,由于某种原因,某些异常被捕获,因此当我按下go!按钮,我在logcat中看到错误消息。我已经尝试解决这个错误两天了。请帮帮我。谢谢android中从URL获取HTML源代码的Java代码不起作用,java,android,html,eclipse,url,Java,Android,Html,Eclipse,Url,请注意下面的代码,我只是为了检索url的页面源信息而编写的。当我按下go!按钮,edittext应该显示url的页面源,在本例中,由于某种原因,某些异常被捕获,因此当我按下go!按钮,我在logcat中看到错误消息。我已经尝试解决这个错误两天了。请帮帮我。谢谢 **//HTMLExtract.java** package com.htmlextract; import java.io.BufferedReader; import java.io.InputStream; import java
**//HTMLExtract.java**
package com.htmlextract;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class HtmlExtract extends Activity{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_html_extract);
final EditText etext = (EditText) findViewById(R.id.etext);
final Button gobutton = (Button) findViewById(R.id.gobutton);
final TextView tview = (TextView) findViewById(R.id.tview);
gobutton.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v) {
try{
HttpClient client = new DefaultHttpClient();
HttpGet request = new HttpGet("https://www.google.com");
HttpResponse response = client.execute(request);
HttpEntity entity = response.getEntity();
InputStream is = entity.getContent();
BufferedReader br = new BufferedReader(new InputStreamReader(is));
StringBuilder sb = new StringBuilder();
String line = null;
while((line = br.readLine()) != null) {
sb.append(line +"\n"); }
String str = sb.toString();
tview.setText(str);
is.close();
br.close();
}
catch(Exception e){
System.out.println("Error Occured");
}
}
});
}
**//activity_html_extract.xml**
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="${relativePackage}.${activityClass}" >
<EditText
android:id="@+id/etext"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="https://google.com"/>
<Button
android:id="@+id/gobutton"
android:layout_width="fill_parent"
android:onClick=""
android:layout_height="wrap_content"
android:text="go!"/>
<TextView
android:id="@+id/tview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="hello world" />
</LinearLayout>
我假设您的应用程序尝试在其主线程上执行网络操作。 针对蜂巢SDK或更高版本的应用程序应在UI的单独线程上执行网络操作
连接到网络所涉及的基本任务已解释。您在AndroidManifest.xml中设置了吗?是的,我在Manifest中添加了internet权限谢谢您的回答。我会试试看,然后告诉你