java - RecyclerView 没有附加适配器;跳过布局(无法从以前的问题中解决)
问题描述
我正在尝试构建一个应用程序,该应用程序应该使用开源运动 API 来获取足球(足球)锦标赛列表并将其显示在 RecyclerView 上,由于某种原因,我收到两个错误:
1- com.mad.footstats E/RecyclerView:未连接适配器;跳过布局
2- E/MainActivity: com.google.gson.stream.MalformedJsonException: 使用 JsonReader.setLenient(true) 在第 1 行第 1 列路径接受格式错误的 JSON
我无法从以前对相同问题的答案中使它起作用,所以请您花时间看看我的代码:这是 MainActivity:
public class MainActivity extends AppCompatActivity {
private static final String TAG = MainActivity.class.getSimpleName();
private final static String API_KEY = "w7c74newrykj8m57rda6xwrk";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Toast if the API key is empty
if(API_KEY .isEmpty()){
Toast.makeText(getApplicationContext(), R.string.api_empty_toast, Toast.LENGTH_SHORT).show();
}
final RecyclerView mRecyclerView = findViewById(R.id.tournaments_rv);
mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
ApiInterface apiService =
ApiClient.getClient().create(ApiInterface.class);
Call<TournamentResponse> call = apiService.getTournamentList(API_KEY);
call.enqueue(new Callback<TournamentResponse>() {
@Override
public void onResponse(Call<TournamentResponse> call, Response<TournamentResponse> response) {
int statusCode = response.code();
List<Tournament_> tournaments = response.body().getResults();
mRecyclerView.setAdapter(new TournamentsAdapter(tournaments,R.layout.tournament_item,getApplicationContext()));
}
@Override
public void onFailure(Call<TournamentResponse> call, Throwable t) {
Log.e(TAG, t.toString());
}
});
}
这是 TournamentsAdapter 的代码:
public class TournamentsAdapter extends RecyclerView.Adapter<TournamentsAdapter.LeagueViewHolder>{
private List<Tournament_> mTournaments;
private int rowLayout;
private Context context;
public static class LeagueViewHolder extends RecyclerView.ViewHolder {
LinearLayout TournamentLayout;
TextView tournamentName, tournamentNation, tournamentYear;
public LeagueViewHolder(View v) {
super(v);
TournamentLayout = v.findViewById(R.id.league_layout);
tournamentName = v.findViewById(R.id.tournament_name);
tournamentNation = v.findViewById(R.id.tournament_nation);
tournamentYear = v.findViewById(R.id.tournament_year);
}
}
public TournamentsAdapter(List<Tournament_> tournaments, int rowLayout, Context context){
this.mTournaments = tournaments;
this.rowLayout = rowLayout;
this.context = context;
}
@Override
public TournamentsAdapter.LeagueViewHolder onCreateViewHolder(ViewGroup parent,
int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(rowLayout, parent, false);
return new LeagueViewHolder(view);
}
@Override
public void onBindViewHolder(LeagueViewHolder holder, final int position){
holder.tournamentName.setText(mTournaments.get(position).getName());
holder.tournamentNation.setText(mTournaments.get(position).getCurrentSeason().getYear());
holder.tournamentYear.setText(mTournaments.get(position).getCategory().getName());
}
@Override
public int getItemCount() {
return mTournaments.size();
}
这是 ApiClient 类的代码:
public class ApiClient {
public static final String BASE_URL = "https:api.sportradar.us/soccer-xt3/eu/en/";
private static Retrofit retrofit = null;
public static Retrofit getClient() {
if (retrofit==null) {
retrofit = new Retrofit.Builder()
.baseUrl(BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.build();
}
return retrofit;
}
由于某种原因,适配器未连接,API 无法正常工作,即使我看到了具有相同代码的示例和教程。谢谢。
解决方案
问题出在您的链接上。您为端点选择了错误的参数。您将永远不会收到 json 响应,因此改造无法为您解析它并且您正在将您的回收器视图填充到从未调用过的改造响应中。
只需将 xml 更改为 json 即可获得正确的响应。
https://api.sportradar.us/soccer-xt3/eu/en/tournaments.xml?api_key=API_KEY
至
https://api.sportradar.us/soccer-xt3/eu/en/tournaments.json?api_key=API_KEY
推荐阅读
- django - 会话身份验证不适用于 django 和 vuejs
- java - ActiveMQ 命令行异常:“无法检索 RMIServer 存根”
- javascript - 如何在 Javascript 中处理此 API 响应?
- excel - 使用 Countifs、Sumif 从多个工作表中的同一单元格将数据拉入我的仪表板
- java - IntelliJ IDEA 无法启动:无法加载 JDK 类:com.sun.jdi.Field
- javascript - 为什么我在正文底部的脚本会阻塞页面?
- c# - DocuSign C# SDK:ResourceToken 和 ViewLink 对于收件人证明端点为空
- ruby-on-rails - HAML ActionView::Template::Error SyntaxError: stdin 缺少缩进
- swift - 使用 Combine 设置 Publishers,同时满足对非 nil 变量初始值的要求
- javascript - 如何在此 JavaScript .text 行中开始新行?