开发者

How to display a protected Blog into a WebView (setHttpAuthUsernamePassword)

开发者 https://www.devze.com 2023-03-18 23:23 出处:网络
I would like to show a protected Blog into a webview. The blog is on Google\'s blogger.com The username & password are provided by the app itself (so that the user does not have to type in anyth

I would like to show a protected Blog into a webview. The blog is on Google's blogger.com

The username & password are provided by the app itself (so that the user does not have to type in anything):

I'm using the following code but it does not work, as it is asking the user to type in its username and password (at the first log only, then it is saved and it works fine)? What's wrong ? What should I be using as 'realm' in :

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

The whole code so far :

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();
}
// =================================================================================

}

I have tried to follow the advice here but it looks like

p开发者_Python百科ublic 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);
    }

never gets triggered (in MyWebViewClient) for whatever reason ?

EDIT : Maybe I need to implement a client-side OAuth on Android ?

like here


From setHttpAuthUsernamePassword, you need to pass in the host of the site: so if you are accessing (for example) http://www.google.com with username and password from your code and realm Protected, you would do

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

You can get the realm by looking at the published realm in your browser: for example, in Firefox if you open http://tools.dynamicdrive.com/password/example/ you will see that the following text in the caption: A username and password are being requested by http://tools.dynamicdrive.com. The site says: "Restricted Area. In this case the realm is Restricted Area.

0

精彩评论

暂无评论...
验证码 换一张
取 消