我需要从手机上读取代理信息,如果手机在代理下,我需要在我的springrestemplate对象上设置这个代理。我已经尝试了下面的代码,但没有任何运气,有人能告诉我我做错了什么。

String proxyHost = android.net.Proxy.getHost(activity
            .getApplicationContext());

SimpleClientHttpRequestFactory requestFactory = new SimpleClientHttpRequestFactory();
Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(proxyHost, 8080));
requestFactory.setProxy(proxy);
RestTemplate rest = new RestTemplate(requestFactory);

谢谢你的帮助

最佳答案

我找到了我问题的答案。将在此处发布代码:

 if (proxyHost != null && proxyPort != null) {
        rest = new RestTemplate();
        HttpComponentsClientHttpRequestFactory factory =   ((HttpComponentsClientHttpRequestFactory) rest
                .getRequestFactory());
        DefaultHttpClient defaultHttpClient = (DefaultHttpClient) factory
                .getHttpClient();
        HttpHost proxy = new HttpHost(proxyHost, proxyPort);
        defaultHttpClient.getParams().setParameter(
                ConnRoutePNames.DEFAULT_PROXY, proxy);

    } else {
        rest = new RestTemplate();
        HttpComponentsClientHttpRequestFactory factory = ((HttpComponentsClientHttpRequestFactory) rest
                .getRequestFactory());
        DefaultHttpClient defaultHttpClient = (DefaultHttpClient) factory
                .getHttpClient();

    }

10-08 02:59