[英]reading a list using Get method on external API (REST)(java)
Im trying to learn how to consume a REST webservice using Jersey. 我试图学习如何使用Jersey使用REST Web服务。 Im using a get request to view data from the URI " https://api.fixer.io/latest " this URI displays a base, date and i believe a array/ArrayList.
我使用获取请求从URI“ https://api.fixer.io/latest ”查看数据,该URI显示了基准,日期,我相信是array / ArrayList。 In my class below, i have managed to display the date and base of the resource.
在下面的课程中,我设法显示了资源的日期和依据。 But I am having trouble displaying the list of items.
但是我无法显示项目列表。 When i run my code it gives this:
当我运行我的代码时,它给出了这一点:
Date = 2017-12-15, base = EURlist = [] 日期= 2017年12月15日,基准= EURlist = []
an example of what im looking for would be: 我正在寻找的一个例子是:
Date = 2017-12-15, base = EUR list = [AUD":1.5382,"BGN":1.9558 (etc)] 日期= 2017年12月15日,基准=欧元列表= [AUD“:1.5382,” BGN“:1.9558(等)]
Here is my code: restServiceClient.java 这是我的代码:restServiceClient.java
import java.util.ArrayList;
import java.util.List;
import javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.client.Entity;
import javax.ws.rs.client.WebTarget;
import javax.ws.rs.core.MediaType;
import javax.xml.ws.Response;
public class restServiceClient{
public static void main(String[] args) {
Client client = ClientBuilder.newClient();
Exchange exchange = client.target("https://api.fixer.io/latest")
.request(MediaType.APPLICATION_XML)
.get(Exchange.class);
String base = exchange.getBase();
String date = exchange.getDate();
ArrayList<String> theList = exchange.getRates();
//String[] excArray = theList.toArray();
System.out.print(exchange);
client.close() ;
}
}
Exchange.java Exchange.java
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author Admin
*/
public class Exchange {
private String base;
private String date;
private ArrayList<String> rates;
public ArrayList<String> getRates() {
return rates;
}
public void setRates(ArrayList<String> rates) {
this.rates = rates;
}
public void setBase(String base) {
this.base = base;
}
public void setDate(String date) {
this.date = date;
}
public String getBase() {
return base;
}
public String getDate() {
return date;
}
@Override
public String toString() {
return "Date = " + date + ", base = "
+ base + " list = " + rates;
}
}
First, program on interfaces ( List
, not ArrayList
). 首先,在接口(
List
,而不是ArrayList
)上编程。
Second, look at the response at https://api.fixer.io/latest . 其次,查看https://api.fixer.io/latest上的响应。 It's JSON, not XML.
它是JSON,而不是XML。 So don't request XML.
因此,请勿请求XML。
Third, as you see, rates is not a list of Strings. 第三,如您所见,rate不是字符串列表。 It's an array of objects with a key of type String, and a value of type number.
它是一个对象数组,键的类型为String,值的类型为number。 This would typically mapped by a JSON mapper as a
Map<String, Double>
, not as an ArrayList<String>
. JSON映射器通常会将其映射为
Map<String, Double>
,而不是ArrayList<String>
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.