インターネットで 3 日以上検索しましたが、解決策が見つかりません。ログイン用の http 投稿を行っています。その後、ログインしている Web サーバーのリンクである Web ビューを開きます。問題は、webview がページを認証済みとして表示していないことです。助けてください。コードは次のとおりです。
`
public class MainActivity extends Activity {
private String sessionCookie;
private CookieManager cookieManager;
private static final int TIMEOUT_MS = 3000;
private WebView mWebView;
private static final String redirURL = "http://slateisb.nu.edu.pk/portal/relogin";
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.activity_main);
//------------------ COOKIES -----------------------//
CookieSyncManager.createInstance(this);
CookieManager cookieManager = CookieManager.getInstance();
Date dateObj = new Date();
dateObj.setTime(dateObj.getTime() + 2 * 7 * 24 * 60 * 60 * 1000);
String sA = "acc=" + 0;
String sL = "lgn=";
SimpleDateFormat postFormater = new SimpleDateFormat("EEE MMM dd yyyy HH:mm:ss zzz");
String oD = postFormater.format(dateObj);
String cookieString = "logondata=" + sA + "&" + sL + "; expires="+ oD;
cookieManager.setCookie(redirURL, cookieString);
cookieManager.getCookie(redirURL);
CookieSyncManager.getInstance().sync();
//------------------ WEBVIEW -----------------------//
CookieSyncManager.createInstance(this);
CookieSyncManager.getInstance().startSync();
mWebView = (WebView) findViewById(R.id.webView1);
WebSettings webSettings = mWebView.getSettings();
webSettings.setSavePassword(true);
webSettings.setSaveFormData(true);
webSettings.setJavaScriptEnabled(true);
webSettings.setSupportZoom(false);
mWebView.setWebViewClient(new WebViewClient() {
public boolean shouldOverrideUrlLoading(WebView view, String url){
// do your handling codes here, which url is the requested url
// probably you need to open that url rather than redirect:
view.loadUrl(url);
return false; // then it is not handled by default action
}
});
//------------------------------ HTTP 4.0 REDIRECT --------------------------//
HttpClient httpClient = new DefaultHttpClient();
HttpConnectionParams.setConnectionTimeout(httpClient.getParams(), TIMEOUT_MS);
HttpConnectionParams.setSoTimeout(httpClient.getParams(), TIMEOUT_MS);
HttpPost httpPost = new HttpPost(redirURL);
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("curl", "varl"));
nameValuePairs.add(new BasicNameValuePair("flags", "0"));
nameValuePairs.add(new BasicNameValuePair("forcedownlevel", "0"));
nameValuePairs.add(new BasicNameValuePair("formdir", "9"));
nameValuePairs.add(new BasicNameValuePair("eid", "i120515"));
nameValuePairs.add(new BasicNameValuePair("pw", "password123"));
nameValuePairs.add(new BasicNameValuePair("trusted", "1"));
HttpResponse end = null;
String endResult = null;
try {
httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpClient.execute(httpPost);
end = response;
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
BasicResponseHandler myHandler = new BasicResponseHandler();
try {
endResult = myHandler.handleResponse(end);
} catch (HttpResponseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//mWebView.loadData(endResult, "text/html", "utf-8");
mWebView.loadUrl("http://slateisb.nu.edu.pk");
//mWebView.loadDataWithBaseURL("http://slateisb.nu.edu.pk/portal/pda", endResult, "text/html", "utf-8", null);
}
@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;
}
} `