简体   繁体   English

简单的jsoup Java HTML解析器将不会更新TextView

[英]Simple jsoup Java HTML Parser Will Not Update TextView

I'm attempting to update my textview using web data via JSOUP 我正在尝试通过JSOUP使用网络数据更新我的textview

http://jsoup.org/ http://jsoup.org/

For some reason it never seems to update my TextView with the table data from the URL I'm attempting to gather data from. 出于某种原因,它似乎从未使用我要从中收集数据的URL中的表数据更新TextView。

Any suggestions? 有什么建议么?

I've looked over it quite a bit and I cannot seem to spot what I'm doing wrong - but I'm sure it's something simple. 我已经仔细检查了一下,似乎无法发现自己​​在做错什么-但我敢肯定这很简单。

XML: XML:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
            android:id="@+id/TextView01"
        android:text="@string/hello_world" />

</RelativeLayout>

SOURCE: 资源:

public class MainActivity extends Activity {

    TextView tv;
    String url = "http://sheriff.org/apps/arrest/results.cfm?lname=ABBOTT&fname=LAUREA";
    String tr;
    Document doc;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        tv = (TextView) findViewById(R.id.TextView01);
        new MyTask().execute(url);
    }

    private class MyTask extends AsyncTask<String, Void, String> {

        ProgressDialog prog;

        String title = "";

        @Override
        protected void onPreExecute() {
            prog = new ProgressDialog(MainActivity.this);
            prog.setMessage("Loading....");
            prog.show();
        }

        @Override
        protected String doInBackground(String... params) {
            try {
                doc = Jsoup.connect(params[0]).get();
                Element tableElement = doc.select(".datagrid").first();

                Elements tableRows = tableElement.select("tr");
                for (Element row : tableRows) {
                    Elements cells = row.select("td");
                    if (cells.size() > 0) {
                        System.out.println(
                                cells.get(0).text() + "; " + cells.get(1).text() + "; " + cells
                                        .get(2).text() + "; " + cells.get(3).text());
                    }
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
            return title;
        }


        @Override
        protected void onPostExecute(String result) {
            super.onPostExecute(result);
            prog.dismiss();
            tv.setText(result);
        }
    }
}

EDIT IN RESPONSE TO FIRST ANSWER - STILL NO FIX: 响应第一个答案进行编辑-仍未解决:

public class MainActivity extends Activity {

    TextView tv;
    String url = "http://sheriff.org/apps/arrest/results.cfm?lname=ABBOTT&fname=LAUREA";
    String tr;
    Document doc;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        tv = (TextView) findViewById(R.id.TextView01);
        new MyTask().execute(url);
    }

    private class MyTask extends AsyncTask<String, Void, String> {

        ProgressDialog prog;

        String title = "";

        @Override
        protected void onPreExecute() {
            prog = new ProgressDialog(MainActivity.this);
            prog.setMessage("Loading....");
            prog.show();
        }

        @Override
        protected String doInBackground(String... params) {
            try {
                doc = Jsoup.connect(params[0]).get();
                Element tableElement = doc.select(".datagrid").first();

                Elements tableRows = tableElement.select("tr");
                for (Element row : tableRows) {
                    Elements cells = row.select("td");
                    if (cells.size() > 0) {
                         title = cells.get(0).text() + "; " + cells.get(1).text() + "; " + 
                                 cells.get(2).text() + "; " + cells.get(3).text();
                    }
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
            return title;
        }


        @Override
        protected void onPostExecute(String result) {
            super.onPostExecute(result);
            prog.dismiss();
            tv.setText(result);
        }
    }
}

I also tried: 我也尝试过:

public class MainActivity extends Activity {

TextView tv;
String url = "http://sheriff.org/apps/arrest/results.cfm?lname=ABBOTT&fname=LAUREA";
String tr;
Document doc;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    tv = (TextView) findViewById(R.id.TextView01);
    new MyTask().execute(url);
}

private class MyTask extends AsyncTask<String, Void, String> {

    ProgressDialog prog;

    String title = "";

    @Override
    protected void onPreExecute() {
        prog = new ProgressDialog(MainActivity.this);
        prog.setMessage("Loading....");
        prog.show();
    }

    @Override
    protected String doInBackground(String... params) {
        try {
            doc = Jsoup.connect(params[0]).get();
            Element tableElement = doc.select(".datagrid").first();

            Elements tableRows = tableElement.select("tr");
            for (Element row : tableRows) {
                Elements cells = row.select("td");
                if (cells.size() > 0) {
                     title = cells.get(0).text() + "; " + cells.get(1).text() + "; " + 
                             cells.get(2).text() + "; " + cells.get(3).text();
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return title;
    }


    @Override
    protected void onPostExecute(String title) {
        super.onPostExecute(title);
        prog.dismiss();
        tv.setText(title);
    }
}
}

1) You don't assign anything to the title variable. 1)您没有为title变量分配任何内容。

2) Don't use System.out.* . 2)不要使用System.out.* It's for console programs. 它用于控制台程序。 In Android, use Log 在Android中,使用Log

In the doInBackground() change your code to: doInBackground()将代码更改为:

 title = cells.get(0).text() + "; " + cells.get(1).text() + "; " + 
 cells.get(2).text() + "; " + cells.get(3).text());

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM