我有一個使用jSoup從URL獲取數據的簡單Android應用程序。 TOAST的骯髒解決方案給我一個在線或離線的警告。我希望能夠從最後一次查詢了櫃面存儲用戶信息的新用戶脫機有數據。我不知道從哪裏開始?我讀到數據緩存,SQLite的,等等我應該存儲在用戶的情況下在sqlite的數據離線?
什麼是從存儲數據jsoup解析這樣我就可以找回它,如果用戶離線的最好方法?
這裏是我到目前爲止的代碼:
public class SlockActivity extends SherlockActivity {
private TextView textView;
private String url = "http://www.backcountryskiers.com/sac/sac-full.html";
private String noDangerUrl = "http://www.sierraavalanchecenter.org/sites/default/files/images/danger_icons_and_bars/0_nodangerrate.png";
private ImageView image;
private TextView tvWX;
// GLOBAL RESOURCES
private static SlockActivity instance = null;
// private ProgressDialog dialog = new ProgressDialog(SlockActivity.this);
@Override
public boolean onCreateOptionsMenu(Menu menu) {
menu.add("Emergency")
.setIcon(R.drawable.emergencyicon)
.setShowAsAction(
MenuItem.SHOW_AS_ACTION_IF_ROOM
| MenuItem.SHOW_AS_ACTION_WITH_TEXT);
menu.add("Weather")
.setIcon(R.drawable.wx_icon)
.setShowAsAction(
MenuItem.SHOW_AS_ACTION_IF_ROOM
| MenuItem.SHOW_AS_ACTION_WITH_TEXT);
menu.add("Refresh")
.setIcon(R.drawable.ic_refresh)
.setShowAsAction(
MenuItem.SHOW_AS_ACTION_IF_ROOM
| MenuItem.SHOW_AS_ACTION_WITH_TEXT);
return true;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView = (TextView) findViewById(R.id.todaysReport);
image = (ImageView) findViewById(R.id.dangerRose);
tvWX = (TextView) findViewById(R.id.todaysWX);
boolean check = isOnline();
if (check == true) {
Toast.makeText(this, "Internet is Connected", Toast.LENGTH_LONG)
.show();
fetcher task = new fetcher();
task.execute();
}
else {
Toast.makeText(this, "Failed to connect to internet.",
Toast.LENGTH_LONG).show();
textView.setText("No Network Connection and attempt to get Cached Data did not work. ");
}
}
public static Bitmap getBitmapFromURL(String src) {
try {
Log.e("src", src);
URL url = new URL(src);
HttpURLConnection connection = (HttpURLConnection) url
.openConnection();
connection.setDoInput(true);
connection.connect();
InputStream input = connection.getInputStream();
Bitmap myBitmap = BitmapFactory.decodeStream(input);
Log.e("Bitmap", "returned");
return myBitmap;
} catch (IOException e) {
e.printStackTrace();
Log.e("Exception", e.getMessage());
return null;
}
}
class fetcher extends AsyncTask<String, Void, String> {
// HERE DECLARE THE VARIABLES YOU USE FOR PARSING
private ProgressDialog dialog = new ProgressDialog(SlockActivity.this);
private Document doc = null;
private Elements content = null;
private Document parse = null;
private String results = null;
private Element dangerRatingImg = null;
private String dangerRatingSrc = null;
private Bitmap bimage;
@Override
protected String doInBackground(String... params) {
try {
doc = Jsoup.connect(url).get();
Log.e("Jsoup", "...is working...");
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
Log.e("Exception", e.getMessage());
}
content = doc.select("#content");
parse = Jsoup.parse(doc.html());
results = doc.select("#content").outerHtml();
// find rating image...
dangerRatingImg = doc.select("img").first();
dangerRatingSrc = dangerRatingImg.absUrl("src");
// Get the rating image
bimage = getBitmapFromURL(dangerRatingSrc);
return results;
}
@Override
protected void onPostExecute(String result) {
dialog.dismiss();
// smooth out the long scrolling...
textView.setMovementMethod(ScrollingMovementMethod.getInstance());
// Set this after the load so the background is the picture without
// the textviews background in
textView.setBackgroundResource(R.drawable.tvbg);
// Set the rating image
//
// DO THE IMAGE REPLACE ON URL COMPARE THING HERE>>>>>>>
if (dangerRatingSrc.equals(noDangerUrl)) {
image.setImageDrawable(getResources().getDrawable(
R.drawable.nodanger));
}
//
// image.setImageBitmap(bimage);
image.setPadding(0, 10, 0, 30);
// image.setScaleType(ScaleType.FIT_XY);
// return the summary
results = parse.select("#reportSummary").outerHtml();
// String reportDate = parse.select("#reportDate").outerHtml();
textView.setText(Html.fromHtml(results));
// textView.append(Html.fromHtml("<a href='#'><em>Read Full Report »</em></a>"));
textView.setPadding(30, 20, 20, 10);
// ditch the dialog, it's all loaded.
tvWX.setText(Html.fromHtml("Today's High: <b>26F</b>"));
}
@Override
protected void onPreExecute() {
// before we get the async results show this
dialog.setMessage("Loading Latest Update from the Sierra Avalanche Center...");
dialog.show();
}
}
// check network connectivity
public boolean isOnline() {
ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo netInfo = cm.getActiveNetworkInfo();
if (netInfo != null && netInfo.isConnectedOrConnecting()) {
Log.i("Network", "IS CONNECTED");
return true;
}
Log.i("Network", "IS NOT-CONNECTED");
return false;
}
public void onClickFullReport(View v) {
Intent intentFullReport = new Intent(getBaseContext(),
FullReportActivity.class);
intentFullReport.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
startActivity(intentFullReport);
}
}
我認爲,你必須在你的應用去服務(後臺服務) –