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

更新 II - コードを少しいじった後、私の問題は通常の http ではなく https 接続の前で機能していたようです。つまり、Cookie を接続に保存する方法を見つけたら、Cookie がプロセスに関与しているようです。他の人が使用できるように、ここに完全な回答を投稿します。

于 2013-09-28T14:17:47.380 に答える
0

私は問題の半分を克服することができました.jsoup 1.7.2は十分に安定していないか、大きなファイルでの作業に失敗しているようです.ここに見られるように、接続にユーザーエージェントを追加しました

于 2013-09-27T23:15:08.563 に答える