Je suis essayer d'analyser ce tableau Json à partir de ce site NewsApi et l'afficher dans un listView.When je cours l'application rien affiche.Voici mon code.Qu'est-ce que je fais mal ?. J'ai essayé de déboguer le problème en vain.Je ne peux pas analyser JSON en utilisant le retrofit et utiliser listView
public class NewsApiClient {
public static String API_BASE_URL = "https://newsapi.org/v1/";
OkHttpClient.Builder public static httpClient = new OkHttpClient.Builder();
public static Retrofit.Builder builder = new Retrofit.Builder()
.baseUrl(API_BASE_URL)
.addConverterFactory(GsonConverterFactory.create()
);
public static Retrofit retrofit = builder.client(httpClient.build()).build();
NewsApiClient client = retrofit.create(NewsApiClient.class);
}
appeler le point final? Je n'utilise que les attributs de nom, de description et de catégorie.
public class Source {
@SerializedName("id")
public String id;
@SerializedName("name")
public String name;
@SerializedName("description")
public String description;
@SerializedName("url")
public String url;
@SerializedName("category")
public String category;
@SerializedName("language")
public String language;
@SerializedName("country")
public String country;
}
public class SourcesAdapter extends BaseAdapter {
Context context;
List<Source> sourceList;
public SourcesAdapter(Context context,List<Source> sourceList){
this.context = context;
this.sourceList = sourceList;
}
@Override
public int getCount() {
return sourceList.size();
}
@Override
public Object getItem(int position) {
return sourceList.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater layoutInflater = LayoutInflater.from(context);
convertView = layoutInflater.inflate(R.layout.sources_list,null);
Source currentsource = sourceList.get(position);
TextView sourcesName = (TextView) convertView.findViewById(R.id.sources_name);
TextView sourcesDescription = (TextView) convertView.findViewById(R.id.sources_description);
TextView sourcesCategory = (TextView) convertView.findViewById(R.id.sources_category);
sourcesName.setText(currentsource.name);
sourcesDescription.setText(currentsource.description);
sourcesCategory.setText(currentsource.category);
return convertView;
}
}
public class SourcesFragment extends Fragment {
ListView listView;
public SourcesFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
//
//instance of the adapter to this listview
View view = inflater.inflate(R.layout.fragment_sources, container, false);
listView = (ListView)view.findViewById(R.id.list_view_sources) ;
getSources();
return view;
}
public void getSources(){
Retrofit retrofit = NewsApiClient.builder.build();
NewsApiInterface newsApiInterface = retrofit.create(NewsApiInterface.class);
Call<SourcesResponse> sourcesCall = newsApiInterface.getTechSources("en", "technology");
sourcesCall.enqueue(new Callback<SourcesResponse>() {
@Override
public void onResponse(Call<SourcesResponse> call, Response<SourcesResponse> response) {
List<Source> sources = response.body().sources;
SourcesAdapter sourcesAdapter = new SourcesAdapter(getContext(),sources);
listView.setAdapter(sourcesAdapter);
}
@Override
public void onFailure(Call<SourcesResponse> call, Throwable t) {
}
});
}
}
public class SourcesResponse {
@SerializedName("status")
public String status;
@SerializedName("sources")
public List<Source> sources;
}
I have created 3 fragments,the sources fragment is one of them.On the sources fragment i only want to display sources with technology.
Thank you in advance!
« Je essayé de déboguer le problème en vain. " Vos tentatives de débogage ont-elles généré une LogCat par hasard? – FWeigl
@Ascorbin, oui c'est fait. – Olal