採用HttpURLConnection方式呼叫第三方介面,介面的返回值經過unicode編碼處理
阿新 • • 發佈:2019-01-04
在介面的呼叫過程中,第三方介面的返回值使用unicode處理了,呼叫過程中一直報錯
改成下面這樣就可以了
public class TestStatic { public static void main(String[] args) throws Exception { getDatas(); } public static void getDatas(){ HttpURLConnection conn = null; try { String INFORMATION_URL = "http://250.250.250:8080/datasnap/rest/TInterFace/getoverdayreaderinfor"; conn = getConnection(INFORMATION_URL, "GET"); conn.setConnectTimeout(30000); conn.setReadTimeout(30000); conn.connect(); String resp = ""; try { BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream(), "utf-8")); String next = null; while ((next = reader.readLine()) != null) { resp += next; } } catch (IOException e) { e.printStackTrace(); } conn.disconnect(); System.out.println(resp); } catch (Exception e) { e.printStackTrace(); } } private static HttpURLConnection getConnection(String urlString,String verb) { HttpURLConnection conn = null; try { URL url = new URL(urlString); conn = (HttpURLConnection)url.openConnection(); conn.setRequestMethod(verb); conn.setDoInput(true); conn.setDoOutput(true); } catch (Exception e) { e.printStackTrace(); } return conn; } }