Android button as WebView click link for Logout (or delete sessionId)

梦想的初衷 提交于 2019-12-25 02:08:14

问题


In my WebView android, I want the user to logout on button click or any other way. I don't want to show any webpage after clicking that button. After that app should auto-exit(System.exit(0) will do that)

Click I want to send is:

http://192.100.1.1:10000/app?service=direct/1/UserSummary/$UserBorder.logoutLink

What's the best way of doing this?


回答1:


Use Rest to make a get call to this URL.

To make it use an async task:

See an example here.

In your do in background do:

protected String doInBackground(String... urls) {
    String url = urls[0];
    HttpClient httpclient = new DefaultHttpClient(getHttpParams());
    HttpResponse response = null;
    try {
        switch (taskType) {
        HttpGet httpget = new HttpGet(url);
        response = httpclient.execute(httpget);  
    } catch (Exception e) {
        Log.e(TAG, e.getLocalizedMessage(), e);
    }
    return response;
}

private HttpParams getHttpParams() {

    HttpParams htpp = new BasicHttpParams();

    HttpConnectionParams.setConnectionTimeout(htpp, CONN_TIMEOUT);
    HttpConnectionParams.setSoTimeout(htpp, SOCKET_TIMEOUT);

    return htpp;
}



回答2:


The main reason behind this question was this logout link has nothing to display. So I wanted something like click to logout. Simple Android button in Main Activity didn't help for this.

So, I created new blank activity, used same layout I was using, but instead of showing blank layout, I added ProcessDialog and made it non-cancel-able as follows:

    final ProgressDialog pd = new ProgressDialog(Logout.this);
    pd.setCancelable(false);
    pd.setCanceledOnTouchOutside(false);
    pd.setMessage("Please wait...");
    pd.show();

And in public void onPageFinished(WebView view, String url) method, I dismissed this ProcessDialog and fired an intent for login page of my app. Also overridden the public boolean shouldOverrideUrlLoading(WebView view, String url) to prevent it from going to out of app.

After that, simply webView.loadUrl(url);.

This way I fixed my problem. (If you know better and simpler way of doing it, please post.)



来源:https://stackoverflow.com/questions/29240160/android-button-as-webview-click-link-for-logout-or-delete-sessionid

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!