Android - 显示我的应用程序的错误信息

时间:2016-04-03 19:40:24

标签: java android android-studio web-scraping jsoup

我正在从Android Studio中的本地HTML页面抓取数据,而不是正确的信息被删除它显示的所有内容" text_view"。有谁知道如何显示我已经刮过的数据?下面是主要代码。

 public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
/*    try {

        File input = new File("C:\\Users\\user\\Desktop\\Mobile Newest\\JSoup\\app\\src\\main\\assets\\filename.html");
        Document doc = Jsoup.parse(input, "UTF-8");
        Elements tableElements = doc.select("td");
        TextView textView = (TextView)findViewById(R.id.text_view);
        for (Element td : tableElements) {
            textView.setText(td.text());
           System.out.println(td.text());
        }

    } catch (IOException e) {
        e.printStackTrace();
    }*/
    try {
        StringBuilder buf=new StringBuilder();
        InputStreamReader inputStream = new InputStreamReader(getAssets().open("filename.html"));
        BufferedReader bufferedReader = new BufferedReader(inputStream);
        String str;
        while ((str=bufferedReader.readLine()) != null) {
            buf.append(str);
        }
        Document doc = Jsoup.parse(buf.toString());
        //other code parts goes next
        Elements tableElements = doc.select("td");
        TextView textView = (TextView)findViewById(R.id.text_view);
        for (Element td : tableElements) {
            textView.setText(td.text());
            System.out.println(td.text());
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
}

以下是content_main.xml

    <?xml version="1.0" encoding="utf-8"?>
 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:showIn="@layout/activity_main" tools:context=".MainActivity">

<TextView android:text="text_view1"
    android:id="@+id/text_view"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />

1 个答案:

答案 0 :(得分:0)

我认为你无法从Android设备到你的本地机器上的那条路径C:\ Users \ user \ Desktop \ Mobile Newest \ JSoup \ app \ src \ main \ assets \ filename.html。你试过调试代码吗?我想你应该触发你的IOException。您可以尝试像这样阅读您的资产文件。

try {
        StringBuilder buf=new StringBuilder();
        InputStreamReader inputStream = new InputStreamReader(getAssets().open("filename.html"));
        BufferedReader bufferedReader = new BufferedReader(inputStream);
        String str;
        while ((str=bufferedReader.readLine()) != null) {
            buf.append(str);
        }
        Document doc = Jsoup.parse(buf.toString());
        //other code parts goes next

    } catch (IOException e) {
        e.printStackTrace();
    }