-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathMyWebViewClient.java
More file actions
129 lines (107 loc) · 4 KB
/
MyWebViewClient.java
File metadata and controls
129 lines (107 loc) · 4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
package com.webviewbasicauthtest;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.Context;
import android.content.DialogInterface;
import android.graphics.Bitmap;
import android.net.http.SslError;
import android.webkit.CookieManager;
import android.webkit.HttpAuthHandler;
import android.webkit.SslErrorHandler;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
public class MyWebViewClient extends WebViewClient {
private String loginCookie;
private Context mContext;
private WebView mWebView;
public MyWebViewClient(Context context, WebView webview) {
super();
mContext = context;
mWebView = webview;
}
@Override
public void onPageStarted( WebView view, String url, Bitmap favicon ) {
}
@Override
public void onPageFinished( WebView view, String url ) {
CookieManager cookieManager = CookieManager.getInstance();
cookieManager.setCookie(url, loginCookie);
}
@Override
public void onReceivedError( WebView view, int errorCode, String description, String failingUrl ) {
Toast.makeText(view.getContext(), "ページ読み込みエラー", Toast.LENGTH_LONG).show();
}
@Override
public void onLoadResource( WebView view, String url ){
CookieManager cookieManager = CookieManager.getInstance();
loginCookie = cookieManager.getCookie(url);
}
@Override
public boolean shouldOverrideUrlLoading( WebView view, String url ) {
return false;
}
@Override
public void onReceivedSslError( WebView view, SslErrorHandler handler, SslError error ) {
handler.proceed();
}
@Override
public void onReceivedHttpAuthRequest( WebView view, final HttpAuthHandler handler, final String host, final String realm ){
// 引数hostにはsetHttpAuthUsernamePasswordの第1引数で設定した文字列が入ってきます。
// Android 4.*(もしかすると3.*から)ではなぜか引数hostに勝手にポート番号が追記されてしまいます。
// 具体的には「:80」が追記されてしまいます。
String userName = null;
String userPass = null;
if (handler.useHttpAuthUsernamePassword() && view != null) {
String[] haup = view.getHttpAuthUsernamePassword(host, realm);
if (haup != null && haup.length == 2) {
userName = haup[0];
userPass = haup[1];
}
}
if (userName != null && userPass != null) {
handler.proceed(userName, userPass);
}
else {
showHttpAuthDialog(handler, host, realm, null, null, null);
}
}
private void showHttpAuthDialog( final HttpAuthHandler handler, final String host, final String realm, final String title, final String name, final String password ) {
LinearLayout llayout = new LinearLayout((Activity)mContext);
final TextView textview1 = new TextView((Activity)mContext);
final EditText edittext1 = new EditText((Activity)mContext);
final TextView textview2 = new TextView((Activity)mContext);
final EditText edittext2 = new EditText((Activity)mContext);
llayout.setOrientation(LinearLayout.VERTICAL);
textview1.setText("username:");
textview2.setText("password:");
llayout.addView(textview1);
llayout.addView(edittext1);
llayout.addView(textview2);
llayout.addView(edittext2);
final Builder mHttpAuthDialog = new AlertDialog.Builder((Activity)mContext);
mHttpAuthDialog.setTitle("Basic Authentication")
.setView(llayout)
.setCancelable(false)
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
EditText etUserName = edittext1;
String userName = etUserName.getText().toString();
EditText etUserPass = edittext2;
String userPass = etUserPass.getText().toString();
mWebView.setHttpAuthUsernamePassword(host, realm, name, password);
handler.proceed(userName, userPass);
}
})
.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
handler.cancel();
}
})
.create().show();
}
}