如何将受保护的博客显示到WebView(setHttpAuthUsernamePassword)

时间:2011-07-10 05:41:53

标签: android webview

我想将受保护的博客显示在网页视图中。 该博客位于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

1 个答案:

答案 0 :(得分:3)

setHttpAuthUsernamePassword开始,您需要传入网站的主机:因此,如果您使用代码和领域Protected中的用户名和密码访问(例如)http://www.google.com,你会做的

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提出要求。网站上写着:“受限区域。在这种情况下,领域是受限区域