Reputation: 6606
attempting to clean up some outdated twitter authentication code Ive been using as the newer versions of android do not allow for network tasks to be done on the ui thread anymore without crashing the app.
I seemed to of solved half the problem by sticking the webview.loadurl(); call onto an async task but the app then crashes during the second part of the process when onLoadResource(); is called by the webviewclient, just cant figure out what the best way would be to place the onLoadResource(); method into an async task without breaking the code.
package com.testapp2.second.activities;
import com.testapp2.second.OTweetApplication;
import com.testapp2.second.R;
import android.app.Activity;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.webkit.WebView;
import android.webkit.WebViewClient;
public class AuthorizationActivity extends Activity {
private OTweetApplication app;
private WebView webView;
private WebViewClient webViewClient = new WebViewClient() {
@Override
public void onLoadResource(WebView view, String url) {
// the URL we're looking for looks like this:
// http://otweet.com/authenticated?oauth_token=1234567890qwertyuiop
Uri uri = Uri.parse(url);
if (uri.getHost().equals("otweet.com")) {
String token = uri.getQueryParameter("oauth_token");
if (null != token) {
webView.setVisibility(View.INVISIBLE);
app.authorized();
finish();
} else {
// tell user to try again
}
} else {
super.onLoadResource(view, url);
}
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
app = (OTweetApplication)getApplication();
setContentView(R.layout.authorization_view);
setUpViews();
}
@Override
protected void onResume() {
super.onResume();
new AuthInBg().execute();
}
private void setUpViews() {
webView = (WebView)findViewById(R.id.web_view);
webView.setWebViewClient(webViewClient);
}
private class AuthInBg extends AsyncTask<Void, Void, Void> {
@Override
protected Void doInBackground(Void... arg0) {
String authURL = app.beginAuthorization();
webView.loadUrl(authURL);
return null;
}
}
}
should I change the async task result to a constructor for the OnLoadResource(); method perhaps?
Upvotes: 1
Views: 3659
Reputation: 11211
I think that you need to put in runOnUIThread()
method the webView.setVisibility(View.INVISIBLE);
line and it should work.
it should be like this:
runOnUiThread(new Runnable() {
public void run() {
webView.setVisibility(View.INVISIBLE);
}
});
Upvotes: 2