GPS公司给了我们一个接口(ip+端口+车牌号),查询车辆的位置。我们公司需要自己写服务端,怎样在服务端访问那个给定的接口,并返回查询的数据,并怎样解析数据?我刚毕业,这个项目有压力啊,还求各位大神帮助。访问接口

解决方案 »

  1.   

    你知道怎么用浏览器访问吧?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;
    }
      

  2.   

    给你的接口应该是基于http的, 就是rest风格的webservice,直接访问,比如用httpClient
      

  3.   


    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();
    }}