如何在androidstudio中使用retofit检索数据

biswetbf  于 2021-07-14  发布在  Java
关注(0)|答案(1)|浏览(524)
  1. https://pro-api.coinmarketcap.com/v1/tools/price-conversion?symbol=NGN&amount=1000897.01&convert=BTC&CMC_PRO_API_KEY=*********************************

这是url,下面是查询上述url返回的json:

  1. {"status":{"timestamp":"2021-04-20T08:48:16.990Z","error_code":0,"error_message":null,"elapsed":17,"credit_count":1,"notice":null},"data":{"id":2819,"symbol":"NGN","name":"Nigerian Naira","amount":1000897.01,"last_updated":"2021-04-20T08:48:07.000Z","quote":{"BTC":{"price":0.0444454708294559,"last_updated":"2021-04-20T08:48:02.000Z"}}}}

下面是我在androidstudio中从上面的api获取数据的代码

  1. my pojo classes
  2. import com.google.gson.annotations.SerializedName;
  3. public class CurrencyModel {
  4. @SerializedName("name")
  5. private String currencyName;
  6. @SerializedName("Amount")
  7. private int currencyAmount;
  8. @SerializedName("quote")
  9. Quota quota;
  10. public CurrencyModel(Quota quota) {
  11. this.quota = quota;
  12. // quota.bitcoinModel.getCoinPrice();
  13. }
  14. public CurrencyModel(String currencyName, int currencyAmount, Quota quota) {
  15. this.currencyName = currencyName;
  16. this.currencyAmount = currencyAmount;
  17. this.quota = quota;
  18. }
  19. public String getCurrencyName() {
  20. return currencyName;
  21. }
  22. public void setCurrencyName(String currencyName) {
  23. this.currencyName = currencyName;
  24. }
  25. public int getCurrencyAmount() {
  26. return currencyAmount;
  27. }
  28. public void setCurrencyAmount(int currencyAmount) {
  29. this.currencyAmount = currencyAmount;
  30. }
  31. public Quota getQuota() {
  32. return quota;
  33. }
  34. public void setQuota(Quota quota) {
  35. this .quota = quota;
  36. }
  37. public class Quota {
  38. int price;
  39. @SerializedName("BTC")
  40. BitcoinModel bitcoinModel;
  41. public Quota(BitcoinModel bitcoinModel) {
  42. this.bitcoinModel = bitcoinModel;
  43. }
  44. public BitcoinModel getBitcoinModel() {
  45. return bitcoinModel = new BitcoinModel(price);
  46. }
  47. public void setBitcoinModel(BitcoinModel bitcoinModel) {
  48. this.bitcoinModel = bitcoinModel;
  49. }
  50. }
  51. public class BitcoinModel {
  52. @SerializedName("price")
  53. private int coinPrice;
  54. public BitcoinModel(int coinPrice) {
  55. this.coinPrice = coinPrice;
  56. }
  57. public int getCoinPrice() {
  58. return coinPrice;
  59. }
  60. public void setCoinPrice(int coinPrice) {
  61. this.coinPrice = coinPrice;
  62. }
  63. }
  64. myactivity
  65. public class BitcoinConversionActivity extends AppCompatActivity {
  66. TextInputEditText amountConverted;
  67. Button conversionButton, buyCoinButton;
  68. TextView amountInCurrency;
  69. int amount;
  70. String amountEntered;
  71. Quota quota;
  72. CurrencyModel currencyModel;
  73. JsonPojo pojo;
  74. String symbol = "NGN";
  75. String convert = "BTC";
  76. int amountneeded = 1024587;
  77. String apiKey = "b34416c7-2bb9-44b1-aa76-1293edd14dda";
  78. String currencyName;
  79. int amountnm;
  80. @Override
  81. protected void onCreate(@Nullable Bundle savedInstanceState) {
  82. super.onCreate(savedInstanceState);
  83. setContentView(R.layout.activity_coinconversion);
  84. amountConverted = findViewById(R.id.amount_edit_text);
  85. amountInCurrency = findViewById(R.id.conversion_rate_tv);
  86. conversionButton = findViewById(R.id.conversion_button);
  87. buyCoinButton = findViewById(R.id.buy_exchange_coin_button);
  88. conversionButton.setOnClickListener(new View.OnClickListener() {
  89. @Override
  90. public void onClick(View v) {
  91. amount = Integer.parseInt(amountConverted.getText().toString());
  92. if (TextUtils.isEmpty(Double.toString(amount))){
  93. Toast.makeText(BitcoinConversionActivity.this, "Please Enter a valid Amount", Toast.LENGTH_LONG).show();
  94. }else{
  95. CoinExchange();
  96. }
  97. }
  98. });
  99. }
  100. private void CoinExchange() {
  101. Retrofit retrofit = new Retrofit.Builder()
  102. .baseUrl("https://pro-api.coinmarketcap.com/")
  103. .addConverterFactory(GsonConverterFactory.create())
  104. .build();
  105. BitcoinInterface service = retrofit.create(BitcoinInterface.class);
  106. Call<CurrencyModel> call = service.loadNairaToBitcionExchange(symbol,amountneeded,convert,apiKey);
  107. call.enqueue(new Callback<CurrencyModel>() {
  108. @Override
  109. public void onResponse(Call<CurrencyModel> call, Response<CurrencyModel> response) {
  110. if (response.isSuccessful()){
  111. String price = response.body().toString();
  112. //amountInCurrency.setText(price);
  113. amountInCurrency.setText("https://pro-api.coinmarketcap.com/"+symbol+amount+convert+apiKey);
  114. Toast.makeText(BitcoinConversionActivity.this, "Data: " + response.body().getQuota().getBitcoinModel().getCoinPrice(), Toast.LENGTH_LONG).show();
  115. }
  116. @Override
  117. public void onFailure(Call<CurrencyModel> call, Throwable t) {
  118. Toast.makeText(BitcoinConversionActivity.this, "Error: " + t.getMessage(), Toast.LENGTH_SHORT).show();
  119. enter code here
  120. }
  121. });
  122. }
  123. }

api接口:

  1. public interface BitcoinInterface {
  2. @GET("v1/tools/price-conversion?")
  3. Call<CurrencyModel> loadNairaToBitcionExchange(@Query("symbol") String coinsSymbol,
  4. @Query("amount") int currencyaAmount,
  5. @Query("convert") String convertedToCurrency,
  6. @Query("CMC_PRO_API_KEY") String ApiKey);
  7. }

调用返回null,时间为零。

dgjrabp2

dgjrabp21#

API 你没有得到完全正确的答案 CurrencyModel 作为回应。你得到的另一个物体是 CurrencyModel 是子对象。必须创建如下所示的resposne对象:

  1. public class ResponseObject{
  2. //@SerializedName("status")
  3. //private int status;
  4. @SerializedName("data")
  5. private CurrencyModel currencyModel;
  6. //getters and setters goes here
  7. }

然后你的 interface 应该像你期望的那样 ResponseObject 在这里

  1. public interface BitcoinInterface {
  2. @GET("v1/tools/price-conversion?")
  3. Call<ResponseObject> loadNairaToBitcionExchange(@Query("symbol") String coinsSymbol,
  4. @Query("amount") int currencyaAmount,
  5. @Query("convert") String convertedToCurrency,
  6. @Query("CMC_PRO_API_KEY") String ApiKey);
  7. }
展开查看全部

相关问题