Android JSON(POJO)模型响应
我的JSON的响应模型有问题 我正在使用hitbtc API: 将此文件转换为POJO模型时,我只有此文件:Android JSON(POJO)模型响应,android,json,retrofit,pojo,Android,Json,Retrofit,Pojo,我的JSON的响应模型有问题 我正在使用hitbtc API: 将此文件转换为POJO模型时,我只有此文件: public class Currency { @SerializedName("id") @Expose private String id; @SerializedName("fullName") @Expose private String fullName; @SerializedName("crypto") @Expose private Boolean crypto; @S
public class Currency {
@SerializedName("id")
@Expose
private String id;
@SerializedName("fullName")
@Expose
private String fullName;
@SerializedName("crypto")
@Expose
private Boolean crypto;
@SerializedName("payinEnabled")
@Expose
private Boolean payinEnabled;
@SerializedName("payinPaymentId")
@Expose
private Boolean payinPaymentId;
@SerializedName("payinConfirmations")
@Expose
private Integer payinConfirmations;
@SerializedName("payoutEnabled")
@Expose
private Boolean payoutEnabled;
@SerializedName("payoutIsPaymentId")
@Expose
private Boolean payoutIsPaymentId;
@SerializedName("transferEnabled")
@Expose
private Boolean transferEnabled;
@SerializedName("delisted")
@Expose
private Boolean delisted;
@SerializedName("payoutFee")
@Expose
private String payoutFee;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getFullName() {
return fullName;
}
public void setFullName(String fullName) {
this.fullName = fullName;
}
public Boolean getCrypto() {
return crypto;
}
public void setCrypto(Boolean crypto) {
this.crypto = crypto;
}
public Boolean getPayinEnabled() {
return payinEnabled;
}
public void setPayinEnabled(Boolean payinEnabled) {
this.payinEnabled = payinEnabled;
}
public Boolean getPayinPaymentId() {
return payinPaymentId;
}
public void setPayinPaymentId(Boolean payinPaymentId) {
this.payinPaymentId = payinPaymentId;
}
public Integer getPayinConfirmations() {
return payinConfirmations;
}
public void setPayinConfirmations(Integer payinConfirmations) {
this.payinConfirmations = payinConfirmations;
}
public Boolean getPayoutEnabled() {
return payoutEnabled;
}
public void setPayoutEnabled(Boolean payoutEnabled) {
this.payoutEnabled = payoutEnabled;
}
public Boolean getPayoutIsPaymentId() {
return payoutIsPaymentId;
}
public void setPayoutIsPaymentId(Boolean payoutIsPaymentId) {
this.payoutIsPaymentId = payoutIsPaymentId;
}
public Boolean getTransferEnabled() {
return transferEnabled;
}
public void setTransferEnabled(Boolean transferEnabled) {
this.transferEnabled = transferEnabled;
}
public Boolean getDelisted() {
return delisted;
}
public void setDelisted(Boolean delisted) {
this.delisted = delisted;
}
public String getPayoutFee() {
return payoutFee;
}
public void setPayoutFee(String payoutFee) {
this.payoutFee = payoutFee;
}
}
在下一步中,我想获得项目列表,如下所示:
public class CurrencyResponse {
@SerializedName("page")
private int page;
@SerializedName("results")
private List<Currency> results;
@SerializedName("total_results")
private int totalResults;
@SerializedName("total_pages")
private int totalPages;
public int getPage() {
return page;
}
public void setPage(int page) {
this.page = page;
}
public List<Currency> getResults() {
return results;
}
public void setResults(List<Currency> results) {
this.results = results;
}
public int getTotalResults() {
return totalResults;
}
public void setTotalResults(int totalResults) {
this.totalResults = totalResults;
}
public int getTotalPages() {
return totalPages;
}
public void setTotalPages(int totalPages) {
this.totalPages = totalPages;
}
}
MainActivity.java:
public class MainActivity extends AppCompatActivity {
private static final String TAG = MainActivity.class.getSimpleName();
// TODO - insert your themoviedb.org API KEY here
private final static String API_KEY = "My_key";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (API_KEY.isEmpty()) {
Toast.makeText(getApplicationContext(), "Please obtain your API KEY from hitbtc.com first!", Toast.LENGTH_LONG).show();
return;
}
final RecyclerView recyclerView = (RecyclerView) findViewById(R.id.movies_recycler_view);
LinearLayoutManager manager = new LinearLayoutManager(this);
recyclerView.setLayoutManager(manager);
recyclerView.setHasFixedSize(true);
ApiInterface apiService =
ApiClient.getClient().create(ApiInterface.class);
Call<CurrencyResponse> call = apiService.getCurrencies(API_KEY);
call.enqueue(new Callback<CurrencyResponse>() {
@EverythingIsNonNull
public void onResponse(Call<CurrencyResponse> call, Response<CurrencyResponse> response) {
int statusCode = response.code();
List<Currency> currencies = response.body().getResults();
recyclerView.setAdapter(new CurrenciesAdapter(currencies, R.layout.list_item_currency, getApplicationContext()));
}
@Override
public void onFailure(Call<CurrencyResponse> call, Throwable t) {
// Log error here since request failed
Log.e(TAG, t.toString());
}
});
}
}
我查看了您在描述中提供的上述链接,我知道有一个数组作为响应。因此,根据这一点,您不应该需要
CurrencyResponse
类。只要Currency
class就足够了
在改型界面中,api方法应返回货币列表
。e、 g.List getCurrencyList()
。它应该会起作用
对于NPE,可能的原因应该是您试图从api响应中不包含的这些类访问字段,因此该字段将为null,访问该字段将抛出NPE。您可以检查stacktrace以了解哪个字段访问正在抛出NPE,或者您可以共享
stacktrace
,以便该社区可以帮助您。我检查了您在说明中提供的上述链接,我知道有一个数组响应。因此,根据这一点,您不应该需要CurrencyResponse
类。只要Currency
class就足够了
在改型界面中,api方法应返回货币列表
。e、 g.List getCurrencyList()
。它应该会起作用
对于NPE,可能的原因应该是您试图从api响应中不包含的这些类访问字段,因此该字段将为null,访问该字段将抛出NPE。您可以检查stacktrace以了解哪个字段访问正在抛出NPE,或者您可以共享
stacktrace
,以便该社区可以帮助您。您在哪里拥有NPE?在哪一行?您是否会共享堆栈跟踪(来自异常)?显示适配器代码,并根据API响应(来自给定链接)发布堆栈跟踪信息,无需CurrencyResponse
类。只需在API界面中使用List
。@Basi,在您有NPE的地方,列表就完成了?在哪一行?您是否会共享堆栈跟踪(来自异常)?显示适配器代码,并根据API响应(来自给定链接)发布堆栈跟踪信息,无需CurrencyResponse
类。只需在API界面中使用List
。@Basi,列表已完成
public class MainActivity extends AppCompatActivity {
private static final String TAG = MainActivity.class.getSimpleName();
// TODO - insert your themoviedb.org API KEY here
private final static String API_KEY = "My_key";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (API_KEY.isEmpty()) {
Toast.makeText(getApplicationContext(), "Please obtain your API KEY from hitbtc.com first!", Toast.LENGTH_LONG).show();
return;
}
final RecyclerView recyclerView = (RecyclerView) findViewById(R.id.movies_recycler_view);
LinearLayoutManager manager = new LinearLayoutManager(this);
recyclerView.setLayoutManager(manager);
recyclerView.setHasFixedSize(true);
ApiInterface apiService =
ApiClient.getClient().create(ApiInterface.class);
Call<CurrencyResponse> call = apiService.getCurrencies(API_KEY);
call.enqueue(new Callback<CurrencyResponse>() {
@EverythingIsNonNull
public void onResponse(Call<CurrencyResponse> call, Response<CurrencyResponse> response) {
int statusCode = response.code();
List<Currency> currencies = response.body().getResults();
recyclerView.setAdapter(new CurrenciesAdapter(currencies, R.layout.list_item_currency, getApplicationContext()));
}
@Override
public void onFailure(Call<CurrencyResponse> call, Throwable t) {
// Log error here since request failed
Log.e(TAG, t.toString());
}
});
}
}
--------- beginning of crash
2019-03-11 13:58:12.020 3325-3325/com.example.mojpierwszyrest
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.mojpierwszyrest, PID: 3325
java.lang.NullPointerException: Attempt to invoke virtual method 'java.util.List com.example.mojpierwszyrest.model.CurrencyResponse.getResults()' on a null object reference
at com.example.mojpierwszyrest.acitivity.MainActivity$1.onResponse(MainActivity.java:63)
at retrofit2.ExecutorCallAdapterFactory$ExecutorCallbackCall$1$1.run(ExecutorCallAdapterFactory.java:71)
at android.os.Handler.handleCallback(Handler.java:873)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:6669)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run
(RuntimeInit.java:493)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
2019-03-11 13:58:12.563 3325-3325/com.example.mojpierwszyrest I/Process:
Sending signal. PID: 3325 SIG: 9