访问接口,并返回获取数据 GPS公司给了我们一个接口(ip+端口+车牌号),查询车辆的位置。我们公司需要自己写服务端,怎样在服务端访问那个给定的接口,并返回查询的数据,并怎样解析数据?我刚毕业,这个项目有压力啊,还求各位大神帮助。访问接口 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 你知道怎么用浏览器访问吧?ip+端口+车牌号这一串敲到浏览器中,回车应该就有结果了。那直接用代码的话,就是用代码模拟你手动敲地址回车的过程,去看看HttpClient相关的东西。来段参考代码:public static String request(String strUrl) { strUrl = QuhaoConstant.HTTP_URL + strUrl; URL url = null; String result = ""; HttpURLConnection urlConn = null; InputStreamReader in = null; try { url = new URL(strUrl); urlConn = (HttpURLConnection) url.openConnection(); urlConn.setConnectTimeout(1000 * 20); in = new InputStreamReader(urlConn.getInputStream()); BufferedReader br = new BufferedReader(in); String readerLine = null; while ((readerLine = br.readLine()) != null) { result += readerLine; } in.close(); urlConn.disconnect(); } catch (MalformedURLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { try { in.close(); } catch (IOException e) { e.printStackTrace(); } urlConn.disconnect(); } return result; } 给你的接口应该是基于http的, 就是rest风格的webservice,直接访问,比如用httpClient public class Demo { public void http() { URL url=null; URLConnection conn=null; try{ url=new URL("需要访问的URL"); conn=url.openConnection(); System.out.println(url.getContent()); System.out.println(url.getAuthority()); System.out.println("*********************************************"); System.out.println(conn.getContent()); System.out.println(conn.getContentType()); }catch(Exception e){ e.printStackTrace(); } try { Runtime.getRuntime().exec("rundll32 url.dll,FileProtocolHandler "+"需访问的URL" ); } catch (IOException e) { e.printStackTrace(); } } public static void main(String[] args) { Demo de=new Demo(); de.http(); }} 求助S2SH整合的分页代码 定时器问题 java进阶请大家指教 Struts2初级异常问题 hibernate 怎么实现嵌套子查询 ? JSP页面运行时提示找不到此属性和方法 站内短信如何实现 请问ejb1.x与2.0有什么不同? 请问servlet用getInputStream取得流以后,怎么解决中文问题啊?按照书上的写法始终不行! 运行jbuilder+ORACLE需要什么样的电脑配置? 谁能谈谈j2ee和php孰优孰劣 J2EE和Mysql中文问题
那直接用代码的话,就是用代码模拟你手动敲地址回车的过程,去看看HttpClient相关的东西。
来段参考代码:
public static String request(String strUrl) {
strUrl = QuhaoConstant.HTTP_URL + strUrl;
URL url = null;
String result = "";
HttpURLConnection urlConn = null;
InputStreamReader in = null;
try {
url = new URL(strUrl);
urlConn = (HttpURLConnection) url.openConnection();
urlConn.setConnectTimeout(1000 * 20);
in = new InputStreamReader(urlConn.getInputStream());
BufferedReader br = new BufferedReader(in); String readerLine = null;
while ((readerLine = br.readLine()) != null) {
result += readerLine;
}
in.close();
urlConn.disconnect();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
urlConn.disconnect();
}
return result;
}
public class Demo {
public void http() {
URL url=null;
URLConnection conn=null;
try{
url=new URL("需要访问的URL");
conn=url.openConnection();
System.out.println(url.getContent());
System.out.println(url.getAuthority());
System.out.println("*********************************************");
System.out.println(conn.getContent());
System.out.println(conn.getContentType());
}catch(Exception e){
e.printStackTrace();
}
try {
Runtime.getRuntime().exec("rundll32 url.dll,FileProtocolHandler "+"需访问的URL" );
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
Demo de=new Demo();
de.http();
}}