gpt4 book ai didi

android - 如何将 protected 博客显示到 WebView 中(setHttpAuthUsernamePassword)

转载 作者:行者123 更新时间:2023-11-29 18:18:51 25 4
gpt4 key购买 nike

我想在 WebView 中显示 protected 博客。该博客在 Google 的 blogger.com 上

用户名和密码由应用程序本身提供(因此用户无需输入任何内容):

我正在使用以下代码,但它不起作用,因为它要求用户输入其用户名和密码(仅在第一个日志中,然后被保存并且工作正常)?怎么了 ?我应该将什么用作“领域”:

webview.setHttpAuthUsernamePassword(URL_WebView, "", getString(R.string.username), getString(R.string.password));

到目前为止的全部代码:

public class Main extends Activity {
/** Called when the activity is first created. */

String URL_WebView = "http://myblog.blogspot.com/";

public static Context mContext;
static SharedPreferences prefs;

static int log_number;
Boolean terms_agreed;

//GoogleAnalyticsTracker tracker;
//static String analytics_tracker = "UA-11967031-29";

WebView webview;
private static final FrameLayout.LayoutParams ZOOM_PARAMS = new FrameLayout.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT, Gravity.BOTTOM);


@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.main);
this.webview = (WebView)findViewById(R.id.webkitWebView1);

mContext = this;

reading_the_prefs();
log_number = log_number + 1;

if (log_number >= 2) {
if ((!terms_agreed)){
TermsDialog();
}
}

/*
tracker = GoogleAnalyticsTracker.getInstance();
tracker.start(analytics_tracker, this);
tracker.trackPageView("/Main");
*/

// adding the zoom controls ...
FrameLayout mContentView = (FrameLayout) getWindow().getDecorView().findViewById(android.R.id.content);
final View zoom = this.webview.getZoomControls();
mContentView.addView(zoom, ZOOM_PARAMS);
zoom.setVisibility(View.GONE);

WebSettings settings = webview.getSettings();
settings.setJavaScriptEnabled(true);
webview.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY);

webview.setWebViewClient(new MyWebViewClient ());

//webview.savePassword(URL_WebView, getString(R.string.username), getString(R.string.password));
webview.setHttpAuthUsernamePassword(URL_WebView, "", getString(R.string.username), getString(R.string.password));
webview.loadUrl(URL_WebView);

}

//======================================================================================

private class MyWebViewClient extends WebViewClient {

public void onReceivedHttpAuthRequest(WebView view,
HttpAuthHandler handler, String host, String realm) {

handler.proceed(getString(R.string.username), getString(R.string.password));
Log.i("Hub","Host ="+host+" with realm ="+realm);
}

public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
Log.e("Hub", "Error: " + description);
Toast.makeText(Main.this, "Oh no! " + description, Toast.LENGTH_LONG).show();
}
}

//======================================================================================

@Override
public boolean onCreateOptionsMenu(Menu menu) {
menu.add(0, 99, 3, "Exit").setIcon(android.R.drawable.ic_menu_delete);
menu.add(0, 999, 2, "Terms of Service").setIcon(android.R.drawable.ic_menu_agenda);
return super.onCreateOptionsMenu(menu);
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch(item.getItemId()){
case 99:
finish();
break;
case 999:
//tracker.trackPageView("/TERMS_OF_SERVICE");
TermsDialog();
break;
}
return super.onOptionsItemSelected(item);
}

// =================================================================================
public void TermsDialog(){

AlertDialog.Builder ad = new AlertDialog.Builder(this);
ad.setIcon(R.drawable.icon);
// custom Title - that's the way I found to center it !
TextView title = new TextView(mContext);
title.setText(R.string.TermsDialog_Title);
title.setBackgroundColor(Color.BLACK);
title.setPadding(10, 10, 10,10);
title.setGravity(Gravity.CENTER);
title.setTextColor(Color.WHITE);
title.setTextSize(20);
// to center the TITLE :
ad.setCustomTitle(title);

ad.setView(LayoutInflater.from(this).inflate(R.layout.terms_dialog,null));

ad.setPositiveButton(R.string.TermsDialog_Yes,
new android.content.DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int arg1) {
//OK, save accepted Terms and Service into the Prefs.
terms_agreed = true;
//tracker.trackPageView("/Terms_agreed");
saving_the_prefs();
}
}
);

ad.setNegativeButton(R.string.TermsDialog_No,
new android.content.DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int arg1) {
terms_agreed = false;
//tracker.trackPageView("/Terms_NOT_agreed");
saving_the_prefs();
finish();
}
}
);

ad.setCancelable(false);

ad.show();
}

// =================================================================================
private void reading_the_prefs() {

prefs = getSharedPreferences("Market_Wrap", 0);
log_number = prefs.getInt("log_number" , 0);
terms_agreed = prefs.getBoolean("terms", false);
Log.i("Hub", "log_number="+log_number);
Log.i("Hub", "terms_agreed="+terms_agreed);

}
// =================================================================================
public void saving_the_prefs() {

prefs = getSharedPreferences("Market_Wrap", 0);
SharedPreferences.Editor editor = prefs.edit();

editor.putInt("log_number", log_number);
editor.putBoolean("terms", terms_agreed);
editor.commit();
}
// =================================================================================

@Override
protected void onPause() {
super.onPause();

//tracker.dispatch();
//tracker.stop();
saving_the_prefs();
}
// =================================================================================

我已经尝试听从建议 here但它看起来像

public void onReceivedHttpAuthRequest(WebView view,
HttpAuthHandler handler, String host, String realm) {

handler.proceed(getString(R.string.username), getString(R.string.password));
Log.i("Hub","Host ="+host+" with realm ="+realm);
}

无论出于何种原因,永远不会被触发(在 MyWebViewClient 中)?

编辑:也许我需要在 Android 上实现客户端 OAuth?

喜欢here

最佳答案

来自 setHttpAuthUsernamePassword ,您需要传入站点的主机:所以如果您正在访问(例如)http://www.google.com使用您的代码和领域中的用户名和密码 Protected,您可以这样做

webview.setHttpAuthUsernamePassword("www.google.com", "Protected", getString(R.string.username), getString(R.string.password));

您可以通过在浏览器中查看已发布的领域来获取领域:例如,在 Firefox 中,如果您打开 http://tools.dynamicdrive.com/password/example/您会在标题中看到以下文本:http://tools.dynamicdrive.com 正在请求用户名和密码| .该网站说:“限制区域。在这种情况下,领域是限制区域

关于android - 如何将 protected 博客显示到 WebView 中(setHttpAuthUsernamePassword),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6639447/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com