I am trying to build an application that is supposed to use an open source sport API to get a list of football (Soccer) tournaments and display it on a RecyclerView, for some reason I am getting two errors:
1- com.mad.footstats E/RecyclerView: No adapter attached; skipping layout
2- E/MainActivity: com.google.gson.stream.MalformedJsonException: Use JsonReader.setLenient(true) to accept malformed JSON at line 1 column 1 path $
I couldn't make it work from previous answers on the same questions so please if you can take the time and look at my code: This is the 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());
}
});
}
This is the code for the 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();
}
And this is the code for the ApiClient Class:
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;
}
For some reason the Adapter isn't attached and the API isn't working even thought I saw examples and tutorials having the same code. Thanks.
Developer Inactive
` . Thats is why you get that xml error and because you do not get response your recycler view adapter would not be set. – Omid Heshmatinia May 13 '18 at 05:58