2017-09-04 10 views
1

このJsonの配列をこのサイトNewsApiから解析してlistViewに表示しようとしています。アプリを実行しても何も表示されません。ここにコードがあります。問題をデバッグしようとしましたが、無駄です。改造を使用してJSONを構文解析できないlistView

パブリッククラスNewsApiClient {

public static String API_BASE_URL = "https://newsapi.org/v1/"; 

パブリック静的OkHttpClient.BuilderのHttpClient =新しい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); 

}

public interface NewsApiInterface { 
    //endpoint 
     @GET("sources") 
     Call <SourcesResponse> getTechSources(
       @Query("language") String language, 
       @Query("category") String category) 
    } 

名前、説明、およびカテゴリの属性のみを使用しています。あなたの改造のonResponseコールバックメソッドで

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

「に

response.body().sources 

を変更するように、ソース用のゲッターとセッターを追加問題をデバッグしようとしました。 万一、あなたのデバッグ試行でLogCatが生成されましたか? – FWeigl

+0

@Ascorbin、そうでした。 – Olal

答えて

0

、あなたはresponse.body().sourcesなどのソースリストにアクセスしています。代わりにSourcesResponseでこれのこの

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

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

は今換装のごonResponseコールバックに行き、私が持っている

response.body().getSources() 
関連する問題