2012-10-25 23 views
4

我試圖實施由Chris發現的here提供的解決方法,以允許PhoneGap/Cordova構建的Android應用向服務器發出AJAX HTTPS請求帶有自簽名的SSL證書。我使用PhoneGap/Cordova 2.1.0,而Chris使用1.7.0。我可以毫無問題地創建MyWebViewClient類。然而,當我這行代碼...AJAX請求在Cordova/PhoneGap 2.1.0中自簽名HTTPS資源Android應用

this.setWebViewClient(this.appView, new MyWebViewClient(this)); 

...添加到MainActivity類別重寫init()方法,我收到此錯誤:

The method setWebViewClient(CordovaWebView, MyWebViewClient) is undefined for the type MainActivity

這裏是我的代碼MyWebViewClient.java:

package [packagename]; 

import android.net.http.SslError; 
import android.webkit.SslErrorHandler; 
import android.webkit.WebView; 
import org.apache.cordova.CordovaWebViewClient; 
import org.apache.cordova.DroidGap; 

public class MyWebViewClient extends CordovaWebViewClient { 

    public class MyWebViewClient(DroidGap ctx) { 
     super(ctx); 
    } 

    @Override 
    public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) { 
     handler.proceed(); 
    } 
} 

這裏是我的MainActivity.java代碼:

package [packagename]; 

import android.os.Bundle; 
import org.apache.cordova.*; 

public class MainActivity extends DroidGap { 

    @Override 
    public void init() { 
     super.init(); 
     this.setWebViewClient(this.appView, new MyWebViewClient(this)); // Error occurs here 
    } 

    @Override 
    public void onCreate(bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     super.setBooleanProperty("keeprunning", false); 
     super.loadUrl("file:///android_asset/www/index.html"); 
    } 
} 

我沒有足夠的聲望,否則我只會評論克里斯的答案。此外,我不尋找一個jQuery解決方案(我已經知道我可以做一個$ .ajax()調用來避免這個問題,但我試圖讓我的應用程序保持jQuery)。

任何想法?非常感謝您的幫助!

編輯:請在迴應前查看我的意見。

+0

我改變有問題的代碼行「this.webViewClient =新MyWebViewClient(本);」並且我可以構建應用程序並仍然發出AJAX HTTP請求,但對於具有自簽名SSL證書的服務器的AJAX HTTPS請求,我仍然會收到HTTP狀態碼0。 – Jesse

+0

我也將有問題的代碼行更改爲「this.appView.setWebViewClient(new MyWebViewClient(this));」但是當我啓動應用程序時,我遇到了一個致命異常。 *嘆息* – Jesse

+0

我發現[這篇文章](http://stackoverflow.com/questions/11317813/android-phonegap-timeout-error-when-trying-to-set-a-webviewclient)和測試完全相同的代碼在使用Cordova/PhoneGap 2.1.0的Simon的答案在調用super.onPageStarted(...)時也會導致錯誤。因此,我想我的問題在於,我所採用的方法在Cordova/PhoneGap 2.1.0中不起作用。不過,我認爲必須有一種方法來覆蓋Cordova/PhoneGap 2.1.0中CordovaWebViewClient類的onReceivedSslError(...)方法。任何新鮮的想法? – Jesse

回答

2

這可以在後面的Cordova版本(我使用2.2)上修改如下。如前所述,它在onPageStarted()失敗 - 這是因爲它期待一個appView,它是null,所以你得到一個NullPointerException。設置APPVIEW似乎解決它如

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    super.init(); 

    CordovaWebViewClient webViewClient = new CustomWebViewClient(this); 
    webViewClient.setWebView(this.appView); 
    this.appView.setWebViewClient(webViewClient); 

    super.loadUrl("file:///android_asset/www/index.html"); 

} 

注意,super.init()還需要

+0

謝謝!幸運的是,科爾多瓦的發佈週期最近很短,我現在也在使用2.2版本。非常感激。 – Jesse