【问题标题】:Using proxy server in InAppBrowser - PhoneGap在 InAppBrowser 中使用代理服务器 - PhoneGap
【发布时间】:2014-05-09 22:48:29
【问题描述】:

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

这是我现在拥有的代码:

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

注意:此快照来自官方 phonegap 文档。

问候, 达乌德

【问题讨论】:

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


    【解决方案1】:

    您可以在运行函数中更改 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
    }   
    

    【讨论】:

      猜你喜欢
      • 2012-05-16
      • 2015-07-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-05-07
      相关资源
      最近更新 更多