如何解决androidspring框架资源访问异常?

vcudknz3  于 2021-07-23  发布在  Java
关注(0)|答案(0)|浏览(157)

我在连接api时收到以下错误消息。
不会每次都出错。请对此提出宝贵建议。

org.springframework.web.client.ResourceAccessException: I/O error: Unable to resolve host "website.name.org": No address associated with hostname; nested exception is java.net.UnknownHostException: Unable to resolve host "website.name.org": No address associated with hostname

我的代码:

Object responseObject = refSpringRequest(object);

public String refSpringRequest(JSONObject refObject) throws Exception{
        ResponseEntity<String> response = null;
        String resString;
        try {
            //.......................................................
            RestTemplate restTemplate = new RestTemplate();
            StringHttpMessageConverter sconverter = new StringHttpMessageConverter();
            restTemplate.getMessageConverters().add(sconverter);
            restTemplate.getMessageConverters().add(new org.springframework.http.converter.xml.SourceHttpMessageConverter());
            restTemplate.getMessageConverters().add(new org.springframework.http.converter.FormHttpMessageConverter());
            org.springframework.http.converter.json.MappingJacksonHttpMessageConverter converter = new org.springframework.http.converter.json.MappingJacksonHttpMessageConverter();
            converter.getObjectMapper().configure(SerializationConfig.Feature.WRITE_NULL_MAP_VALUES, false);
            converter.getObjectMapper().configure(SerializationConfig.Feature.WRITE_NULL_PROPERTIES, false);
            converter.getObjectMapper().configure(SerializationConfig.Feature.WRITE_CHAR_ARRAYS_AS_JSON_ARRAYS, true);
            restTemplate.getMessageConverters().add(converter);
            restTemplate.setRequestFactory( new HttpComponentsClientHttpRequestFactory());
            restTemplate.setRequestFactory(new HttpComponentsClientHttpRequestFactory(HttpUtils.getNewHttpClient()));
            //.......................................................
            HttpHeaders headers = new HttpHeaders();
            headers.setContentType(MediaType.APPLICATION_JSON);
            HttpEntity<String> entity = new HttpEntity<String>(refObject.toString(), headers);//ZedAPI.URL_PAYMENT_REFERENCE
            response = restTemplate.exchange(ZedAPI.URL_PAYMENT_REFERENCE_SPRING, HttpMethod.POST, entity, String.class);
            resString = response.getBody();
        } catch(HttpStatusCodeException e) {
            resString=e.getResponseBodyAsString();
        }
        return resString;
    }

util类:

public class HttpUtils {
    public static HttpClient getNewHttpClient() {
        try {
            KeyStore trustStore = KeyStore.getInstance(KeyStore.getDefaultType());
            trustStore.load(null, null);

            SSLSocketFactory sf = new EasySSLSocketFactory(trustStore);
            sf.setHostnameVerifier(
                    SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);

            HttpParams params = new BasicHttpParams();
            HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
            HttpProtocolParams.setContentCharset(params, HTTP.UTF_8);

            SchemeRegistry registry = new SchemeRegistry();
            registry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
            registry.register(new Scheme("https", sf, 443));

            ClientConnectionManager ccm = new ThreadSafeClientConnManager(params, registry);

            return new DefaultHttpClient(ccm, params);
        } catch (Exception e) {
            return new DefaultHttpClient();
        }
    }
}

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题