--- a/app/src/main/java/com/codesys/forge/MyService.java +++ b/app/src/main/java/com/codesys/forge/MyService.java @@ -99,61 +99,52 @@ notificationManager.notify(id, builder.build()); } protected void CheckForUpdatesAndNotify() { - new AsyncTask<Void, Void, String>() { - @Override - protected String doInBackground(Void... voids) { - String urlString = "https://forge.codesys.com/rest/forge/saml"; - try { - URL url = new URL(urlString); - HttpURLConnection conn = (HttpURLConnection) url.openConnection(); - conn.setReadTimeout(10000 /* milliseconds */); - conn.setConnectTimeout(15000 /* milliseconds */); - conn.setRequestMethod("GET"); - conn.setDoInput(true); - if (cookies != null) - conn.setRequestProperty("Cookie", cookies); - conn.connect(); - BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream())); - StringBuilder stringBuilder = new StringBuilder(); + String urlString = "https://forge.codesys.com/rest/forge/saml"; + try { + URL url = new URL(urlString); + HttpURLConnection conn = (HttpURLConnection) url.openConnection(); + conn.setReadTimeout(10000 /* milliseconds */); + conn.setConnectTimeout(15000 /* milliseconds */); + conn.setRequestMethod("GET"); + conn.setDoInput(true); + if (cookies != null) + conn.setRequestProperty("Cookie", cookies); + conn.connect(); + BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream())); + StringBuilder stringBuilder = new StringBuilder(); - String line = null; - while ((line = reader.readLine()) != null) - { - stringBuilder.append(line + "\n"); + String line = null; + while ((line = reader.readLine()) != null) + { + stringBuilder.append(line + "\n"); + } + String news = stringBuilder.toString(); + JSONObject obj = new JSONObject(news); + for (int i=0; i < obj.getJSONArray("news").length(); i++) + { + String uri = obj.getJSONArray("news").getString(i); + switch(uri) { + case "/forge/community-feed/": + CreateNotification(1); + break; + case "/forge/news/": + CreateNotification(2); + break; + case "/forge/talk/": + CreateNotification(3); + break; } - String news = stringBuilder.toString(); - JSONObject obj = new JSONObject(news); - for (int i=0; i < obj.getJSONArray("news").length(); i++) - { - String uri = obj.getJSONArray("news").getString(i); - switch(uri) { - case "/forge/community-feed/": - CreateNotification(1); - break; - case "/forge/news/": - CreateNotification(2); - break; - case "/forge/talk/": - CreateNotification(3); - break; - } - } - return news; } - catch(MalformedURLException e) { - Log.e("get news", e.getLocalizedMessage()); - } - catch(IOException e) { - Log.e("get news", e.getLocalizedMessage()); - } - catch(JSONException e) { - Log.e("get news", e.getLocalizedMessage()); - } - return "error"; } - - }.execute(); - + catch(MalformedURLException e) { + Log.e("get news", e.getLocalizedMessage()); + } + catch(IOException e) { + Log.e("get news", e.getLocalizedMessage()); + } + catch(JSONException e) { + Log.e("get news", e.getLocalizedMessage()); + } } @Override public void onCreate() { @@ -177,7 +168,9 @@ @Override public int onStartCommand(Intent intent, int flags, int startId) { mRunning = true; - cookies = intent.getStringExtra("cookies"); + if (intent != null) { + cookies = intent.getStringExtra("cookies"); + } return super.onStartCommand(intent, flags, startId); }