我现在对 oAuth 有一点问题。它是我一直在弄乱的示例代码,它在 2.2 上完美运行,但是当我在 4.1 上测试它时,我得到了强制关闭错误。
这是我一直在弄乱的示例代码:
public class OAuthAccessTokenActivity extends Activity {
final String TAG = getClass().getName();
private SharedPreferences prefs;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.i(TAG, "Starting task to retrieve request token.");
this.prefs = PreferenceManager.getDefaultSharedPreferences(this);
// new OAuthRequestTokenTask(this).execute();
}
@Override
protected void onResume() {
super.onResume();
WebView webview = new WebView(this);
webview.getSettings().setJavaScriptEnabled(true);
webview.setVisibility(View.VISIBLE);
setContentView(webview);
String authorizationUrl = new GoogleAuthorizationRequestUrl(
OAuth2ClientCredentials.CLIENT_ID,
OAuth2ClientCredentials.REDIRECT_URI,
OAuth2ClientCredentials.SCOPE).build();
/* WebViewClient must be set BEFORE calling loadUrl! */
webview.setWebViewClient(new WebViewClient() {
@Override
public void onPageStarted(WebView view, String url, Bitmap bitmap) {
System.out.println("onPageStarted : " + url);
}
@Override
public void onPageFinished(WebView view, String url) {
if (url.startsWith(OAuth2ClientCredentials.REDIRECT_URI)) {
try {
if (url.indexOf("code=") != -1) {
String code = extractCodeFromUrl(url);
AccessTokenResponse accessTokenResponse = new GoogleAuthorizationCodeGrant(
new NetHttpTransport(),
new JacksonFactory(),
OAuth2ClientCredentials.CLIENT_ID,
OAuth2ClientCredentials.CLIENT_SECRET,
code, OAuth2ClientCredentials.REDIRECT_URI)
.execute();
CredentialStore credentialStore = new SharedPreferencesCredentialStore(prefs);
credentialStore.write(accessTokenResponse);
view.setVisibility(View.INVISIBLE);
startActivity(new Intent(OAuthAccessTokenActivity.this,AgentPortalActivity.class));
} else if (url.indexOf("error=") != -1) {
view.setVisibility(View.INVISIBLE);
new SharedPreferencesCredentialStore(prefs).clearCredentials();
startActivity(new Intent(OAuthAccessTokenActivity.this,LatitudeApiSample.class));
}
} catch (IOException e) {
e.printStackTrace();
}
}
System.out.println("onPageFinished : " + url);
}
private String extractCodeFromUrl(String url) {
return url.substring(
OAuth2ClientCredentials.REDIRECT_URI.length() + 7,
url.length());
}
});
webview.loadUrl(authorizationUrl);
}
}
这是我的日志:
04-04 13:04:50.407: E/AndroidRuntime(20119): FATAL EXCEPTION: main
04-04 13:04:50.407: E/AndroidRuntime(20119): android.os.NetworkOnMainThreadException
04-04 13:04:50.407: E/AndroidRuntime(20119): at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1118)
04-04 13:04:50.407: E/AndroidRuntime(20119): at java.net.InetAddress.lookupHostByName(InetAddress.java:385)
04-04 13:04:50.407: E/AndroidRuntime(20119): at java.net.InetAddress.getAllByNameImpl(InetAddress.java:236)
04-04 13:04:50.407: E/AndroidRuntime(20119): at java.net.InetAddress.getAllByName(InetAddress.java:214)
04-04 13:04:50.407: E/AndroidRuntime(20119): at libcore.net.http.HttpConnection.<init>(HttpConnection.java:70)
04-04 13:04:50.407: E/AndroidRuntime(20119): at libcore.net.http.HttpConnection.<init>(HttpConnection.java:50)
04-04 13:04:50.407: E/AndroidRuntime(20119): at libcore.net.http.HttpConnection$Address.connect(HttpConnection.java:340)
04-04 13:04:50.407: E/AndroidRuntime(20119): at libcore.net.http.HttpConnectionPool.get(HttpConnectionPool.java:87)
04-04 13:04:50.407: E/AndroidRuntime(20119): at libcore.net.http.HttpConnection.connect(HttpConnection.java:128)
04-04 13:04:50.407: E/AndroidRuntime(20119): at libcore.net.http.HttpEngine.openSocketConnection(HttpEngine.java:315)
04-04 13:04:50.407: E/AndroidRuntime(20119): at libcore.net.http.HttpsURLConnectionImpl$HttpsEngine.makeSslConnecti(HttpsURLConnectionImpl.java:461)
04-04 13:04:50.407: E/AndroidRuntime(20119): at libcore.net.http.HttpsURLConnectionImpl$HttpsEngine.connect(HttpsURLConnectionImpl.java:433)
04-04 13:04:50.407: E/AndroidRuntime(20119): at libcore.net.http.HttpEngine.sendSocketRequest(HttpEngine.java:289)
04-04 13:04:50.407: E/AndroidRuntime(20119): at libcore.net.http.HttpEngine.sendRequest(HttpEngine.java:239)
04-04 13:04:50.407: E/AndroidRuntime(20119): at libcore.net.http.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:80)
04-04 13:04:50.407: E/AndroidRuntime(20119): at libcore.net.http.HttpURLConnectionImpl.getOutputStream(HttpURLConnectionImpl.java:188)
04-04 13:04:50.407: E/AndroidRuntime(20119): at libcore.net.http.HttpsURLConnectionImpl.getOutputStream(HttpsURLConnectionImpl.java:281)
04-04 13:04:50.407: E/AndroidRuntime(20119): at com.google.api.client.http.javanet.NetHttpRequest.execute(NetHttpRequest.java:78)
04-04 13:04:50.407: E/AndroidRuntime(20119): at com.google.api.client.http.HttpRequest.execute(HttpRequest.java:361)
04-04 13:04:50.407: E/AndroidRuntime(20119): at com.google.api.client.auth.oauth2.draft10.AccessTokenRequest.executeUnparsed(AccessTokenRequest.java:457)
04-04 13:04:50.407: E/AndroidRuntime(20119): at com.google.api.client.auth.oauth2.draft10.AccessTokenRequest.execute(AccessTokenRequest.java:473)
04-04 13:04:50.407: E/AndroidRuntime(20119): at com.jetdelivery.mobile.OAuthAccessTokenActivity$1.onPageFinished(OAuthAccessTokenActivity.java:95)
04-04 13:04:50.407: E/AndroidRuntime(20119): at android.webkit.CallbackProxy.handleMessage(CallbackProxy.java:389)
04-04 13:04:50.407: E/AndroidRuntime(20119): at android.os.Handler.dispatchMessage(Handler.java:99)
04-04 13:04:50.407: E/AndroidRuntime(20119): at android.os.Looper.loop(Looper.java:137)
04-04 13:04:50.407: E/AndroidRuntime(20119): at android.app.ActivityThread.main(ActivityThread.java:4918)
04-04 13:04:50.407: E/AndroidRuntime(20119): at java.lang.reflect.Method.invokeNative(Native Method)
04-04 13:04:50.407: E/AndroidRuntime(20119): at java.lang.reflect.Method.invoke(Method.java:511)
04-04 13:04:50.407: E/AndroidRuntime(20119): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1004)
04-04 13:04:50.407: E/AndroidRuntime(20119): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:771)
04-04 13:04:50.407: E/AndroidRuntime(20119): at dalvik.system.NativeStart.main(Native Method)
似乎崩溃发生在第 95 行,即“.execute();”
谁能给我一些关于我应该做什么的建议?谢谢!