我正在做一个使用WebView和onStartUp的Android应用程序,它会加载我在onCreate方法中提供的Url。 Url需要身份验证,我需要提供用户提供凭据(用户名,密码)。所以在菜单设置中,我给出了两个选项,"设置"和"退出"。在"设置"选项,我已经给出了URL,用户名和密码。我想将onReceivedHttpAuthRequest()方法中的这些值传递给Authenticate User,但它不起作用。我在网上冲浪时尝试了我的水平,最后我在这里。我在下面给出了我的代码。请指导并纠正我。这个项目非常重要!对我来说。
BaiBrower.java
import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.preference.PreferenceManager;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.Window;
import android.view.WindowManager;
import android.webkit.HttpAuthHandler;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.EditText;
import android.widget.Toast;
public class BaiBrowser extends Activity
{
private static final int RESULT_SETTINGS = 1;
private WebView webView;
public String URL, USERNAME, PASSWORD;
EditText user,password;
private int refreshrate;
private String noRefresh;
private String URLPref;
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.webpage);
//Get webview
webView = (WebView) findViewById(R.id.webView1);
URL = getURL();
USERNAME = getUser();
PASSWORD = getPassword();
startWebView(URL,USERNAME,PASSWORD);
}
@SuppressLint("SetJavaScriptEnabled")
private void startWebView(final String url, final String Username, final String Password)
{
//Create new webview Client to show progress dialog
//When opening a url or click on link
webView.setWebViewClient(new WebViewClient()
{
ProgressDialog progressDialog;
//If you will not use this method url links are opeen in new brower not in webview
public boolean shouldOverrideUrlLoading(WebView view, String url)
{
view.loadUrl(url);
return true;
}
//Show loader on url load
public void onLoadResource (WebView view, String url)
{
if (progressDialog == null) {
// in standard case YourActivity.this
progressDialog = new ProgressDialog(BaiBrowser.this);
progressDialog.setMessage("Loading...");
progressDialog.show();
}
}
public void onPageFinished(WebView view, String url)
{
try
{
if (progressDialog.isShowing())
{
progressDialog.dismiss();
progressDialog = null;
}
}
catch(Exception ex)
{
Log.e("ProgressDialog Error", ""+ex);
}
}
@Override
public void onReceivedHttpAuthRequest(WebView view, final HttpAuthHandler handler, String host, String realm)
{
// TODO Auto-generated method stub
handler.proceed(Username, Password);
}
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl)
{
if (errorCode == 401)
{
Toast.makeText(null, "Need Your Credentials", 2000).show();
}
}
});
// Javascript inabled on webview
webView.getSettings().setJavaScriptEnabled(true);
// Other webview options
webView.getSettings().setLoadWithOverviewMode(true);
webView.getSettings().setUseWideViewPort(true);
webView.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY);
webView.setScrollbarFadingEnabled(false);
webView.getSettings().setBuiltInZoomControls(true);
webView.setHttpAuthUsernamePassword(url, null, Username, Password);
/*
String summary = "<html><body>You scored <b>192</b> points.</body></html>";
webview.loadData(summary, "text/html", null);
*/
//Load url in webview
webView.loadUrl(url);
}
// Open previous opened link from history on webview when back button pressed
@Override
// Detect when the back button is pressed
public void onBackPressed()
{
if(webView.canGoBack())
{
webView.goBack();
}
else
{
// Let the system handle the back button
finish();
System.exit(0);
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu)
{
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item)
{
switch (item.getItemId())
{
case R.id.settings:
Intent prefIntent = new Intent(this, Preferences.class);
startActivityForResult(prefIntent, RESULT_SETTINGS);
break;
case R.id.exit:
finish();
System.exit(0);
default:
return super.onOptionsItemSelected(item);
}
return true;
}
public String getURL()
{
final SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(this);
SharedPreferences.Editor prefEditor = sharedPrefs.edit();
prefEditor.putString("prefUrl", sharedPrefs.getString("prefUrl", "http://10.227.100.8:8080/"));
prefEditor.commit();
String Url = sharedPrefs.getString("prefUrl", "http://10.227.100.8:8080/");
return Url;
}
public String getUser()
{
final SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(this);
SharedPreferences.Editor prefEditor = sharedPrefs.edit();
prefEditor.putString("Username", sharedPrefs.getString("Username", "BRANDIXLK\\BAIICTIntern4"));
prefEditor.commit();
String User = sharedPrefs.getString("Username", "BRANDIXLK\\BAIICTIntern4");
return User;
}
public String getPassword()
{
final SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(this);
SharedPreferences.Editor prefEditor = sharedPrefs.edit();
prefEditor.putString("Password", sharedPrefs.getString("Password", "pass@1234"));
prefEditor.commit();
String User = sharedPrefs.getString("Password", "pass@1234");
return User;
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode)
{
case RESULT_SETTINGS: final String URL = getURL();
final String User = getUser();
final String Pass = getPassword();
new CountDownTimer(365 * 24 * 60 * 60, 60000)
{
public void onTick(long millisUntilFinished)
{
startWebView(URL, User, Pass);
}
public void onFinish()
{
startWebView(URL, User, Pass);
}
}.start();
break;
}
}
}
的preferences.xml
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="match_parent" >
<PreferenceCategory android:title="@string/pref_url_setting" >
<EditTextPreference
android:title="@string/pref_url"
android:summary="@string/pref_url_summary"
android:defaultValue="http://10.227.100.8:8080/"
android:key="prefUrl"/>
</PreferenceCategory>
<PreferenceCategory android:title="@string/pref_other_setting" >
<EditTextPreference
android:title="@string/pref_sync_frequency"
android:summary="@string/pref_sync_frequency_summary"
android:key="prefRefreshRate"/>
</PreferenceCategory>
<PreferenceCategory android:title="@string/authentication" >
<EditTextPreference
android:title="@string/username"
android:summary="@string/username_summary"
android:defaultValue="BRANDIXLK\\BAIICTIntern4"
android:key="Username"/>
<EditTextPreference
android:title="@string/password"
android:summary="@string/password_summary"
android:defaultValue="pass@1234"
android:key="Password"/>
</PreferenceCategory>
</PreferenceScreen>
menu.xml文件
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:id="@+id/settings"
android:icon="@drawable/icon_preferences"
android:title="@string/settings">
</item>
<item android:id="@+id/exit"
android:icon="@drawable/icon_exit"
android:title="@string/exit">
</item>
</menu>
注意:如果我直接在&#34; handler.proceed(用户名,密码)中传递凭证值,则身份验证成功;&#34;。但是将值传递给它失败了。请指导我完成项目。
答案 0 :(得分:0)
最后我找到答案,我只是将代码行webView.setHttpAuthUsernamePassword(url, null, Username, Password);
放在方法
webView.setWebViewClient(new WebViewClient()
{ ---------
-------
-------
}
由于