From 606cfe8aebfaaf35ad4a6c40303cede47d95dc13 Mon Sep 17 00:00:00 2001 From: xdrm-brackets Date: Sun, 19 Nov 2017 16:30:22 +0100 Subject: [PATCH] Clean @1 --- Classes/ApiCall.java | 336 +++++++++++++++++++++----------- Classes/Category.java | 27 ++- Classes/Event.java | 36 +++- Interfaces/Callback.java | 15 +- Interfaces/DelayedCallback.java | 4 +- Interfaces/Event.java | 2 +- controller/Article.java | 6 +- controller/HeaderMenu.java | 59 ++++-- controller/RootLayout.java | 161 ++++++++------- fxml/article_disp.fxml | 2 +- fxml/model.fxml | 70 +------ model/LangModel.java | 38 ++-- model/NewsListModel.java | 25 ++- 13 files changed, 464 insertions(+), 317 deletions(-) diff --git a/Classes/ApiCall.java b/Classes/ApiCall.java index c78cad2..7e878a5 100644 --- a/Classes/ApiCall.java +++ b/Classes/ApiCall.java @@ -22,154 +22,272 @@ import javafx.util.Pair; public class ApiCall implements Runnable, EventObserver { - //Methode appelée une fois que la requete s'est terminé - private Callback callback; - //connection HTTP - private HttpURLConnection connection; - //méthode HTTP - private String method; - //est ce que cet appel d'API attend un évenement pour se lancer - private Boolean isDelayed = false; - //Methode appellée une fois l'évenement arrivé, il retourne la nouvelle URL et les nouveaux parametres POST - private DelayedCallback delayedCallback; - //ID et Type de l'évenement attendu - private String eventID; - private String eventType; - //objet contenant le thread de l'appel - private Thread thread; - //si une erreur surviens durant le delayedCallback, la boucle run doit tout arreter, cette variable sert a notifier run() de ça - private Boolean shouldStop = false; + /* (1) Attributes + ---------------------------------------------------------*/ + /* (1) Request data */ + private String httpMethod; // HTTP method + private HttpURLConnection httpConnection; // HTTP connection - - public ApiCall(String URL, String method, Callback call) { - this.callback = call; - this.method = method; + /* (2) Callback data */ + private Boolean isDelayed = false; // If must wait for an event to start + private Callback callback; // Callback ran when the request ends + private DelayedCallback delayedCallback; // Callback ran when event handled, returns new URL and POST parameters + private String eventID; // awaited event ID + private String eventType; // awaited event Type + private Thread thread; // main Thread + private Boolean shouldStop = false; // If delayedCallback error -> run() must stop (notify) + + + + /* (2) API call constructor + * + * @tUrl Target URL + * @tMethod HTTP method + * @callback Callback run after the HTTP response received + * + ---------------------------------------------------------*/ + public ApiCall(String tUrl, String tMethod, Callback callback) { + + /* (1) Store attributes + ---------------------------------------------------------*/ + this.callback = callback; + this.httpMethod = tMethod; + + + /* (2) Create the connection + ---------------------------------------------------------*/ try { - //création de la connection - URL url = new URL(URL); - this.connection = (HttpURLConnection) url.openConnection(); - this.connection.setRequestMethod(method); - //si on fait une requete GET, il faut interdire l'envoie de donnée POST - if(method == "GET") { - this.connection.setDoOutput(false); - }else { - this.connection.setDoOutput(true); - } - } catch (IOException e) { - this.callback.onError(); + + /* (1) Set the target URL */ + URL url = new URL(tUrl); + + /* (2) Create the connection from URL */ + this.httpConnection = (HttpURLConnection) url.openConnection(); + + /* (3) Set the HTTP method */ + this.httpConnection.setRequestMethod(this.httpMethod); + + /* (4) If GET request, forbid POST data to be sent */ + this.httpConnection.setDoOutput( httpMethod == "POST" ); + + }catch( IOException e ){ + + /* (x) If cannot create connection -> trigger the onError() callback */ + this.callback.onError("cannot create http connection"); + } + } - public void addHeaders(HashMap map) { - Iterator it = map.entrySet().iterator(); - while (it.hasNext()) { - Map.Entry pair = (Map.Entry)it.next(); - this.connection.setRequestProperty((String)pair.getKey(), (String)pair.getValue()); - it.remove(); // évite les erreurs en cas de modification concurente + + /* (3) Add headers to the HTTP connection + * + * @headerMap> HashMap containing the header pairs (key, value) + * + ---------------------------------------------------------*/ + public void addHeaders(HashMap headerMap){ + + /* (1) Create iterator */ + Iterator it = headerMap.entrySet().iterator(); + + /* (2) Iterate until the end */ + while( it.hasNext() ){ + + /* (2.1) Get the current header pair */ + Map.Entry pair = (Map.Entry) it.next(); + + /* (2.2) Add the header to the @httpConnection */ + this.httpConnection.setRequestProperty( + (String) pair.getKey(), + (String) pair.getValue() + ); + + /* (2.3) Avoid concurrency errors -> remove current header */ + it.remove(); + } + } + @Override public void run() { - //si notre appel est delayed, on endort le thread - if(this.isDelayed) { - try { - //on doit se synchroniser avec le thread avant de l'endormir - synchronized(this.thread) { - this.thread.wait(); - } - } catch (InterruptedException e) { - this.callback.onError(); + + /* (1) If delayed call -> sleep the thread, wait for an event + ---------------------------------------------------------*/ + if( this.isDelayed ){ + + /* (1) Try to sleep the thread */ + try{ + + synchronized(this.thread){ this.thread.wait(); } + + /* (2) If cannot -> call callback onError() */ + }catch(InterruptedException e){ + + this.callback.onError("cannot sleep thread"); return; + } + } - - //si il y a eu une erreur durant le delayedallback, on stope le thread - if(this.shouldStop) { + + + /* (2) If thread must stop (error during delayedCallback) + ---------------------------------------------------------*/ + if( this.shouldStop ) return; - } + + + /* (3) Manage response + ---------------------------------------------------------*/ try { - //On envoie les paramètres POST si besoin - if(this.method != "GET") { - DataOutputStream wr = new DataOutputStream (connection.getOutputStream ()); - //TODO: implémenter la gestion des parametres POST + + /* (1) Get output stream to set Post data */ + if( this.httpMethod == "POST" ){ + + DataOutputStream wr = new DataOutputStream( httpConnection.getOutputStream() ); + // TODO: manage post data wr.flush (); wr.close (); + } - //on récupère la réponse de la requete - InputStream is = connection.getInputStream(); - BufferedReader rd = new BufferedReader(new InputStreamReader(is)); - String line; - StringBuffer response = new StringBuffer(); - while((line = rd.readLine()) != null) { + /* (2) Prepare to get response */ + InputStream is = httpConnection.getInputStream(); // Get input stream + BufferedReader rd = new BufferedReader(new InputStreamReader(is)); // Input stream into readable buffer + String line; + StringBuffer response = new StringBuffer(); + + /* (3) Get response */ + while( ( line = rd.readLine() ) != null ){ response.append(line); } + rd.close(); - - //transformation en objet JSON - try { - //raplce null values by empty strings - JSONObject json = new JSONObject(response.toString().replaceAll(":null,", ":\"\",")); - this.callback.onSuccess(json); - }catch(JSONException e) { - this.callback.onError(); + + /* (4) Parse JSON */ + try{ + + // {4.1} Specific: replace 'null' by empty strings // + JSONObject parsedJson = new JSONObject(response.toString().replaceAll(":null,", ":\"\",")); + + // {4.2} If no error -> trigger onSuccess() // + this.callback.onSuccess(parsedJson); + + /* (5) If cannot parse JSON -> trigger onError() */ + }catch(JSONException jsonex){ + + this.callback.onError("json parse error"); + } - } catch (IOException e) { - this.callback.onError(); + + /* (4) If cannot get response -> trigger onError() + ---------------------------------------------------------*/ + }catch (IOException ioex){ + + this.callback.onError("cannot get response"); + } + } + /* (5) Launches the run() loop in a Thread + * + ---------------------------------------------------------*/ public void send() { - //send sert juste a lancer la boucle principale run() + + /* (1) Create a new Thread for the current class */ this.thread = new Thread(this); + + /* (2) Start the thread */ this.thread.start(); + } - - public void setAsDelayedCall(String eventID, String eventType, DelayedCallback call) { - //on enregistre tout ce dont on a besoin pour traiter l'évenement - this.isDelayed = true; - this.delayedCallback = call; - this.eventID = eventID; - this.eventType = eventType; + + + /* (6) Bind a delayed callback to the API call (chain another API call) + * + * @eventID Event ID + * @eventType Event Type + * @delayedCallback Delayed callback + * + ---------------------------------------------------------*/ + public void setAsDelayedCall(String eventID, String eventType, DelayedCallback delayedCallback) { + + /* (1) Notify the delayed callback */ + this.isDelayed = true; + this.delayedCallback = delayedCallback; + this.eventID = eventID; + this.eventType = eventType; + } + @Override public void handleEvent(Event e) { - //si on est un appel delayed et que l'évenement est le bon, on lance le delayedCallback - if(this.isDelayed && e.getObjectId() == this.eventID && e.getEventType() == this.eventType) { - Pair> returned = this.delayedCallback.call(); - if(this.method == "GET") { - try { - //on met a jour la connection HTTP avec la nouvelle URL en gardant l'ancienne méthode HTTP - HttpURLConnection newConnection = (HttpURLConnection) new URL(returned.getKey()).openConnection(); - newConnection.setRequestMethod(this.connection.getRequestMethod()); - newConnection.setDoOutput(this.connection.getDoOutput()); - this.connection = newConnection; - //on a fini de mettre a jour la connection, le thread principal peut reprendre - synchronized(this.thread) { - this.thread.notify(); - } - } catch (IOException e1) { - //une erreur est survenue, on stope tout - this.callback.onError(); - this.shouldStop = true; - synchronized(this.thread) { - this.thread.notify(); - } + + /* (1) If delayed callback and right event received + ---------------------------------------------------------*/ + if( this.isDelayed && e.getObjectId() == this.eventID && e.getEventType() == this.eventType ){ + + try{ + + /* (1) Call the callback and fetch its retusn URL+POST params */ + Pair> newData = this.delayedCallback.call(); + URL newURL = new URL(newData.getKey()); + //HashMap newPostData = newData.getValue(); + + /* (2) Create the new connection */ + HttpURLConnection newConnection = (HttpURLConnection) newURL.openConnection(); + + /* (3) Keep the same HTTP method as previous request */ + String lastHttpMethod = this.httpConnection.getRequestMethod(); + newConnection.setRequestMethod( lastHttpMethod ); + + /* (4) If GET request, forbid POST data to be sent */ + newConnection.setDoOutput( lastHttpMethod == "POST" ); + + /* (5) Manage POST data */ + if( this.httpMethod == "POST" ){ + // TODO: implement POST params management } - }else { - //TODO: implémenter les param POST + + /* (6) Update current connection (replace with new) */ + this.httpConnection = newConnection; + + /* (7) Replay main thread */ + synchronized(this.thread){ this.thread.notify(); } + + + /* (1.8) If error while creating new connection -> onError() + ---------------------------------------------------------*/ + }catch( IOException ioe ){ + + /* (1) Launch onError() callback method */ + this.callback.onError("cannot create delayed http connection"); + + /* (2) Stop the main thread */ + this.shouldStop = true; + synchronized(this.thread){ this.thread.notify(); } + } - }else { - this.callback.onError(); + + + /* (2) If not a delayed call, or not same event (id/type) + ---------------------------------------------------------*/ + }else{ + + /* (1) Launch onError() callback method */ + this.callback.onError("unknown event"); + + /* (2) Stop the main thread */ this.shouldStop = true; - synchronized(this.thread) { - this.thread.notify(); - } + synchronized(this.thread){ this.thread.notify(); } + } - + } } diff --git a/Classes/Category.java b/Classes/Category.java index f910b19..82061dd 100644 --- a/Classes/Category.java +++ b/Classes/Category.java @@ -1,21 +1,20 @@ package Classes; public enum Category { -// business, entertainment, gaming, general, healthAndMedical, music, politics, scienceAndNature, sport, technology, all - all("all", "black"), - business("business", "red"), - entertainment("entertainment", "skyblue"), - gaming("gaming", "green"), - health("health", "yellow"), - music("music", "purple"), - sport("sport", "brown"), - science("science", "#f14405"), - nature("nature", "#16c668"), - economics("economics", "#d1991b"), - politics("politics", "#6825f4"), - technology("technology", "#1e7ebe"); - + all ( "all", "black" ), + business ( "business", "red" ), + entertainment ( "entertainment", "skyblue" ), + gaming ( "gaming", "green" ), + health ( "health", "yellow" ), + music ( "music", "purple" ), + sport ( "sport", "brown" ), + science ( "science", "#f14405" ), + nature ( "nature", "#16c668" ), + economics ( "economics", "#d1991b" ), + politics ( "politics", "#6825f4" ), + technology ( "technology", "#1e7ebe" ); + protected String color; protected String label; Category(String label, String color){ this.label = label; this.color = color; } diff --git a/Classes/Event.java b/Classes/Event.java index b716fc6..a21228c 100644 --- a/Classes/Event.java +++ b/Classes/Event.java @@ -1,22 +1,44 @@ package Classes; public class Event implements Interfaces.Event { - + + /* (1) Attributes: Event identifiers + ---------------------------------------------------------*/ private String objectId; private String eventType; - + + + /* (2) Constructs an event + * + * @objectId Event ID + * @eventType Type of event (arbitrary) + * + ---------------------------------------------------------*/ public Event(String objectId, String eventType) { + this.objectId = objectId; this.eventType = eventType; - + } - public String getObjectId() { - return objectId; + + /* (3) GET: @objectId + * + ---------------------------------------------------------*/ + public String getObjectId(){ + + return this.objectId; + } - public String getEventType() { - return eventType; + + /* (4) GET: @eventType + * + ---------------------------------------------------------*/ + public String getEventType(){ + + return this.eventType; + } } diff --git a/Interfaces/Callback.java b/Interfaces/Callback.java index 6689331..68e2eff 100644 --- a/Interfaces/Callback.java +++ b/Interfaces/Callback.java @@ -4,8 +4,17 @@ import org.json.JSONObject; public interface Callback { + /* (1) Called when success state reached + * + * @response The fetched response + * + ---------------------------------------------------------*/ public void onSuccess(JSONObject response); - - public void onError(); - + + + /* (2) Called when success state cannot be reached + * + ---------------------------------------------------------*/ + public void onError(String errDesc); + } diff --git a/Interfaces/DelayedCallback.java b/Interfaces/DelayedCallback.java index 8e03db9..d8c63c5 100644 --- a/Interfaces/DelayedCallback.java +++ b/Interfaces/DelayedCallback.java @@ -4,8 +4,10 @@ import java.util.HashMap; import javafx.util.Pair; + + public interface DelayedCallback { public Pair> call(); - + } diff --git a/Interfaces/Event.java b/Interfaces/Event.java index db910ce..80c61c2 100644 --- a/Interfaces/Event.java +++ b/Interfaces/Event.java @@ -3,6 +3,6 @@ package Interfaces; public interface Event { public String getObjectId(); - + public String getEventType(); } diff --git a/controller/Article.java b/controller/Article.java index 45fcb57..51d467a 100644 --- a/controller/Article.java +++ b/controller/Article.java @@ -114,7 +114,11 @@ public class Article{ ImageView g_image = (ImageView) p_parent.getChildren().get(0); /* (2) Update title */ - g_image.setImage(new Image(p_uri)); + try{ + g_image.setImage(new Image(p_uri)); + }catch(IllegalArgumentException wrongurlex){ + System.out.println("Cannot find image URL: '"+p_uri+"'"); + } } diff --git a/controller/HeaderMenu.java b/controller/HeaderMenu.java index 2a2cc75..6f8a54b 100644 --- a/controller/HeaderMenu.java +++ b/controller/HeaderMenu.java @@ -10,38 +10,65 @@ import javafx.scene.input.MouseEvent; import javafx.scene.layout.FlowPane; public class HeaderMenu{ - - /* Data */ + + /* (1) Attributes + ---------------------------------------------------------*/ private ArrayList items; - private FlowPane parent; - private EventObserver observer; - - + private FlowPane parent; + private EventObserver observer; + + /* Constructor */ + + /* (2) Constructor + * + * @p_parent gParent element + * @observer Main observer (to send event to) + * + ---------------------------------------------------------*/ public HeaderMenu(FlowPane p_parent, EventObserver observer){ - this.parent = p_parent; - this.items = new ArrayList(); + + this.parent = p_parent; + this.items = new ArrayList(); this.observer = observer; + } + + /* (3) Add a new menu item (graphically too) + * + * @p_name The new menu item's name + * @p_image_uri The new menu item's image link + * + ---------------------------------------------------------*/ public void addItem(String p_name, String p_image_uri) { - + + /* (1) Create ImageView */ ImageView menuItem = new ImageView(); - - menuItem.setImage(new Image(p_image_uri)); - menuItem.setId("header_menu_item_"+p_name); - menuItem.getStyleClass().add("header_menu_item"); - + + /* (2) Set useful attributes */ + menuItem.setImage(new Image(p_image_uri)); // image link + menuItem.setId("header_menu_item_"+p_name); // id + menuItem.getStyleClass().add("header_menu_item"); // css .class + + /* (3) Bind event -> 'changeMainLayout' */ menuItem.setOnMousePressed(new EventHandler() { + @Override public void handle(MouseEvent event) { + HeaderMenu.this.observer.handleEvent(new Classes.Event(menuItem.getId(), "changeMainLayout")); + } + }); - + + /* (4) Add item to list */ this.items.add(menuItem); - // Add to parent + + /* (5) Add gitem to gparent */ parent.getChildren().add(menuItem); + } } diff --git a/controller/RootLayout.java b/controller/RootLayout.java index 673734b..f7c692d 100644 --- a/controller/RootLayout.java +++ b/controller/RootLayout.java @@ -22,159 +22,178 @@ import model.NewsListModel; import model.NewsModel; public class RootLayout extends Application implements EventObserver { - + + public static void main(String[] args){ launch(args); } + + + + /* (1) Attributes + ---------------------------------------------------------*/ + /* (1) Root elements */ private Stage root_stage; private Scene root_scene; private AnchorPane root_layout; + + /* (2) Local elements */ private FlowPane main_container; private Article articles; - + + + /* (2) Builds the stage, scene, ... + * + * @primary_stage The primary stage to create in + * + ---------------------------------------------------------*/ @Override public void start(Stage primary_stage) throws Exception { - + + /* (1) Init. stage, scene and context + ---------------------------------------------------------*/ /* (1) store primary stage + title it */ this.root_stage = primary_stage; this.root_stage.setTitle("Inifiny Mail Client"); - + /* (2) Load the root layout*/ - this.loadRootLayout(); - + this.loadRootLayout(); + /* (3) Load the CSS CONTEXT */ ContextBuilder.createContext(); - + /* (3) Store container */ this.main_container = (FlowPane) this.root_scene.lookup("#container"); + + + /* (2) Manage static stylesheet + ---------------------------------------------------------*/ + /* (1) #header */ + new HeaderStyleSheet( this.root_scene.lookup("#header") ); + + /* (2) #menu_container */ + new MenuStyleSheet( this.root_scene.lookup("#menu") ); + + /* (3) #submenu */ + new SubMenuStyleSheet( this.root_scene.lookup("#submenu") ); + + /* (4) #header_icon*/ + new HeaderIconStyleSheet( this.root_scene.lookup("#header_icon") ); + - - - /* (1) Create controllers' views - -------------------------------------*/ - /* (1) Create header menu */ + + /* (3) Manage controllers + ---------------------------------------------------------*/ + /* (1) Create HeaderMenu */ HeaderMenu hm = new HeaderMenu((FlowPane) this.root_scene.lookup("#header_menu"),this); - + hm.addItem("notification", "/src/header-notif.png"); hm.addItem("mail", "/src/header-mail.png"); hm.addItem("search", "/src/header-search.png"); hm.addItem("menu", "/src/header-menu.png"); - - - /* (2) Create container */ - this.articles = new Article(this.main_container, this); - - - /* (2) CSS - -------------------------------------*/ - /* (1) #header */ - new HeaderStyleSheet( this.root_scene.lookup("#header") ); - - /* (2) #menu_container */ - new MenuStyleSheet( this.root_scene.lookup("#menu") ); - - /* (3) #submenu */ - new SubMenuStyleSheet( this.root_scene.lookup("#submenu") ); - - /* (4) #header_icon*/ - new HeaderIconStyleSheet( this.root_scene.lookup("#header_icon") ); - + /* (2) Init. articles controller */ + this.articles = new Article(this.main_container, this); + + + } - + + + + /* (3) Loads the root layout and init. the scene-stage-pane + * + * + ---------------------------------------------------------*/ public void loadRootLayout(){ - + try{ - + /* (1) Load the root_disp.fxml */ FXMLLoader loader = new FXMLLoader(); - + loader.setLocation(getClass().getResource("/fxml/model.fxml")); - + /* (2) Load the layout into the scene */ this.root_layout = (AnchorPane) loader.load(); this.root_scene = new Scene(this.root_layout); - + /* (3) Add the scene to the stage */ this.root_stage.setScene(this.root_scene); - + /* (4) Show the stage */ this.root_stage.show(); - + }catch(IOException e){ e.printStackTrace(); } - + } - - public static void main(String[] args) { - launch(args); - } + + /* (4) Event dispatcher + * + * @e observed event received + * + ---------------------------------------------------------*/ @Override public void handleEvent(Event e) { /* HashMap headers = new HashMap(); headers.put("Referer", "http://www.wordreference.com"); - + ApiCall call = new ApiCall("http://api.wordreference.com/1/json/enfr/grin","GET",new Callback() { @Override public void onSuccess(JSONObject response) { System.out.println(response.toString()); - + } @Override public void onError() { System.out.println("APICall error"); - + } - + }); call.addHeaders(headers); call.send();*/ - - switch(e.getEventType()){ - + + switch( e.getEventType() ){ + + /* (1) On HeaderMenu.item.click -> search sources */ case "changeMainLayout": this.handleMainLayoutChange(e.getObjectId()); break; - + + /* (2) articles.query.success -> display articles */ case "NewsQuerySuccess": - System.out.println(NewsListModel.getInstance().getNews().size()+" News ont été trouvé"); - - // For each news + for( NewsModel news : NewsListModel.getInstance().getNews() ){ - + try{ - this.articles.addItem( news ); - }catch(Exception e1){ - System.out.println("Cannot fetch article data"); - e1.printStackTrace(); - } - + } - if( NewsListModel.getInstance().getNews().size() != 0 ) - System.out.println("La description du premier article est: "+NewsListModel.getInstance().getNews().get(0).getDescription()); - break; - + + /* (3) articles.query.error -> display error ('no result') */ case "NewsQueryFailed": System.out.println("une erreur est survenue"); break; } - + } - + public void handleMainLayoutChange(String layout) { + NewsListModel.getInstance().addObserver("MainClass", this); NewsListModel.getInstance().setCategory(Category.business); NewsListModel.getInstance().setSortType(SortTypes.publishedAt); NewsListModel.getInstance().query("bitcoin"); - + } } diff --git a/fxml/article_disp.fxml b/fxml/article_disp.fxml index d8a01d0..d19157d 100644 --- a/fxml/article_disp.fxml +++ b/fxml/article_disp.fxml @@ -10,7 +10,7 @@ - + diff --git a/fxml/model.fxml b/fxml/model.fxml index 695c627..1ab7bae 100644 --- a/fxml/model.fxml +++ b/fxml/model.fxml @@ -1,12 +1,11 @@ - + - @@ -54,63 +53,14 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + diff --git a/model/LangModel.java b/model/LangModel.java index ca7ba98..0f08432 100644 --- a/model/LangModel.java +++ b/model/LangModel.java @@ -3,37 +3,35 @@ package model; import Classes.Languages; public class LangModel { - + private static LangModel instance; - + private Languages fromLang = Languages.fr; - private Languages toLang = Languages.en; - - - private LangModel() { - } - - public static LangModel getInstance() { - if(LangModel.instance == null) { + private Languages toLang = Languages.en; + + + private LangModel(){} + + public static LangModel getInstance(){ + if( LangModel.instance == null ) LangModel.instance = new LangModel(); - } - + return LangModel.instance; } - - public void setFromLang(Languages lang) { + + public void setFromLang(Languages lang){ this.fromLang = lang; } - - public Languages getFromLang() { + + public Languages getFromLang(){ return this.fromLang; } - - public void setToLang(Languages lang) { + + public void setToLang(Languages lang){ this.toLang = lang; } - - public Languages getToLang() { + + public Languages getToLang(){ return this.toLang; } diff --git a/model/NewsListModel.java b/model/NewsListModel.java index 5e3aa39..b88ab97 100644 --- a/model/NewsListModel.java +++ b/model/NewsListModel.java @@ -6,7 +6,6 @@ import java.util.Date; import java.util.HashMap; import org.json.JSONArray; -import org.json.JSONException; import org.json.JSONObject; import Classes.ApiCall; @@ -74,7 +73,7 @@ public class NewsListModel implements Observable{ String lang = LangModel.getInstance().getToLang().name(); String URL = "http://beta.newsapi.org/v2/sources?language="+lang; - //on rajoute la catégorie + //on rajoute la catégorie if(cat != Category.all) { URL += "&category="+cat.name(); } @@ -100,7 +99,7 @@ public class NewsListModel implements Observable{ } @Override - public void onError() { + 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) NewsListModel.this.isRetreivingSources = false; NewsListModel.this.apiError = true; @@ -127,7 +126,7 @@ public class NewsListModel implements Observable{ //on créer l'URL de l'appel String lang = LangModel.getInstance().getToLang().name(); String URL = "http://beta.newsapi.org/v2/everything?apiKey="+this.APIKey+"&language="+lang+"&q="+q+"&sortBy="+this.sortType.name(); - + //on ajoute la liste des sources a l'URL de la requete if(NewsListModel.this.sources != null && NewsListModel.this.sources.size() != 0) { String sources = "&sources="; @@ -149,7 +148,7 @@ public class NewsListModel implements Observable{ for(int i = 0;i getNews(){ return this.news; } @@ -247,7 +246,7 @@ public class NewsListModel implements Observable{ @Override public void removeObserver(String key) { this.observers.remove(key); - + }