我想將受保護的Blog顯示爲webview。 博客是對谷歌的blogger.com如何在WebView中顯示受保護的Blog(setHttpAuthUsernamePassword)
用戶名密碼&由應用程序本身(使用戶不必輸入任何東西)提供:
我用下面的代碼,但它不起作用,因爲它要求用戶輸入其用戶名和密碼(僅在第一個日誌中,然後保存並且工作正常)?怎麼了 ?我應該被使用在「境界」:
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
我沒有看到這樣的境界這裏是有問題的博客:。http://theodiousscribe.blogspot.com - 什麼境界 – Hubert
這是一個表單身份驗證網站,而不是一個基本的身份驗證網站:您不能使用webview.setHttpAuthUsernamePassword來對該博客進行身份驗證,您必須查看使用cookie登錄 – Femi
好吧,我會研究一下,你知道有關如何做這件事的好tuto嗎?我從來沒有真正使用過Cookie,它看起來很基本,但是如何識別正確的cookie(名稱 - 值對)和URL +重新指導在這種情況下使用...這就是我需要的。特別是谷歌的blogger.com。但我想任何嘟嘟Gm所有人也會沒事的。 – Hubert