1

我正在尝试使用 Java 在https://www.routexl.nl上使用 Restful API来优化某些位置的旅行路线。目前我只是在网站上使用示例内容,但我对 Rest HTTP 内容缺乏了解真的让我望而却步。我曾选择使用 JavaLite,但看到了 UniRest。很高兴使用这些或其他 Rest/HTTP 客户端。

理想情况下,我正在寻找一些代码,它将发送一组具有纬度/经度和描述信息的对象,并使用最短的驾驶路线返回这些对象的排序列表,但很高兴让下面的代码工作,它使用示例从网站。

这就是我到目前为止的原因:-

    //import com.mashape.unirest.http.HttpResponse;
//import com.mashape.unirest.http.JsonNode;
//import com.mashape.unirest.http.Unirest;
//import com.mashape.unirest.http.exceptions.UnirestException;
import org.javalite.http.Get;
import org.javalite.http.Http;
import org.javalite.http.Post;

public class RestClient {

  public static void main(String[] args)  {    
    Post post = Http.post("https://api.routexl.nl/tour", "locations=[{\"address\":\"The Hague, The Netherlands\",\"lat\":\"52.05429\",\"lng\":\"4.248618\"},{\"address\":\"The Hague, The Netherlands\",\"lat\":\"52.076892\",\"lng\":\"4.26975\"},{\"address\":\"Uden, The Netherlands\",\"lat\":\"51.669946\",\"lng\":\"5.61852\"},{\"address\":\"Sint-Oedenrode, The Netherlands\",\"lat\":\"51.589548\",\"lng\":\"5.432482\"}]")
             .header("Accept", "application/json")
             .header("Content-Type", "application/json")            
             .basic("USERNAME", "PASSWORD");
     System.out.println(post.text());
     System.out.println(post.responseMessage());                         
  }
}

这是我得到的输出:-

No input found: 
Conflict

编辑:- 通过删除两个标题使其工作。有人知道为什么会这样吗? EDIT2:- 这很有意义 RouteXL。好的,这是另一个似乎可行的方法。

Post post2 = Http.post("https://api.routexl.nl/tour") .param("locations","[{\"address\":\"The Hague, The Netherlands\",\"lat\":\"52.05429\",\"lng\":\"4.248618\"},{\"address\":\"The Hague, The Netherlands\",\"lat\":\"52.076892\",\"lng\":\"4.26975\"},{\"address\":\"Uden, The Netherlands\",\"lat\":\"51.669946\",\"lng\":\"5.61852\"},{\"address\":\"Sint-Oedenrode, The Netherlands\",\"lat\":\"51.589548\",\"lng\":\"5.432482\"}]") .basic("USERNAME", "PASSWORD"); System.out.println(post2.text()); System.out.println(post2.responseMessage());

4

0 回答 0