0

我正在尝试使用 jsoup (0.5 mb) 解析一个长的 html 文档,问题是 - 使用常规 java 控制台程序对我有用的相同代码在 android 上的 asynctask 中不起作用..有什么建议吗?

我的代码 -

import java.util.ArrayList;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import com.telofun.db.DatabaseHandler;
import com.telofun.logic.Constants;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.database.SQLException;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends Activity {
    private DatabaseHandler db;
    private String loadingStationsMessage;
    private String somethingWentWrongMessage;
    private String stationsWereLoadedIntoDb;
    private Button openMap;
    private Button openListOfRegions;
    private ProgressDialog progressDialog;
    private boolean english = false;
    private Document doc;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        openMap = (Button) findViewById(R.id.btn_goToMap);
        openListOfRegions = (Button) findViewById(R.id.btn_lookForBikes);
        db = new DatabaseHandler(this);
        loadingStationsMessage = this.getResources().getString(
                R.string.loading_stations_please_wait);
        somethingWentWrongMessage = this.getResources().getString(
                R.string.something_went_wrong_message);
        stationsWereLoadedIntoDb = this.getResources().getString(
                R.string.fetched_all_stations_from_server);

        // Assign actions for button clicks
        openMap.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                Intent gotoMap = new Intent(getApplicationContext(),
                        FragmentMap.class);
                startActivity(gotoMap);
            }
        });

        openListOfRegions.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                try {
                new LoadStations().execute();
                } catch(Exception e) {
                    Toast.makeText(getApplicationContext(), somethingWentWrongMessage, Toast.LENGTH_LONG).show();
                }
            }
        });
    }

    /**
     * Background Async Task to Load all stations via jsoup parsing
     * */
    class LoadStations extends AsyncTask<String, String, String> {
        boolean succeeded = false;
        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            progressDialog = new ProgressDialog(MainActivity.this);
            progressDialog.setMessage(loadingStationsMessage);
            progressDialog.setIndeterminate(false);
            progressDialog.setCancelable(false);
            progressDialog.show();
        }

        /**
         * loading stations data into db
         * */
        protected String doInBackground(String[] args) {
            String telofunUrl;
            ArrayList<String> regionNames;
            String regionId;
            int regionID;
            long rowId;
            try {
                if (english) {
                    telofunUrl = Constants.URL_ENGLISH;
                } else {
                    telofunUrl = Constants.URL_HEBREW;
                }
                // Getting the stations locations
                doc = Jsoup.connect(telofunUrl).get();
                try {
                    Elements regionsTextualRepresenataion = doc.select(
                            "a.bicycle_regionname");
                    Elements headers = regionsTextualRepresenataion.select("b");
                    regionNames = new ArrayList<String>();

                    // Get the region names
                    for (Element singleRegion : headers) {
                        regionNames.add(singleRegion.text());
                    }

                    // Get the region contents
                    Elements bicycleRegions = doc
                            .select(Constants.TAG_DIV_CLASS_BICYCLE_REGION);
                    // Get the region names
                    for (Element bicycleRegion : bicycleRegions) {
                        // Get the region id
                        regionId = bicycleRegion.attr(Constants.REGION_ID);
                        regionID = Integer.parseInt(regionId) - 1;

                        // Get all the bicycle station of the region
                        Elements bicycleStations = bicycleRegion
                                .select("a.bicycle_station");
                        for (Element bicycleStation : bicycleStations) {
                            rowId = db.addStation(regionId, regionNames.get(regionID),
                                    bicycleStation);
                            if (rowId < 0) {
                                succeeded = false;
                            }
                        }

                    }
                } catch (SQLException ex) {
                    Log.w("SQLException", ex.fillInStackTrace());
                }
            } catch (Exception e) {
                Toast.makeText(getApplicationContext(), somethingWentWrongMessage, Toast.LENGTH_LONG).show();
                e.printStackTrace();
            }

            return null;
        }

        /**
         * After completing background task Dismiss the progress dialog
         * **/
        protected void onPostExecute(String file_url) {
            // dismiss the dialog after getting all stations
            progressDialog.dismiss();
            if(succeeded) {
                Toast.makeText(getApplicationContext(), stationsWereLoadedIntoDb, Toast.LENGTH_LONG).show();
            } else {
                Toast.makeText(getApplicationContext(), somethingWentWrongMessage, Toast.LENGTH_LONG).show();
            }

        }

    }

}
4

2 回答 2

0

我设法克服了一半的问题,显然 jsoup 1.7.2 不够稳定,或者它似乎无法处理大文件 - 我将连接分为两个阶段,在 onPreExecute 上连接并在 doInBackground 上,在这样做之后我在连接中添加了一个用户代理,如此处所示

于 2013-09-27T23:15:08.563 回答
0

更新二 - 在代码弄乱了一点之后,似乎我的问题是在 https 连接而不是常规 http 的前面工作,即一旦我弄清楚如何将它们保存到我的连接中,cookie 就会参与该过程我会在这里发布一个完整的答案供其他人使用。

于 2013-09-28T14:17:47.380 回答