使用改造的Json Get Request将无法使用

时间:2018-05-17 15:40:33

标签: java android json retrofit retrofit2

直到这个api我有这些和我的代码现在工作我无法弄清楚我需要得到什么以及放在Map中的内容我也不知道从哪里开始将这个响应放到我的应用程序中。

这个api我知道如何工作,我有它的工作代码: https://gyazo.com/f2eb4858c48c31c5c48765a9e7512179

但这个api真的很难为我找到。 https://gyazo.com/d2bad9dbe66bf7c51b169b54a68a003a

我真的不知道我需要在Map中放置什么以及如何获取“result”数组(如果是数组列表?)

谢谢你们,这是我不切实际的例子。

Datas.class

package Model.BittrexApiModel;

public class Datas {

    private Result result;

    public Result getResult() {
        return result;
    }

    public void setResult(Result result) {
        this.result = result;
    }

    public Datas withDatas(Result result){
        this.result=result;
        return this;
    }
}

这是我的结果POJO CLASS

package Model.BittrexApiModel;

import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;

public class Result {

    @SerializedName("MarketName")
    @Expose
    private String marketName;
    @SerializedName("High")
    @Expose
    private Double high;
    @SerializedName("Low")
    @Expose
    private Double low;
    @SerializedName("Volume")
    @Expose
    private Double volume;
    @SerializedName("Last")
    @Expose
    private Double last;
    @SerializedName("BaseVolume")
    @Expose
    private Double baseVolume;
    @SerializedName("TimeStamp")
    @Expose
    private String timeStamp;
    @SerializedName("Bid")
    @Expose
    private Double bid;
    @SerializedName("Ask")
    @Expose
    private Double ask;
    @SerializedName("OpenBuyOrders")
    @Expose
    private Integer openBuyOrders;
    @SerializedName("OpenSellOrders")
    @Expose
    private Integer openSellOrders;
    @SerializedName("PrevDay")
    @Expose
    private Double prevDay;
    @SerializedName("Created")
    @Expose
    private String created;

    public String getMarketName() {
        return marketName;
    }

    public void setMarketName(String marketName) {
        this.marketName = marketName;
    }

    public Result withMarketName(String marketName) {
        this.marketName = marketName;
        return this;
    }

    public Double getHigh() {
        return high;
    }

    public void setHigh(Double high) {
        this.high = high;
    }

    public Result withHigh(Double high) {
        this.high = high;
        return this;
    }

    public Double getLow() {
        return low;
    }

    public void setLow(Double low) {
        this.low = low;
    }

    public Result withLow(Double low) {
        this.low = low;
        return this;
    }

    public Double getVolume() {
        return volume;
    }

    public void setVolume(Double volume) {
        this.volume = volume;
    }

    public Result withVolume(Double volume) {
        this.volume = volume;
        return this;
    }

    public Double getLast() {
        return last;
    }

    public void setLast(Double last) {
        this.last = last;
    }

    public Result withLast(Double last) {
        this.last = last;
        return this;
    }

    public Double getBaseVolume() {
        return baseVolume;
    }

    public void setBaseVolume(Double baseVolume) {
        this.baseVolume = baseVolume;
    }

    public Result withBaseVolume(Double baseVolume) {
        this.baseVolume = baseVolume;
        return this;
    }

    public String getTimeStamp() {
        return timeStamp;
    }

    public void setTimeStamp(String timeStamp) {
        this.timeStamp = timeStamp;
    }

    public Result withTimeStamp(String timeStamp) {
        this.timeStamp = timeStamp;
        return this;
    }

    public Double getBid() {
        return bid;
    }

    public void setBid(Double bid) {
        this.bid = bid;
    }

    public Result withBid(Double bid) {
        this.bid = bid;
        return this;
    }

    public Double getAsk() {
        return ask;
    }

    public void setAsk(Double ask) {
        this.ask = ask;
    }

    public Result withAsk(Double ask) {
        this.ask = ask;
        return this;
    }

    public Integer getOpenBuyOrders() {
        return openBuyOrders;
    }

    public void setOpenBuyOrders(Integer openBuyOrders) {
        this.openBuyOrders = openBuyOrders;
    }

    public Result withOpenBuyOrders(Integer openBuyOrders) {
        this.openBuyOrders = openBuyOrders;
        return this;
    }

    public Integer getOpenSellOrders() {
        return openSellOrders;
    }

    public void setOpenSellOrders(Integer openSellOrders) {
        this.openSellOrders = openSellOrders;
    }

    public Result withOpenSellOrders(Integer openSellOrders) {
        this.openSellOrders = openSellOrders;
        return this;
    }

    public Double getPrevDay() {
        return prevDay;
    }

    public void setPrevDay(Double prevDay) {
        this.prevDay = prevDay;
    }

    public Result withPrevDay(Double prevDay) {
        this.prevDay = prevDay;
        return this;
    }

    public String getCreated() {
        return created;
    }

    public void setCreated(String created) {
        this.created = created;
    }

    public Result withCreated(String created) {
        this.created = created;
        return this;
    }

}

这是我的BittrexResponse.class(我认为这个没有用。)

package Model.BittrexApiModel;

import java.util.Map;

import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;

public class BittrexResponse {

    @SerializedName("success")
    @Expose
    private Boolean success;
    @SerializedName("message")
    @Expose
    private String message;
    @SerializedName("result")
    @Expose
    private Map<String, Result> datas;


    public Map<String,Result> getDatas(){
        return datas;
    }

    public void setDatas(Map<String,Result> datas){
        this.datas=datas;
    }

    //private List<Result> result = new ArrayList<>();

    //This is first original JSONSCHEMA2POJO - SAVING RESPONSE DON't WORK
   // private List<Result> result = new ArrayList<Result>();

    public Boolean getSuccess() {
        return success;
    }

    public void setSuccess(Boolean success) {
        this.success = success;
    }

    public BittrexResponse withSuccess(Boolean success) {
        this.success = success;
        return this;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public BittrexResponse withMessage(String message) {
        this.message = message;
        return this;
    }

ApiClient.class(工作,在之前的例子中测试过)

package Model.CoinMarketCapApiModel;

import com.test.retrofit.CryptoCyber.Settings;

import okhttp3.OkHttpClient;
import okhttp3.logging.HttpLoggingInterceptor;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class ApiClient {

    public static final String BASE_URL = Settings.getBase_url();
    private static Retrofit retrofit = null;


    public static Retrofit getClient() {
        HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
        interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
        OkHttpClient client = new OkHttpClient.Builder().addInterceptor(interceptor).build();



            retrofit = new Retrofit.Builder()
                    .client(client)
                    .baseUrl(BASE_URL)
                    .addConverterFactory(GsonConverterFactory.create())
                    .build();

        return retrofit;
    }
}

我希望有人能帮助我找出为什么我不知道获取数据并将其保存到地图......

P.S。这是api的链接 CLICK HERE

1 个答案:

答案 0 :(得分:0)

这是设计类以正确解析响应的可能方法。

ApiResponse

public class ApiResponse {
    private boolean success;

    private String message;

    @SerializedName("result")
    private List<Market> markets;

    // Other fields + getters&setters
    ...
}

市场

public class Market {

    @SerializedName("MarketName")
    private String marketName;

    @SerializedName("High")
    private double high;

    @SerializedName("Low")
    private double low;

    @SerializedName("Volume")
    private double volume;

    // Other fields + getters&setters
    ...
}

主要

public class Main {

    public static void main(String[] args) throws IOException {

        Retrofit retrofit = createRetrofit();

        Api api = retrofit.create(Api.class);
        retrofit2.Response<ApiResponse> response = api.getMarketSummaries().execute();
        if (!response.isSuccessful()) {
            // Handle error case
        } else {
            ApiResponse marketApiResponse = response.body();
            System.out.println(marketApiResponse);
        }
    }

    private static Retrofit createRetrofit() {
        return new Retrofit.Builder()
                .baseUrl("https://bittrex.com/api/v1.1/public/")
                .addConverterFactory(GsonConverterFactory.create())
                .build();
    }
}

它会打印这样的东西(为了简洁起见,只报告列表的第一项):

ApiResponse{success=true, message='', markets=[Market{marketName='BTC-2GIVE', high=1.1E-6, low=9.7E-7, volume=3499023.70109898},