java 调用接口事例

//调用接口

String inteFaceUrl = OperateProperties.readValueByKey("functionInteFace");

String strurl = inteFaceUrl+"/rest/bmwebservice/getreporthichar?towntype=1&type=1&datetype=1";

try {

HttpURLConnection con = null;//getSMSRecord

URL url = new URL(strurl);

con = (HttpURLConnection)url.openConnection();

con.setRequestMethod("POST");

con.setDoInput(true);

con.setDoOutput(true);

con.setUseCaches(false);

con.setRequestProperty("Accept","application/json");

con.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");

DataOutputStream out = new DataOutputStream(con.getOutputStream());

out.flush();

out.close();

InputStreamReader input=new InputStreamReader(con.getInputStream(), "UTF-8");

BufferedReader bufReader = new BufferedReader(input);  

String line = "";  

StringBuilder contentBuf = new StringBuilder();  

while ((line = bufReader.readLine()) != null) {  

contentBuf.append(line);  

}

System.out.println(contentBuf.toString());

//处理数据

JSONObject json  = JSONObject.fromObject(contentBuf.toString());

JSONArray eventList = json.getJSONArray("data1");

data1=eventList.toString();

// request.setAttribute("data1", eventList.toString());

return "listreporthichar";

} catch (MalformedURLException e) {

e.printStackTrace();

} catch (ProtocolException e) {

e.printStackTrace();

} catch (UnsupportedEncodingException e) {

e.printStackTrace();

} catch (IOException e) {

e.printStackTrace();

}


你可能感兴趣的:(java 调用接口事例)