0

Good day, I'm using Retrofit for the first time and I can't seem to get it to work. working on an app that get crypto currency rates in local currencies, here is the api link, JSON format

{
  "BTC": {
    "USD": 6019.1,
    "EUR": 5139.44,
    "NGN": 2136243.25
  },
  "ETH": {
    "USD": 290.98,
    "EUR": 250.03,
    "NGN": 103031.01
  }
}

Here are my models (from http://www.jsonschema2pojo.org/): UPDATED

public class CryptoCurrency {

@SerializedName("BTC")
@Expose
private BTC BTC;
@SerializedName("ETH")
@Expose
private ETH ETH;

public BTC getBTC() {
    return BTC;
}

public void setBTC(BTC BTC) {
    this.BTC = BTC;
}

public ETH getETH() {
    return ETH;
}

public void setETH(ETH ETH) {
    this.ETH = ETH;
}

public class BTC {

    @SerializedName("USD")
    @Expose
    private Double USD;

    @SerializedName("EUR")
    @Expose
    private Double EUR;

    @SerializedName("NGN")
    @Expose
    private Double NGN;

    public Double getUSD() {
        return USD;
    }

    public void setUSD(Double USD) {
        this.USD = USD;
    }

    public Double getEUR() {
        return EUR;
    }

    public void setEUR(Double EUR) {
        this.EUR = EUR;
    }

    public Double getNGN() {
        return NGN;
    }

    public void setNGN(Double NGN) {
        this.NGN = NGN;
    }


}


public class ETH {

    @SerializedName("USD")
    @Expose
    private Double USD;
    @SerializedName("EUR")
    @Expose
    private Double EUR;
    @SerializedName("NGN")
    @Expose
    private Double NGN;

    public Double getUSD() {
        return USD;
    }

    public void setUSD(Double USD) {
        this.USD = USD;
    }

    public Double getEUR() {
        return EUR;
    }

    public void setEUR(Double EUR) {
        this.EUR = EUR;
    }

    public Double getNGN() {
        return NGN;
    }

    public void setNGN(Double NGN) {
        this.NGN = NGN;
    }

}

}

My interface:

public interface CurrencyInterface {
    @GET("/data/pricemulti/")
    Call<CryptoCurrency.BTC> currency(@Query("crypto") String crypto, @Query("local") String local);
}

my onResponse method:

call.enqueue(new Callback<CryptoCurrency.BTC>() {
        @Override
        public void onResponse(Call<CryptoCurrency.BTC> call, Response<CryptoCurrency.BTC> response) {
            String currency = response.body().toString();
            Toast.makeText(getApplicationContext(), "result " + currency, Toast.LENGTH_LONG).show();
            Log.i("MainActivity", currency);

        }

        @Override
        public void onFailure(Call<CryptoCurrency.BTC> call, Throwable t) {

        }
    });

When I run the code and check my log I get this error:

java.lang.NullPointerException
at com.example.raynold.cryptorates.MainActivity$1.onResponse(MainActivity.java:53)
at retrofit2.ExecutorCallAdapterFactory$ExecutorCallbackCall$1$1.run(ExecutorCallAdapterFactory.java:70)

UPDATED---------

My MainActivity class

public class MainActivity extends AppCompatActivity {

private RecyclerView mCurrencyRecycler;
private Button mclickMe;
private CurrencyAdapter mCurrencyAdapter;
private List<CryptoCurrency> mCurrencyList;
private CurrencyInterface mCurrencyInterface;
private LinearLayoutManager mLinearLayoutManager;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    mCurrencyRecycler = (RecyclerView) findViewById(R.id.rv_rates);
    mLinearLayoutManager = new LinearLayoutManager(this);
    mCurrencyRecycler.setLayoutManager(mLinearLayoutManager);
    mCurrencyRecycler.setHasFixedSize(true);
    mclickMe = (Button) findViewById(R.id.click_me);

    mCurrencyInterface = ApiClient.getApiClient().create(CurrencyInterface.class);

    Call<CryptoCurrency.BTC> call = mCurrencyInterface.currency("BTC", "NGN");

    call.enqueue(new Callback<CryptoCurrency.BTC>() {
        @Override
        public void onResponse(Call<CryptoCurrency.BTC> call, Response<CryptoCurrency.BTC> response) {
            String currency = response.body().toString();
            Toast.makeText(getApplicationContext(), "result " + currency, Toast.LENGTH_LONG).show();
            Log.i("MainActivity", currency);

        }

        @Override
        public void onFailure(Call<CryptoCurrency.BTC> call, Throwable t) {

        }
    });

    mclickMe.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

        }
    });

}

}

I will also like to know how i can get it to work with a recyclerview since the JSON is of object type not Array.

Raynold
  • 131
  • 9

4 Answers4

0

Change your onResponse method like this:

...........
call.enqueue(new Callback<CryptoCurrency.BTC>() {
        @Override
        public void onResponse(Call<CryptoCurrency.BTC> call, Response<CryptoCurrency.BTC> response) {
            String currency = response.getUSD().toString();
            Toast.makeText(getApplicationContext(), "result " + currency, Toast.LENGTH_LONG).show();
            Log.i("MainActivity", currency);

        }

        @Override
        public void onFailure(Call<CryptoCurrency.BTC> call, Throwable t) {

        }
    });

Try this solve your issue.

HassanUsman
  • 1,787
  • 1
  • 20
  • 38
0

You should not use backslash at the start and end of URLs.

Try

@GET("data/pricemulti")

instead of

@GET("/data/pricemulti/")
amzer
  • 640
  • 4
  • 16
0
public class CryptoCurrency {

        @SerializedName("BTC")
        @Expose
        private BTC BTC;
        @SerializedName("ETH")
        @Expose
        private ETH ETH;

        public BTC getBTC() {
            return BTC;
        }

        public void setBTC(BTC BTC) {
            this.BTC = BTC;
        }

        public ETH getETH() {
            return ETH;
        }

        public void setETH(ETH ETH) {
            this.ETH = ETH;
        }

    class ETH {
      ...
    }

    class BTC {
       ...
    }

}

In your interface CurrencyInterface use like

@GET("/data/pricemulti")
Call<CryptoCurrency> currency(@Query("fsyms") String crypto, @Query("tsyms") String local);

Handle like

Call<CryptoCurrency> call = mCurrencyInterface.currency("BTC", "NGN");

call.enqueue(new Callback<CryptoCurrency>() {
        @Override
        public void onResponse(Call<CryptoCurrency> call, Response<CryptoCurrency> response) {
            BTC btc = response.body().getBTC();
            Toast.makeText(getApplicationContext(), "result " + btc.getNGN(), Toast.LENGTH_LONG).show();

        }

        @Override
        public void onFailure(Call<CryptoCurrency.BTC> call, Throwable t) {

        }
    });
Lingeshwaran
  • 579
  • 4
  • 15
0

check if response.body() not null before this line

String currency = response.body().toString();

if response code not equal 200 your response will be null