54 lines
1.3 KiB
Java
54 lines
1.3 KiB
Java
|
package Classes.api;
|
||
|
|
||
|
import org.json.JSONArray;
|
||
|
import org.json.JSONObject;
|
||
|
|
||
|
import Classes.Event;
|
||
|
import Interfaces.Callback;
|
||
|
import model.NewsListModel;
|
||
|
|
||
|
public class fetchSources implements Callback{
|
||
|
|
||
|
private NewsListModel context;
|
||
|
private String nextQuery;
|
||
|
|
||
|
public fetchSources(NewsListModel context){
|
||
|
|
||
|
this.context = context;
|
||
|
this.nextQuery = nextQuery;
|
||
|
|
||
|
}
|
||
|
|
||
|
|
||
|
@Override
|
||
|
public void onSuccess(JSONObject response){
|
||
|
|
||
|
//on récupère toutes les sources (dans la limite de 20 sources)
|
||
|
JSONArray arr = response.getJSONArray("sources");
|
||
|
|
||
|
for(int i = 0;i<arr.length() && i<20;i++){
|
||
|
|
||
|
String source_i = (JSONObject) arr.get(i);
|
||
|
|
||
|
this.context.sources.add( source_i.getString("id") );
|
||
|
|
||
|
}
|
||
|
|
||
|
this.context.isRetreivingSources = false;
|
||
|
this.context.apiError = false;
|
||
|
|
||
|
//tout s'est bien passé, on peut notifier du succès
|
||
|
this.context.notifyObservers(new Event("NewsModel", "SourcesUpdated"));
|
||
|
|
||
|
}
|
||
|
|
||
|
@Override
|
||
|
public void onError(String errDesc) {
|
||
|
//on notifie de l'échec et on garde en mémoire le fait que l'on a échoué (afin d'annuler la recherche de news qui va suivre)
|
||
|
this.context.isRetreivingSources = false;
|
||
|
this.context.apiError = true;
|
||
|
this.context.notifyObservers(new Event("NewsModel", "SourcesUpdateFailed"));
|
||
|
|
||
|
}
|
||
|
|
||
|
}
|