简体   繁体   English

使用外部API(REST)上的Get方法读取列表(java)

[英]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.

相关问题 Java Rest-使用GET方法发送参数化列表 - Java Rest - send parametrized List using GET method CRUD在休眠状态下从ManyToMany中使用Jersey使用Java的REST API在REST API中使用POST方法读取JSON的问题 - CRUD issue reading a JSON using POST method in a REST API in Java using Jersey from a ManyToMany noted in hibernate 在 rest API 中使用 POST 方法而不是 GET - Using POST method instead of GET in a rest API 错误 86 此方法需要使用 AppEngine 中的 Java 在 Twitter Rest API 1.1 上使用 Bearer 令牌的 GET 或 HEAD - Error 86 This method requires a GET or HEAD using Bearer token on Twitter Rest API 1.1 using Java in AppEngine REST API 方法获取 - Rest API method get 使用Java代码中的JIRA REST API的GET方法时出现HTTP 401异常 - HTTP 401 exception while using GET method of JIRA REST API from Java Code 如何使用Java中的stash rest API获取stash存储库中特定文件夹内的文件名列表 - How to get list of file names inside a specific folder in stash repository using stash rest API in Java REST API - GET方法,输入参数为主体中的JAVA对象 - REST API - GET Method with input parameter as JAVA Object in body Java Rest API客户端-GET方法-错误415 - Java Rest API client - GET method - Error 415 Java Rest API JAX-RS中的多个参数-GET方法 - Multiple params in Java Rest API JAX-RS - GET Method
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM