为Android应用程序中的特定URL设置代理

时间:2018-08-25 11:31:42

标签: java android proxy httpurlconnection vpn

enter code here我在项目中使用的是OneSignal。从proxy开始使用时如何设置OneSignal

以下是我的代码,但没有proxy在互联网上使用:

    new Thread(new Runnable() {
        @Override
        public void run() {
            try {
                String userName = "userNAme";
                String pass = "Pass";
                Authenticator authenticator = new Authenticator() {

                    public PasswordAuthentication getPasswordAuthentication() {
                        return (new PasswordAuthentication(userName, pass.toCharArray()));
                    }
                };
                Authenticator.setDefault(authenticator);
                Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress("xx.xxx.xx", 443));
                HttpURLConnection connection = (HttpURLConnection) new URL("https://onesignal.com/").openConnection(proxy);
                connection.usingProxy();
                connection.setAllowUserInteraction(true);
                connection.connect();
                if (connection.getResponseCode() == 200) {
                    OneSignal.startInit(App.this)
                            .inFocusDisplaying(OneSignal.OSInFocusDisplayOption.Notification)
                            .setNotificationReceivedHandler(new NotificationReceivedHandler())
                            .setNotificationOpenedHandler(new NotificationOpenedHandler(App.this))
                            .unsubscribeWhenNotificationsAreDisabled(true)
                            .init();
                }
            } catch (Exception ex) {

            }

        }
    }).start();

0 个答案:

没有答案