在InAppBrowser中使用代理服务器 - PhoneGap

时间:2014-03-30 17:33:27

标签: cordova cordova-3 cordova-2.0.0 cordova-2.7.0

我正在构建一个使用我的服务器之一作为代理的phonegap应用程序。不幸的是,没有正式的API或文档可以这样做。所以,我的问题是如何在InAppBrowser中使用代理。

以下是我现在拥有的代码:

var ref = window.open('http://apache.org', '_blank', 'location=yes');
ref.addEventListener('loadstart', function() { alert(event.url); });

注意:此快照来自官方的手机文档。

此致 达乌德

1 个答案:

答案 0 :(得分:0)

您可以在run函数中的loadUrl之前更改inappbrowser.java代码,如下所示:

Context appContext = cordova.getActivity().getApplicationContext();
System.setProperty("http.proxyHost", "yourhost");
System.setProperty("http.proxyPort", "yourport");
System.setProperty("https.proxyHost", "yourhost");
System.setProperty("https.proxyPort",  "yourport");
try {
Class applictionCls = Class.forName("android.app.Application");
            Field loadedApkField = applictionCls.getField("mLoadedApk");
            loadedApkField.setAccessible(true);
            Object loadedApk = loadedApkField.get(appContext);
            Class loadedApkCls = Class.forName("android.app.LoadedApk");
            Field receiversField = loadedApkCls.getDeclaredField("mReceivers");
            receiversField.setAccessible(true);
            ArrayMap receivers = (ArrayMap) receiversField.get(loadedApk);
            for (Object receiverMap : receivers.values()) {
                for (Object rec : ((ArrayMap) receiverMap).keySet()) {
                    Class clazz = rec.getClass();
                    if (clazz.getName().contains("ProxyChangeListener")) {
                        Method onReceiveMethod = clazz.getDeclaredMethod("onReceive", Context.class, Intent.class);
                        Intent intent = new Intent(Proxy.PROXY_CHANGE_ACTION);

                        onReceiveMethod.invoke(rec, appContext, intent);
                    }
                }
            }
} catch (Exception e) {
    // TODO
}