나는 웹뷰 내용의 텍스트 문자열을 얻는 방법을 읽었다 고 생각한다. 그런 다음 jsoup를 사용하여 구문 분석 할 수 있습니다. [neh, 심지어 jsoup 필요가 없습니다. indexOf string check]
HTTP 클라이언트로 로그인을 처리하는 것이 좋습니다. 그것은 당신에게 유연성을 제공하며 더 적절한 방법으로 보입니다. 나는 HTTP get 및 post 요청을 위해 loopj 라이브러리를 사용 해왔다. 단순한 코드가 가능합니다. 어쨌든 나를 위해, 상대적으로 안드로이드 초보자. 내 프로젝트에서 얻은 몇 가지 코드가 있습니다. 진행률 표시 줄 및 쿠키 관리 같은 것을 생략했습니다.
private void loginFool() {
String urlString = "http://www.example.com/login/";
// username & password
RequestParams params = new RequestParams();
params.put("username", username.getText().toString());
params.put("password", password.getText().toString());
// send the request
loopjClient.post(urlString, params, new TextHttpResponseHandler() {
@Override
public void onStart() {
// called before request is started
//System.err.println("Starting...");
}
@Override
public void onSuccess(int statusCode, Header[] headers, String responseString) {
// called when response HTTP status is "200 OK"
// see if 'success' was a failed login...
int idx = responseString.indexOf("Please try again!");
if(idx > -1) {
makeMyToast("Sorry, login failed!");
}
// or actual success-ful login
else {
// manage cookies here
// put extractData in separate thread
final String responseStr = responseString;
new Thread(new Runnable() {
public void run(){
extractData(responseStr);
selectData(defaultPrefs.getInt("xyz_display_section", 0));
// start the next activity
Intent intent = new Intent(MainActivity.this, PageViewActivity.class);
startActivity(intent);
finish();
}
}).start();
}
}
@Override
public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) {
// called when response HTTP status is "4XX" (eg. 401, 403, 404)
makeMyToast("Whoops, network error!");
}
@Override
public void onFinish() {
// done
}
});
}
당신은 onFailure 콜백에서, 내가 네트워크 오류 메시지를주고 응답 핸들러의는 onSuccess 콜백에서, 내가 로그인이 실패하는지, 문자열을 테스트 할 수 있습니다, 그것을 참조 할 수 있습니다.
나는이 유형의 포스트 로그인이 작동하는 웹 서버의 비율을 알만큼 충분히 경험이 없습니다.
loopj 클라이언트는 쿠키를 수신하고 관리합니다. webview를 통해 사이트의 페이지에 액세스하는 경우 loopj 클라이언트의 쿠키를 webview로 복사해야합니다.
private AsyncHttpClient loopjClient = new AsyncHttpClient();
private PersistentCookieStore xyzCookieStore;
xyzCookieStore = new PersistentCookieStore(this);
loopjClient.setCookieStore(Utility.xyzCookieStore);
: I 클래스 변수 정의 및 초기화를 포함해야, 그리고 :
// get cookies from the generic http session, and copy them to the webview
CookieSyncManager.createInstance(getApplicationContext());
CookieManager.getInstance().removeAllCookie();
CookieManager cookieManager = CookieManager.getInstance();
List<Cookie> cookies = xyzCookieStore.getCookies();
for (Cookie eachCookie : cookies) {
String cookieString = eachCookie.getName() + "=" + eachCookie.getValue();
cookieManager.setCookie("http://www.example.com", cookieString);
//System.err.println(">>>>> " + "cookie: " + cookieString);
}
CookieSyncManager.getInstance().sync();
// holy crap, it worked; I am automatically logged in, in the webview
편집 : 나는 그렇게 몇 온라인 게시물에서 코드를 자갈길