我正在为 OAUTH API 实现客户端库。在身份验证过程中,webview 客户端会启动并加载身份验证页面的 URL。但是,webview 客户端有时会正确启动,但最近开始会导致 ANR。
这是库的代码:
public String inflateView(Activity activity,String redirectUrl, String scope){
final String rUrl=redirectUrl;
LayoutInflater inflater = activity.getLayoutInflater();
page=inflater.inflate(R.layout.web_overlay, null);
final String scopes=scope;
activity.setContentView(page);
gWebView = (WebView) page.findViewById(R.id.webview);
gWebView.loadUrl("https://api.23andme.com/authorize/?redirect_uri="
+ redirectUrl + "&response_type=code&client_id=" + this.clientId
+ "&scope=" + scope );
Log.d(TAG, "http://192.241.244.189/auth/authorize?response_type=code&client_id=IV9AMqP9&scope=read:sequence read:patient");
gWebView.setWebViewClient(new WebViewClient(){
@Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
if(url.startsWith("https://api.23andme.com")){
Log.d(TAG, "got to authentication page");
}
if (url.startsWith(rUrl)) {
System.out.println("got to override");
if (url.indexOf("code=") != -1) {
//if the query contains code
String queryString = null;
try {
queryString = new URL(url).getQuery();
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println(queryString);
String[] params = queryString.split("&");
for (String param : params) {
if (param.startsWith("code=")) {
code = param.substring(param.indexOf('=') + 1);
}
}
gWebView.setVisibility(View.GONE);
request= new PostRequest();
request.execute(code, smartClient.this.clientId, smartClient.this.clientSecret, rUrl, scopes);
Log.d(TAG, "Post execute");
Log.d(TAG, "cancelled");
while(true){
if(!accessToken.equals("")){
request.cancel(true);
Log.d(TAG, "not empty");
gWebView.destroy();
break;
}
}
// don't go to redirectUri
}
}
}
});
我使用以下方法调用它:
client= new smartClient(id, secret);
String token=client.inflateView(SmartActivity.this,REDIRECT_URI, SCOPE);
webview的布局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<WebView
android:id="@+id/webview"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
我在不知不觉中做了一些愚蠢的事情吗?
编辑: PostRequest 是一个 AsyncTask 类,只是为了澄清。编辑:我发现这个代码在 webview 客户端之外运行的原因:
while(true){
if(!accessToken.equals("")){
Log.d(TAG, "not empty");
break;
}
}
我知道这不是一件聪明的事,但是你将如何实现一些监听器机制,等待 accessToken 不为空然后返回它?