Главная > Android > android WebView read cookies

android WebView read cookies

public class MainActivity extends AppCompatActivity {
    private WebView webView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        webView = findViewById(R.id.webView);
        webView.setWebViewClient(new CustomWebViewClient());
    }
    private class CustomWebViewClient extends WebViewClient{
        @Override
        public void onPageFinished(WebView view, String url) {
            String username = "";
            String cookies_all = CookieManager.getInstance().getCookie(url);
            if (cookies_all != null) {
                String[] cookies_array = cookies_all.split(";");
                for (String cookie : cookies_array) {
                    String[] cookie_array = cookie.split("=");
                    if (cookie_array[0].contains("users")){
                        String[] user_data = cookie_array[1].split("%");
                        username=user_data[0];
                        if (!Objects.equals(username, "")) {
                            Toast.makeText(view.getContext(), username, Toast.LENGTH_LONG).show();
                        }
                    }
                }
            }
            super.onPageFinished(view, url);
        }
    }
}
Categories: Android Tags:
  1. Пока что нет комментариев.
Похожие публикации