gpt4 book ai didi

java - 在 Android 上使用 Retrofit 正确解析 JSON

转载 作者:行者123 更新时间:2023-12-02 12:21:48 24 4
gpt4 key购买 nike

我想解析这个url生成的Json:

https://blockchain.info/es/ticker

此 URL 生成 JSON 响应,如下所示:

{
"USD" : {"15m" : 4097.85, "last" : 4097.85, "buy" : 4097.84, "sell" : 4093.37, "symbol" : "$"},
"AUD" : {"15m" : 5209.87, "last" : 5209.87, "buy" : 5209.86, "sell" : 5204.18, "symbol" : "$"},
"BRL" : {"15m" : 12985.88, "last" : 12985.88, "buy" : 12985.85, "sell" : 12971.68, "symbol" : "R$"},
"CAD" : {"15m" : 5214.82, "last" : 5214.82, "buy" : 5214.81, "sell" : 5209.12, "symbol" : "$"},
"CHF" : {"15m" : 3985.18, "last" : 3985.18, "buy" : 3985.17, "sell" : 3980.82, "symbol" : "CHF"},
"CLP" : {"15m" : 2660734.01, "last" : 2660734.01, "buy" : 2660727.51, "sell" : 2657825.14, "symbol" : "$"},
"CNY" : {"15m" : 27427.32, "last" : 27427.32, "buy" : 27427.25, "sell" : 27397.33, "symbol" : "¥"},
"DKK" : {"15m" : 25993.26, "last" : 25993.26, "buy" : 25993.2, "sell" : 25964.85, "symbol" : "kr"},
"EUR" : {"15m" : 3495.38, "last" : 3495.38, "buy" : 3495.37, "sell" : 3491.56, "symbol" : "€"},
"GBP" : {"15m" : 3180.65, "last" : 3180.65, "buy" : 3180.64, "sell" : 3177.17, "symbol" : "£"},
"HKD" : {"15m" : 32058.22, "last" : 32058.22, "buy" : 32058.14, "sell" : 32023.17, "symbol" : "$"},
"INR" : {"15m" : 262979.52, "last" : 262979.52, "buy" : 262978.88, "sell" : 262692.02, "symbol" : "₹"},
"ISK" : {"15m" : 447157.39, "last" : 447157.39, "buy" : 447156.3, "sell" : 446668.53, "symbol" : "kr"},
"JPY" : {"15m" : 454250.37, "last" : 454250.37, "buy" : 454249.26, "sell" : 453753.75, "symbol" : "¥"},
"KRW" : {"15m" : 4677285.99, "last" : 4677285.99, "buy" : 4677274.58, "sell" : 4672172.52, "symbol" : "₩"},
"NZD" : {"15m" : 5656.28, "last" : 5656.28, "buy" : 5656.26, "sell" : 5650.09, "symbol" : "$"},
"PLN" : {"15m" : 14947.54, "last" : 14947.54, "buy" : 14947.51, "sell" : 14931.2, "symbol" : "zł"},
"RUB" : {"15m" : 244110.7, "last" : 244110.7, "buy" : 244110.1, "sell" : 243843.82, "symbol" : "RUB"},
"SEK" : {"15m" : 33149.72, "last" : 33149.72, "buy" : 33149.64, "sell" : 33113.48, "symbol" : "kr"},
"SGD" : {"15m" : 5603.48, "last" : 5603.48, "buy" : 5603.46, "sell" : 5597.35, "symbol" : "$"},
"THB" : {"15m" : 136432.79, "last" : 136432.79, "buy" : 136432.46, "sell" : 136283.64, "symbol" : "฿"},
"TWD" : {"15m" : 124291.89, "last" : 124291.89, "buy" : 124291.59, "sell" : 124156.01, "symbol" : "NT$"}
}

我想使用 Retrofit 将每种货币作为对象,以便稍后能够在 Activity 上打印所有信息。我注意到的一件事是返回的类型,我不太确定如何定义。不管怎样,我正在使用 Retrofit,所以只是为了尝试一个例子,我正在做这样的事情:

public interface CryptoCurrencyService {

@GET("ticker")
Call<JsonObject> listCurrencies();
}

然后我有:

public class ApiClient
{

//https://blockchain.info/es/ticker


private static Retrofit retrofit = null;

static Retrofit getClient() {

retrofit = new Retrofit.Builder()
.baseUrl("https://blockchain.info/es/")
.addConverterFactory(GsonConverterFactory.create())
.build();

return retrofit;
}


}

货币类:

public class Currency
{
@SerializedName("15m")
private double valueLast15m;
@SerializedName("last")
private double lastValue;
@SerializedName("buy")
private double buyValue;
@SerializedName("sell")
private double sellValue;
@SerializedName("symbol")
private String symbol;

public Currency(double valueLast15m, double lastValue, double buyValue, double sellValue, String symbol) {
this.valueLast15m = valueLast15m;
this.lastValue = lastValue;
this.buyValue = buyValue;
this.sellValue = sellValue;
this.symbol = symbol;
}

public Currency()
{
this.valueLast15m = 0.0;
this.lastValue = 0.0;
this.buyValue = 0.0;
this.sellValue = 0.0;
this.symbol = "";
}

public double getValueLast15m() {
return valueLast15m;
}

public void setValueLast15m(double valueLast15m) {
this.valueLast15m = valueLast15m;
}

public double getLastValue() {
return lastValue;
}

public void setLastValue(double lastValue) {
this.lastValue = lastValue;
}

public double getBuyValue() {
return buyValue;
}

public void setBuyValue(double buyValue) {
this.buyValue = buyValue;
}

public double getSellValue() {
return sellValue;
}

public void setSellValue(double sellValue) {
this.sellValue = sellValue;
}

public String getSymbol() {
return symbol;
}

public void setSymbol(String symbol) {
this.symbol = symbol;
}
}

因此,在我的 Activity 中的某个时刻,我正在这样做:

 CryptoCurrencyService service = ApiClient.getClient().create(CryptoCurrencyService.class);
service.listCurrencies().enqueue(new Callback<JsonObject>() {
@Override
public void onResponse(Call<JsonObject> call, Response<JsonObject> response) {
Log.d("RESPONSE",response.body().getAsString());


}

@Override
public void onFailure(Call<JsonObject> call, Throwable t) {

Toast.makeText(getApplicationContext(),"Failure",Toast.LENGTH_LONG).show();
}
});

但是我没有将 JSON 转换为字符串,只是得到网络的响应(好的:200)。

我已经尝试过:

response.body().getAsString()、response.body().toString()、response.toString().... 但我得到的只是 http 响应,而不是 JSON,顺便说一下,我得到的只是 http 响应,而不是 JSON。我知道我做对了,因为如果我将鼠标放在“响应”对象上,我可以通过右键单击“响应”->“正文”->“成员”来搜索返回的 JSON 对象。我也尝试过response.body().get("members");但它崩溃了。

知道我做错了什么吗?我可以直接解析列表而不获取原始 json 响应吗?

非常感谢!

最佳答案

在改造中,您的 Call 的参数化类应该是与 json 响应的映射相对应的类。

更多这样的事情:

public interface CryptoCurrencyService {

@GET("ticker")
Call<Map<String, Currency>> listCurrencies();
}

关于java - 在 Android 上使用 Retrofit 正确解析 JSON,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45712013/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com