Issue
I'm using AsyncTask
to load a div
in a WebView
, while displaying a ProgressDialog
.
The content is loaded and the ProgressDialog
gets displayed, but it doesn't disappear because the execution does not enter onPostExecute()
. Can you tell me what I'm doing wrong?
package xx.xxx.xxx;
import java.io.IOException;
import org.jsoup.Jsoup;
import org.jsoup.select.Elements;
import org.jsoup.nodes.Document;
import android.R.string;
import android.app.Activity;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Toast;
public class NewsActivity extends Activity {
private WebView wView;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.news_main);
new LoadNews().execute("");
}
private class LoadNews extends AsyncTask<String, Integer, String> {
private ProgressDialog Dialog = new ProgressDialog(NewsActivity.this);
@Override
protected String doInBackground(String... params) {
String url = "http://www.example.com/example";
Document doc;
try {
doc = Jsoup.connect(url).get();
Elements ele = doc.select("div#examplediv");
String html = ele.toString();
String mime = "text/html";
String encoding = "utf-8";
wView = (WebView)findViewById(R.id.wvNews);
wView.loadDataWithBaseURL(url, html, mime, encoding, null);
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
protected void onPreExecute()
{
Dialog.setMessage("please wait...");
Dialog.show();
}
protected void onPostExecute(Void unused)
{
try
{
if(Dialog.isShowing())
{
Dialog.dismiss();
}
} catch(Exception e) {
}
}
}
}
Solution
Actually, Your AsyncTask
Class implementation is wrong..
There are lots of mistakes in your code,
You are trying to Access UI (WebView) in
doInBackground()
which doesn't allowed in Android. As doInBackground runs in worker thread, while your Activity Ui (WebView) is in MainUI Thread.The argument of
onPostExecute()
and return type ofdoInBackground()
doesn't match.
Please Go trough the AsyncTask Class from Android Developers and get Basic Fundamental of How it works..
Answered By - user370305
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.