【问题标题】:Android - Displaying the Wrong Info for my AppAndroid - 为我的应用显示错误信息
【发布时间】:2016-04-03 19:40:23
【问题描述】:

我正在从 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" />

【问题讨论】:

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


    【解决方案1】:

    我认为您无法从 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();
        }
    

    【讨论】:

    • 其他代码部分?元素 tableElements = doc.select("td"); TextView textView = (TextView)findViewById(R.id.text_view); for (元素 td : tableElements) { textView.setText(td.text()); System.out.println(td.text()); } 这是你的意思?
    • 我的意思是文件的路径有问题,所以你不会读取文件,也不会设置你的文本视图。另一件事是只设置最后一个表格元素。并且 System.out.println 在 android 中不起作用,请改用 Log.d 在 Logcat 中查看已解析的元素。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-05-25
    • 2016-07-25
    • 2013-08-20
    • 2019-02-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多