2017-09-04 11 views
1

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!   
+0

« 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

+0

@Ascorbin, oui c'est fait. – Olal

Répondre

0

Dans votre méthode de rappel onResponse de mise à niveau, vous accédez à la liste des sources comme response.body().sources. Au lieu de cela dans le SourcesResponse, ajoutez un getter et setter pour les sources comme celui-ci

public List<SourcesResponse> getSources() { 
    return sources; 
} 

public void setSources(List<SourcesResponse> sources) { 
    this.sources = sources; 
} 

Maintenant, allez à votre rappel onResponse de rénovation et le changement

response.body().sources 

à

response.body().getSources()