1

我正在編寫一個測試webview應用程序,其中我打算在啓動應用程序視圖的那一刻打開我的雅虎郵件。這將是一個webview,在那裏我想對我的用戶名和密碼進行硬編碼。我知道雅虎使用get方法而不是post方法。有沒有辦法實現這一目標? 這裏是我到目前爲止的代碼:如何編寫webview以自動登錄到我的雅虎帳戶?

Webview webview = (WebView) getView().findViewById(R.id.mywebview); 
webview.setBackgroundColor(0); 
      webview.getSettings().setJavaScriptEnabled(true); 

      webview.setWebViewClient(new webClient()); 
      String url = "https://www.yahoomail.com"; 
      webview.loadUrl(url); 

private class webClient extends WebViewClient { 
     @Override 
     public void onPageStarted(WebView view, String url, Bitmap favicon) { 
      view.setVisibility(View.VISIBLE); 

     } 
     @Override 
     public void onPageFinished(WebView view, String url) { 

      final Animation fade = new AlphaAnimation(0.0f, 1.0f); 
      fade.setDuration(200); 
      view.startAnimation(fade); 

      view.setVisibility(View.VISIBLE); 

     } 
     public void animate(final WebView view) { 
      final Animation anim = AnimationUtils.loadAnimation(getActivity(), 
        R.anim.slide_in_from_left); 
      view.startAnimation(anim); 
     } 
     @Override 
     public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) { 
      Toast.makeText(view.getContext(), "Authentication Error", Toast.LENGTH_LONG).show(); 
      CookieManager cookieManager = CookieManager.getInstance(); 
      cookieManager.setCookie("[email protected]", "yahoopass"); 
      super.onReceivedError(view, errorCode, description, failingUrl); 
     } 

     @Override 
     public void onLoadResource(WebView view, String url){ 

     } 

     @Override 
     public boolean shouldOverrideUrlLoading(WebView view, String url) { 

      return super.shouldOverrideUrlLoading(view, url); 

     } 

     @Override 
     public void onReceivedHttpAuthRequest(WebView view, final HttpAuthHandler handler, final String host, final String realm){ 

      handler.proceed("[email protected]", "yahoopass"); 

     } 

    } 

回答

0

http://developer.android.com/reference/android/webkit/WebView.html

使用loadURL(字符串URL)
加載指定的URL。

postUrl(字符串URL,字節[] POSTDATA)
加載使用具有POSTDATA的URL 「POST」 方法進入這個web視圖。

因此,而不是使用:

String url = "https://www.yahoomail.com"; 
webview.loadUrl(url); 

使用:

String url = "http://example.com/somepage.php"; 
String postData = "postvar=value&postvar2=value2"; 
webView.postUrl(url, EncodingUtils.getBytes(postData, "base64")); 

您可以提取您從登錄頁面需要爲雅虎的增值經銷商。

+0

我如何使用posturl for yahoomail.com? –

+0

這是什麼? postvar =值&postvar2 =值2 –