【问题标题】:progress of jsoup.connect/jsoup.parsejsoup.connect/jsoup.parse 的进度
【发布时间】:2011-07-20 08:29:27
【问题描述】:

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

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

Jsoup.parse(....);

怎么做?当进度改变时,是否有任何方法会自动调用,例如 webview。请告诉我如何完成任务。

【问题讨论】:

    标签: android jsoup


    【解决方案1】:

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

    而且我认为用户不需要超级精确的计时器。

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

    【讨论】:

      【解决方案2】:

      我相信 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" />
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2011-10-31
        • 2016-03-23
        • 2013-12-22
        • 1970-01-01
        • 2012-02-06
        • 1970-01-01
        • 2017-04-23
        相关资源
        最近更新 更多