I'm building a currency conversion application by using a public API. In my code, I need to read the conversion_rates part of this JSON Object:
{"result":"success","documentation":"https://www.exchangerate-api.com/docs","terms_of_use":"https://www.exchangerate-api.com/terms","time_last_update_unix":1604275451,"time_last_update_utc":"Mon, 02 Nov 2020 00:04:11 +0000","time_next_update_unix":1604361971,"time_next_update_utc":"Tue, 03 Nov 2020 00:06:11 +0000","base_code":"USD","conversion_rates":{"USD":1,"AED":3.6720,"ARS":78.2978,"AUD":1.4207,"BGN":1.6731,"BRL":5.7739,"BSD":1.0000,"CAD":1.3323,"CHF":0.9158,"CLP":771.5889,"CNY":6.6867,"COP":3852.2500,"CZK":23.3398,"DKK":6.3721,"DOP":58.1472,"EGP":15.6627,"EUR":0.8571,"FJD":2.1386,"GBP":0.7722,"GTQ":7.7863,"HKD":7.7528,"HRK":6.4788,"HUF":314.5384,"IDR":14686.5055,"ILS":3.4125,"INR":74.4728,"ISK":140.9082,"JPY":104.5730,"KRW":1133.2948,"KZT":432.8371,"MVR":15.2900,"MXN":21.2752,"MYR":4.1540,"NOK":9.5245,"NZD":1.5077,"PAB":1.0000,"PEN":3.6123,"PHP":48.4156,"PKR":159.8444,"PLN":3.9539,"PYG":7004.0909,"RON":4.1695,"RUB":79.1120,"SAR":3.7504,"SEK":8.8849,"SGD":1.3645,"THB":31.1583,"TRY":8.3641,"TWD":28.5836,"UAH":28.4108,"UYU":42.9699,"ZAR":16.2831}}
When I try to debug my mainActivity.java at the line where log the API response, I see nothing in my debugging console! I believe it must be a problem with reading from the JSON object. This is my first time using APIs with android studio, so I'm not sure if I did it correctly. Here are my codes
MainActivity.java:
package com.example.currencyconversion;
import androidx.appcompat.app.AppCompatActivity;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import com.example.currencyconversion.Retrofit.RetrofitBuilder;
import com.example.currencyconversion.Retrofit.RetrofitInterface;
import com.google.gson.JsonObject;
public class MainActivity extends AppCompatActivity {
Button button;
EditText currencyToBeConverted;
EditText currencyConverted;
Spinner convertToDropdown;
Spinner convertFromDropdown;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Initialization
currencyConverted =(EditText) findViewById(R.id.currency_converted);
currencyToBeConverted =(EditText) findViewById(R.id.currency_to_be_converted);
convertToDropdown = (Spinner) findViewById(R.id.convert_to);
convertFromDropdown = (Spinner) findViewById(R.id.convert_from);
button = (Button) findViewById(R.id.button);
// Adding Functionality
String[] downDownList = {"USD", "AED", "EUR", "GBP"};
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.support_simple_spinner_dropdown_item, downDownList);
convertToDropdown.setAdapter(adapter);
convertFromDropdown.setAdapter(adapter);
button.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View view) {
//API Call
RetrofitInterface retrofitInterface = RetrofitBuilder.getRetrofitInstance().create(RetrofitInterface.class);
//Call<JsonObject> call = retrofitInterface.getExchangeCurrency(convertFromDropdown.getSelectedItem().toString());
Call<JsonObject> call = retrofitInterface.getExchangeCurrency("AED");
call.enqueue(new Callback<JsonObject>() {
@Override
public void onResponse(Call<JsonObject> call, Response<JsonObject> response) {
Log.d("response", String.valueOf(response.body())); //HERE IS WHERE I DEBUG
/*JsonObject res = response.body();
JsonObject rates = res.getAsJsonObject("conversion_rates");
double currency = Double.valueOf(currencyToBeConverted.getText().toString());
double multiplier = Double.valueOf(rates.get(convertToDropdown.getSelectedItem().toString()).toString());
double result = currency * multiplier;
currencyConverted.setText(String.valueOf(result));*/
}
@Override
public void onFailure(Call<JsonObject> call, Throwable t) {
}
});
}
});
}
RetrofitBuilder.java:
package com.example.currencyconversion.Retrofit;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;
public class RetrofitBuilder {
public static Retrofit retrofit;
public static Retrofit getRetrofitInstance() {
if (retrofit == null) {
retrofit = new Retrofit.Builder()
.baseUrl("https://v6.exchangerate-api.com/")
.addConverterFactory(GsonConverterFactory.create())
.build();
}
return retrofit;
}
}
RetrofitInterface.java:
package com.example.currencyconversion.Retrofit;
import com.google.gson.JsonObject;
import retrofit2.Call;
import retrofit2.http.GET;
import retrofit2.http.Path;
public interface RetrofitInterface {
@GET("v6/4API-KEY/latest/{currency}")
Call<JsonObject> getExchangeCurrency(@Path("currency") String currency);
}

Here is why you're not getting an error..
You're getting a result from your API using an INVALID KEY which is "AED" as stated on your code.
Here's the link of your request: https://v6.exchangerate-api.com/v6/4API-KEY/latest/AED
And this is the result that the API is outputting:
I recommend to check your APIs documentation.