【问题标题】:Java code to fetch HTML source from URL in android not working从Android中的URL获取HTML源的Java代码不起作用
【发布时间】:2014-12-30 00:39:01
【问题描述】:

请注意我编写的以下代码,它只是为了检索 url 的页面源信息。当我按下“开始!”按钮,edittext 应该显示 url 的页面源,在这种情况下是https://www.google.com,但是由于某种原因,一些异常被捕获,因此当我按下“开始!”按钮,我在 logcat 中看到“发生错误”消息。我已经尝试解决此错误 2 天了。请帮帮我。谢谢!

**//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>       

【问题讨论】:

  • 你是否在AndroidManifest.xml中设置了
  • 是的,我确实在清单中添加了互联网权限

标签: java android html eclipse url


【解决方案1】:

我想您的应用程序尝试在其主线程上执行网络操作。 针对 Honeycomb SDK 或更高版本的应用程序应在与 UI 不同的线程上执行网络操作。

连接到网络所涉及的基本任务解释here

【讨论】:

  • 感谢ivkil的回答。我会试一试告诉你
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-07-25
  • 2014-11-25
  • 2019-06-12
  • 2011-07-14
  • 1970-01-01
相关资源
最近更新 更多