Skip to content
Advertisement

Getting error while using live currency conversion API by Retrofit in Android

// This is my MainActivity.java

ublic class MainActivity extends AppCompatActivity implements View.OnClickListener{

private static final String TAG = "MainActivity";
private TextView result;
private EditText currency;
private Button button;
private static String BASE_URL = "https://api.currencyscoop.com/v1/";

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Log.d(TAG, "onCreate: called");
    initViews();
    button.setOnClickListener(this);
}

public void initViews(){
    Log.d(TAG, "initViews: called");
    result = findViewById(R.id.result);
    currency = findViewById(R.id.amount);
    button = findViewById(R.id.button);
}

@Override
public void onClick(View v) {
    Log.d(TAG, "onClick: called");
    Retrofit retrofit = new Retrofit.Builder()
            .baseUrl(BASE_URL)
            .addConverterFactory(GsonConverterFactory.create())
            .build();
    RetrofitClient retrofitClient = retrofit.create(RetrofitClient.class);
    Call<ConvertAmt> calling = retrofitClient.getConvertedValue("my_api_key","USD", "INR", currency.getText().toString());
    calling.enqueue(new Callback<ConvertAmt>() {
        @Override
        public void onResponse(Call<ConvertAmt> call, Response<ConvertAmt> response) {
            Log.d(TAG, "onResponse: called :----------------------> "+response.body().getResult());
        }

        @Override
        public void onFailure(Call<ConvertAmt> call, Throwable t) {
            Log.d(TAG, "onFailure: --------------> "+t.getStackTrace());
        }
    });
}

}

// This is my RetrofitClient interface

public interface RetrofitClient {

@GET("/convert?")
Call<ConvertAmt> getConvertedValue(@Query("api_key") String api_key, @Query("base") String base, @Query("to") String to, @Query("amount") String amount);

}

// This is my ConvertAmt class (Model class where i am storing the result)

public class ConvertAmt { private double result;

public ConvertAmt(double result) {
    this.result = result;
}

public double getResult() {
    return result;
}

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

@Override
public String toString() {
    return "ConvertAmt{" +
            "result=" + result +
            '}';
}

}

// This is my StackTrace

2020-08-27 23:59:53.212 1471-1471/com.example.online_currency W/System.err: java.io.EOFException: End of input at line 1 column 1 path $

2020-08-27 23:59:53.212 1471-1471/com.example.online_currency W/System.err: at com.google.gson.stream.JsonReader.nextNonWhitespace(JsonReader.java:1401) 2020-08-27 23:59:53.212 1471-1471/com.example.online_currency W/System.err: at com.google.gson.stream.JsonReader.doPeek(JsonReader.java:549) 2020-08-27 23:59:53.212 1471-1471/com.example.online_currency W/System.err: at com.google.gson.stream.JsonReader.peek(JsonReader.java:425) 2020-08-27 23:59:53.212 1471-1471/com.example.online_currency W/System.err: at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:205) 2020-08-27 23:59:53.212 1471-1471/com.example.online_currency W/System.err: at retrofit2.converter.gson.GsonResponseBodyConverter.convert(GsonResponseBodyConverter.java:39) 2020-08-27 23:59:53.212 1471-1471/com.example.online_currency W/System.err: at retrofit2.converter.gson.GsonResponseBodyConverter.convert(GsonResponseBodyConverter.java:27) 2020-08-27 23:59:53.213 1471-1471/com.example.online_currency W/System.err: at retrofit2.OkHttpCall.parseResponse(OkHttpCall.java:223) 2020-08-27 23:59:53.213 1471-1471/com.example.online_currency W/System.err: at retrofit2.OkHttpCall$1.onResponse(OkHttpCall.java:121) 2020-08-27 23:59:53.213 1471-1471/com.example.online_currency W/System.err: at okhttp3.RealCall$AsyncCall.execute(RealCall.java:206) 2020-08-27 23:59:53.213 1471-1471/com.example.online_currency W/System.err: at okhttp3.internal.NamedRunnable.run(NamedRunnable.java:32) 2020-08-27 23:59:53.213 1471-1471/com.example.online_currency W/System.err: at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1133) 2020-08-27 23:59:53.213 1471-1471/com.example.online_currency W/System.err: at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:607) 2020-08-27 23:59:53.213 1471-1471/com.example.online_currency W/System.err: at java.lang.Thread.run(Thread.java:761)

Advertisement

Answer

It seems the data you are expecting from the response is not matching to the pojo that you are mapping this is what you are expecting,

public class ConvertAmt { 
 private double result;
}

actual response is

{
    "meta": {
        "code": 200,
        "disclaimer": "Usage subject to terms: https://currencyscoop.com/terms"
    },
    "response": {
        "timestamp": 1598589131,
        "date": "2020-08-27",
        "from": "USD",
        "to": "INR",
        "amount": "10",
        "value": 738.1371942
    }
}

So you can convert your pojo to

public class ConvertAmt { 
 private Map<String,Object> meta;
 private Map<String,Object> response;

}

and you can get the response like this

Call<ConvertAmt> calling = retrofitClient.getConvertedValue("my_api_key","USD", "INR", currency.getText().toString());


calling.getResponse().get("amount");
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement