クライアント エンド ライブラリを 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 が空でなくなるまで待機してそれを返すリスナー メカニズムをどのように実装しますか?