我使用android WebView
加载Instagram登录页面。它加载很好,我可以在屏幕上看到它。之后我点击"通过Facebook登录"按钮,将我重定向到Facebook登录页面。我输入我的Facebook凭据并单击登录按钮。之后,Facebook将我重定向回Instagram以重定向URI(https://www.instagram.com/accounts/signup)并且此页面无限加载(如果我在浏览器中执行相同的操作,Facebook会将我重定向到同一页面,但此页面不会加载无限,但重定向到我的Instagram帐户的个人资料页面)。我该怎么做才能防止无限加载?这是我的Activity
代码WebView
以及此页面的屏幕截图
public class InstagramAuthActivity extends BaseActivity implements InstagramRepository.OnLoadListener<String> {
public static final String EXTRA_COOKIES = "cookies_instagram";
public static final String EXTRA_USERNAME = "username_instagram";
private static final String URL = "https://www.instagram.com";
private ViewHolder mViewHolder;
private String mCookies;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_instagram_auth);
createViewHolder();
populateViewHolder();
}
private void createViewHolder() {
mViewHolder = new ViewHolder();
mViewHolder.mAuthWebView = (WebView) findViewById(R.id.auth);
}
private void populateViewHolder() {
WebSettings settings = mViewHolder.mAuthWebView.getSettings();
settings.setJavaScriptEnabled(true);
mViewHolder.mAuthWebView.setWebViewClient(new InstagramWebViewClient());
mViewHolder.mAuthWebView.loadUrl(URL);
}
@Override
public void onLoad(String data) {
if (data != null) {
Intent intent = new Intent();
intent.putExtra(EXTRA_COOKIES, mCookies);
intent.putExtra(EXTRA_USERNAME, data);
setResult(RESULT_OK, intent);
} else {
setResult(RESULT_CANCELED);
}
finish();
}
@Override
public void onFail() {
setResult(RESULT_CANCELED);
finish();
}
private class ViewHolder {
WebView mAuthWebView;
}
private class InstagramWebViewClient extends WebViewClient {
@Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
String cookies = CookieManager.getInstance().getCookie(url);
Log.d("tag", "url: " + url + "; cookies: " + cookies);
if (cookies.contains("ds_user_id")) {
mCookies = cookies;
String[] cookieData = cookies.split("; ");
if (cookieData != null && cookieData.length > 1) {
String[] idData = null;
for (String cookie : cookieData) {
if (cookie.contains("ds_user_id")) {
idData = cookie.split("=");
break;
}
}
if (idData != null && idData.length > 1) {
InstagramRepository.getInstance(InstagramAuthActivity.this).getUsername(idData[1], InstagramAuthActivity.this);
}
}
}
}
@Override
public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) {
handler.proceed();
}
@Override
public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error) {
super.onReceivedError(view, request, error);
}
}
}
此处还记录了我重定向到的所有网址
09-14 10:53:14.555 21316-21316/ru.luckybook D/tag: url: https://www.instagram.com/
09-14 10:53:15.539 21316-21316/ru.luckybook D/tag: url: https://www.instagram.com/
09-14 10:53:22.048 21316-21316/ru.luckybook D/tag: url: https://m.facebook.com/login.php?skip_api_login=1&api_key=124024574287414&signed_next=1&next=https%3A%2F%2Fm.facebook.com%2Fv1.0%2Fdialog%2Foauth%3Fredirect_uri%3Dhttps%253A%252F%252Fwww.instagram.com%252Faccounts%252Fsignup%252F%26scope%3Demail%26response_type%3Dtoken%252Cgranted_scopes%26client_id%3D124024574287414%26ret%3Dlogin%26logger_id%3D7b5f7abf-6769-464a-86e9-a26e8301375d&cancel_url=https%3A%2F%2Fwww.instagram.com%2Faccounts%2Fsignup%2F%3Ferror%3Daccess_denied%26error_code%3D200%26error_description%3DPermissions%2Berror%26error_reason%3Duser_denied%23_%3D_&display=touch&locale=ru_RU&logger_id=7b5f7abf-6769-464a-86e9-a26e8301375d&_rdr
09-14 10:53:46.864 21316-21316/ru.luckybook D/tag: url: https://www.instagram.com/accounts/signup/
答案 0 :(得分:1)
当您创建WebView
时,您只需在CookieManager.getInstance().setAcceptThirdPartyCookies(mWebView,true);
中启用第三方Cookie,如下所示:
WebView
在此之后,Instagram将在#include<stdio.h>
int main() {
char c='1';
int i=10;
printf("%c", c|i);
}
开始正常工作。我遇到了同样的问题,我通过实施上述代码解决了这个问题。
答案 1 :(得分:-1)
我找到了一个解决方案,希望它将对其他人有所帮助。
以下块很重要,
webSettings.setDomStorageEnabled(true);
private void populateViewHolder() {
WebSettings settings = mViewHolder.mAuthWebView.getSettings();
settings.setJavaScriptEnabled(true);
webSettings.setDomStorageEnabled(true);
mViewHolder.mAuthWebView.setWebViewClient(new InstagramWebViewClient());
mViewHolder.mAuthWebView.loadUrl(URL);
}
启用此选项后,Instagram正常运行。
我认为是disabled by default for space savings and security
。