这是我第一次使用AsyncTask并且它可以正常工作但是因为我刚刚复制并粘贴了一些代码而非常脏。我想以正确的方式做到尽可能干净。那么任何人都可以帮我清理我的代码并告诉我如何以正确的方式做到这一点?在帮助我并给予我未来Android开发的更多经验时,你会帮我一个大忙。我想从一开始就以正确的方式学习它;)
修改 在阅读了大量教程并观看视频后,我认为我取得了巨大的进步!我很高兴我做到了!但是只留下一个小问题。在应用启动时,它不会加载页面。单击链接后,shouldOverrideUrlLoading工作得很好,但在启动时它只显示一个空白屏幕。问题是什么?
public class WebviewActivity extends MainActivity {
private WebView myWebView;
private ProgressDialog progressDialog;
private boolean mConnection = false;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final ScrollView mainScrollView = (ScrollView)findViewById(R.id.ScrollView01);
myWebView = (WebView)findViewById(R.id.webview);
WebSettings webSettings = myWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
myWebView.setWebViewClient(new WebViewClient(){
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
new checkConnection().execute();
if (mConnection == true){
view.loadUrl(url);
}
return true;
}
@Override
public void onPageFinished(WebView view, String url) {
mainScrollView.fullScroll(ScrollView.FOCUS_UP);
}
});
myWebView.requestFocus(View.FOCUS_DOWN);
myWebView.setOnTouchListener(new View.OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
case MotionEvent.ACTION_UP:
if (!v.hasFocus()) {
v.requestFocus();
}
break;
}
return false;
}
});
}
@Override
public void onResume() {
super.onResume();
new checkConnection().execute();
if (mConnection == true){
myWebView.loadUrl(webLink);
}
}
//------------------------------------------------------
//-----DOING THE CONNECTION CHECK IN ANOTHER THREAD-----
//------------------------------------------------------
public class checkConnection extends AsyncTask<Void, Void, Void>{
int mStatusCode = 0;
Exception mConnectionException;
@Override
protected void onPreExecute(){
super.onPreExecute();
progressDialog = ProgressDialog.show(WebviewActivity.this, "", "Loading...", true);
progressDialog.show();
}
@Override
protected Void doInBackground(Void... params) {
try {
ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
if (cm.getActiveNetworkInfo().isConnectedOrConnecting()) {
URL url = new URL(webLink);
HttpURLConnection urlc = (HttpURLConnection) url.openConnection();
urlc.setConnectTimeout(1000 * 5); // mTimeout is in seconds
urlc.connect();
mStatusCode = urlc.getResponseCode();
if (mStatusCode == 200){
//Nothing to do.
}
}
} catch (IOException e) {
e.printStackTrace();
mConnectionException = e;
}
return null;
}
@Override
protected void onPostExecute(Void param){
progressDialog.dismiss();
if (mStatusCode == 200){
mConnection = true;
}
else if (mStatusCode == 404){
myWebView.loadUrl("file:///android_asset/errorpage404.html");
}
else {
myWebView.loadUrl("file:///android_asset/errorpage.html");
}
}
}
}
答案 0 :(得分:1)
请不要从progressDialog.dismiss();
致电doInBackground()
,而是使用onPostExecute()
。 doInBackground()
不是UI线程,因此尝试操作UI元素可能会给您带来严重后果。
这就是您第一次尝试不加载页面的原因:
public boolean shouldOverrideUrlLoading(WebView view, String url) {
new checkConnection().execute(); <- (1)
if (mConnection == true){ <- (2)
view.loadUrl(url);
}
return true;
(1)启动异步任务,将在未来的某个时间完成 (2)试图检查(1)的结果,所以还没有结果。
这是AsyncTask
的修改版本,它接受url作为参数:
public class checkConnection extends AsyncTask<String, Void, String>{ <------ change this
int mStatusCode = 0;
Exception mConnectionException;
@Override
protected void onPreExecute(){
super.onPreExecute();
progressDialog = ProgressDialog.show(WebviewActivity.this, "", "Loading...", true);
progressDialog.show();
}
@Override
protected Void doInBackground(String... params) { <------ change this
try {
ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
if (cm.getActiveNetworkInfo().isConnectedOrConnecting()) {
URL url = new URL(webLink);
HttpURLConnection urlc = (HttpURLConnection) url.openConnection();
urlc.setConnectTimeout(1000 * 5); // mTimeout is in seconds
urlc.connect();
mStatusCode = urlc.getResponseCode();
if (mStatusCode == 200){
//Nothing to do.
}
}
} catch (IOException e) {
e.printStackTrace();
mConnectionException = e;
}
return params[0]; <------ change this
}
@Override
protected void onPostExecute(String param){ <------ change this
progressDialog.dismiss();
if (mStatusCode == 200){
myWebView.loadUrl(param); <------ change this
}
else if (mStatusCode == 404){
myWebView.loadUrl("file:///android_asset/errorpage404.html");
}
else {
myWebView.loadUrl("file:///android_asset/errorpage.html");
}
}
}