我正在尝试使用 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();
}
}
}
}