我是编程新手,我想显示回收器视图,代码表中没有错误,但在日志中我发现com.example.beaconsenjacoffee.ListPromo$1.onResponse(ListPromo.java:49)
我的代码有什么问题?
public class ListPromo extends AppCompatActivity {
private RecyclerView recyclerView;
private ArrayList<NotificationList> data;
private DataAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_list_promo);
initviews();
}
private void initviews(){
recyclerView = findViewById(R.id.card_recycler_view);
recyclerView.setHasFixedSize(true);
RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(getApplicationContext());
recyclerView.setLayoutManager(layoutManager);
loadJSON();
}
private void loadJSON(){
Retrofit retrofit = new Retrofit.Builder()
.baseUrl("http://api.learn2crack.com")
.addConverterFactory(GsonConverterFactory.create())
.build();
RequestInterface request = retrofit.create(RequestInterface.class);
Call<JSONResponse> call = request.getJSON();
call.enqueue(new Callback<JSONResponse>() {
@Override
public void onResponse(Call<JSONResponse> call, Response<JSONResponse> response) {
JSONResponse jsonResponse = response.body();
data = new ArrayList<>(Arrays.asList(jsonResponse.getListNotif()));
adapter = new DataAdapter(data);
recyclerView.setAdapter(adapter);
}
@Override
public void onFailure(Call<JSONResponse> call, Throwable t) {
Log.d("Error",t.getMessage());
}
});
}
}
答案 0 :(得分:1)
URL错误
URL ="http://api.learn2crack.com"
检查您传递的网址,返回无效的json
值,即"Hello"
示例:-
有效的URL = "https://api.androidhive.info/contacts/"
https://www.androidhive.info/2012/01/android-json-parsing-tutorial/