jsoup.connect / jsoup.parse的进展

时间:2011-03-22 09:57:00

标签: android jsoup

我在Android应用程序中使用Jsoup来解析网页中的数据。我想显示进度

Jsoup.connect(...).get();

Jsoup.parse(....);

怎么做?是否有像webview一样更改进度时自动调用的方法。请告诉我如何完成任务。

2 个答案:

答案 0 :(得分:0)

同样的想法:使用测试网页的权重除以解析时间,并将此值用作其他页面的乘数。在第一个应用程序运行,您可以计算它。

我认为用户不需要超精准的计时器。

轻盈的谷歌搜索告诉我,jsoup不需要方法。 (如果我错了,请纠正我)

答案 1 :(得分:0)

我相信Jsoup中没有方法可以获得进度get()和post()。我已经设法通过HttpURLConnection执行此操作,然后传递Jsoup.parse文件。它看起来像这样: (不要忘记在清单文件中添加权限)

WithFileProgress.java:

public class WithFileProgress {
ProgressDialog progressDialog;
Context context;
TextView content;

public WithFileProgress(Context context, TextView content) {
    this.context = context;
    this.content = content;
}

public void connect(String url) {
    new downloadHTML().execute(url);
}

private class downloadHTML extends AsyncTask<String, Integer, Document> { // params, progress, result

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        progressDialog = new ProgressDialog(context);
        progressDialog.setTitle("downloadHTML");
        progressDialog.setMessage("Loading...");
        progressDialog.setIndeterminate(true);
        progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
        progressDialog.setCancelable(true);
        progressDialog.show();
    }

    @Override
    protected Document doInBackground(String... params) {
        InputStream input = null;
        OutputStream output = null;
        HttpURLConnection connection = null;
        File file;
        Document d = null;

        try {
            URL url = new URL(params[0]);
            connection = (HttpURLConnection) url.openConnection();
            connection.connect();

            // expect HTTP 200 OK, so we don't mistakenly save error report
            // instead of the file
            if (connection.getResponseCode() != HttpURLConnection.HTTP_OK) {
                AlertDialog.Builder builder = new AlertDialog.Builder(context);
                builder.setTitle("downloadHTML").setMessage("Server returned HTTP " + connection.getResponseCode()
                        + " " + connection.getResponseMessage()).create();
            }

            // this will be useful to display download percentage
            // might be -1: server did not report the length
            int fileLength = connection.getContentLength();

            // download the file
            input = connection.getInputStream();

            file = new File(Environment.getExternalStorageDirectory(), "downloadHTML.tmp");
            output = new FileOutputStream(file);

            byte data[] = new byte[8192];
            long total = 0;
            int count;
            while ((count = input.read(data)) != -1) {
                // allow canceling with back button
                if (isCancelled()) {
                    input.close();
                    return null;
                }
                total += count;
                // publishing the progress....
                if (fileLength > 0) // only if total length is known
                    publishProgress((int) (total * 100 / fileLength));
                output.write(data, 0, count);
            }

            d = Jsoup.parse(file, null);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (output != null)
                    output.close();
                if (input != null)
                    input.close();
            } catch (IOException ignored) {
            }

            if (connection != null)
                connection.disconnect();
        }
        return d;
    }

    @Override
    protected void onPostExecute(Document d) {
        super.onPostExecute(d);
        content.setText(d.html());
        progressDialog.dismiss();
    }

    @Override
    protected void onProgressUpdate(Integer... values) {
        super.onProgressUpdate(values);
        // if we get here, length is known, now set indeterminate to false
        progressDialog.setIndeterminate(false);
        progressDialog.setMax(100);
        progressDialog.setProgress(values[0]);
    }

}

}

MainActivity.java:

public class MainActivity extends Activity {
WithFileProgress api;

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

    TextView content = (TextView)findViewById(R.id.content);
    api = new WithFileProgress(this, content);

    Button button = (Button)findViewById(R.id.button);
    button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            EditText input = (EditText)findViewById(R.id.input);
            String url = input.getText().toString();

            if (Jsoup.isValid(url, new Whitelist())) {
                api.connect(url);
            } else {
                AlertDialog.Builder builder = new AlertDialog.Builder(getApplicationContext());
                builder.setTitle("MainActivity").setMessage("Invalid address: "+url).create();
            }
        }
    });
}

}

activity_layout.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:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context=".MainActivity">

<ScrollView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/contentScroll"
    android:layout_above="@+id/input"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:layout_alignParentRight="true">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/content" />

</ScrollView>

<EditText
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/input"
    android:layout_alignParentBottom="true"
    android:layout_alignParentLeft="true"
    android:layout_toLeftOf="@+id/button"
    android:hint="URL address" />

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Download"
    android:id="@+id/button"
    android:layout_alignBottom="@+id/input"
    android:layout_alignParentRight="true"
    android:layout_alignTop="@+id/input" />