From 22ce9c1c09b7ba6d613366aa52b1d6904a4c3518 Mon Sep 17 00:00:00 2001 From: Equip de Desenvolupadors de PBE Date: Sun, 9 Dec 2012 18:48:46 +0000 Subject: [PATCH] mejor gestion de urls invalidas y demas --- src/com/upc/pbe/upcnews/Descarrega.java | 10 ++-------- src/com/upc/pbe/upcnews/Directoris.java | 60 +++++++++++++++++++++++++++++++++++++++--------------------- src/com/upc/pbe/upcnews/MainActivity.java | 24 ++++++++++++++++++++---- src/com/upc/pbe/upcnews/Parser.java | 79 ++++++++++++++++++++++++++++++++++++++++++++++++------------------------------- 4 files changed, 109 insertions(+), 64 deletions(-) diff --git a/src/com/upc/pbe/upcnews/Descarrega.java b/src/com/upc/pbe/upcnews/Descarrega.java index c4d4d8d..1ffd999 100644 --- a/src/com/upc/pbe/upcnews/Descarrega.java +++ b/src/com/upc/pbe/upcnews/Descarrega.java @@ -74,10 +74,10 @@ public class Descarrega extends AsyncTask { } } - protected String doInBackground(String u) { + protected String doInBackground(String u) throws IOException { this.url = u; html = ""; - try { + URLConnection conn = new URL(url).openConnection(); BufferedReader in = new BufferedReader(new InputStreamReader( conn.getInputStream())); @@ -92,12 +92,6 @@ public class Descarrega extends AsyncTask { Log.d(TAG, "Descarrega finalitzada"); in.close(); - } catch (MalformedURLException e) { - Log.d(TAG, "excepcio 1, no sha trobat la URL"); - } catch (IOException e) { - Log.d(TAG, "excepcio 2"); - } - return html; } diff --git a/src/com/upc/pbe/upcnews/Directoris.java b/src/com/upc/pbe/upcnews/Directoris.java index f7423e3..ac3a96d 100644 --- a/src/com/upc/pbe/upcnews/Directoris.java +++ b/src/com/upc/pbe/upcnews/Directoris.java @@ -1,5 +1,6 @@ package com.upc.pbe.upcnews; +import java.io.IOException; import java.util.ArrayList; import android.app.Activity; @@ -67,8 +68,16 @@ public class Directoris extends Activity implements OnItemClickListener public void showResources() { - ArrayList entries = parser.parse(d.doInBackground(currentFolder)); - this.createEntries(entries); + ArrayList entries; + try + { + entries = parser.parse(d.doInBackground(currentFolder)); + this.createEntries(entries); + } + catch (IOException e) + { + // Nunca llegará aquí la cosa, y si lo hace ES CULPA DEL SERVIDOR!! + } } public void createEntries(ArrayList directories) @@ -86,29 +95,38 @@ public class Directoris extends Activity implements OnItemClickListener if(path.endsWith(".m3u8")) { path = currentFolder + path; - String playlist = d.doInBackground(path); - Parser p = new Parser(path.substring(0, path.lastIndexOf("/") + 1), this); + String playlist; try { - ArrayList m3u8parsed = p.parseFile(playlist); - Log.d(TAG, "parsing completed"); - HLS h = new HLS(m3u8parsed,((UpcApp)getApplication()).getLocalPath()); - h.start(); - } - catch (ErrorException e) - { - Toast.makeText(this, e.getMessage(), Toast.LENGTH_SHORT).show(); - Log.d(TAG, e.getMessage()); - } - catch (WarningException e) - { - Toast.makeText(this, e.getMessage(), Toast.LENGTH_SHORT).show(); - Log.d(TAG, e.getMessage()); + playlist = d.doInBackground(path); + + Parser p = new Parser(path.substring(0, path.lastIndexOf("/") + 1), this); + try + { + ArrayList m3u8parsed = p.parseFile(playlist); + Log.d(TAG, "parsing completed"); + HLS h = new HLS(m3u8parsed,((UpcApp)getApplication()).getLocalPath()); + h.start(); + } + catch (ErrorException e) + { + Toast.makeText(this, e.getMessage(), Toast.LENGTH_SHORT).show(); + Log.d(TAG, e.getMessage()); + } + catch (WarningException e) + { + Toast.makeText(this, e.getMessage(), Toast.LENGTH_SHORT).show(); + Log.d(TAG, e.getMessage()); + } + catch (InfoException e) + { + Toast.makeText(this, e.getMessage(), Toast.LENGTH_SHORT).show(); + Log.d(TAG, e.getMessage()); + } } - catch (InfoException e) + catch (IOException e1) { - Toast.makeText(this, e.getMessage(), Toast.LENGTH_SHORT).show(); - Log.d(TAG, e.getMessage()); + // Nunca llegará aquí la cosa, y si lo hace ES CULPA DEL SERVIDOR!! } } diff --git a/src/com/upc/pbe/upcnews/MainActivity.java b/src/com/upc/pbe/upcnews/MainActivity.java index 41440d0..fed2ff7 100644 --- a/src/com/upc/pbe/upcnews/MainActivity.java +++ b/src/com/upc/pbe/upcnews/MainActivity.java @@ -1,6 +1,8 @@ package com.upc.pbe.upcnews; import java.io.File; +import java.io.IOException; +import java.net.MalformedURLException; import android.app.Activity; import android.content.Intent; @@ -58,14 +60,28 @@ public class MainActivity extends Activity implements OnClickListener { public void onClick(View v) { Log.d(TAG, "onClicked"); this.descarregar(); - ((UpcApp) getApplication()).setDesc(html); - startActivity(new Intent(this, Directoris.class)); + if(!html.equals("EMPTY")) + { + ((UpcApp) getApplication()).setDesc(html); + startActivity(new Intent(this, Directoris.class)); + } } public void descarregar() { Descarrega d = new Descarrega(); - Toast.makeText(this, "Download succesfull", Toast.LENGTH_LONG).show(); - html = (String) d.doInBackground(((UpcApp)getApplication()).getUrl()); + html="EMPTY"; + try + { + html = (String) d.doInBackground(((UpcApp)getApplication()).getUrl()); + } + catch(MalformedURLException e) + { + Toast.makeText(this, "Invalid URL", Toast.LENGTH_LONG).show(); + } + catch(IOException e) + { + Toast.makeText(this, "Can't find URL", Toast.LENGTH_LONG).show(); + } } public boolean onOptionsItemSelected(MenuItem item) { diff --git a/src/com/upc/pbe/upcnews/Parser.java b/src/com/upc/pbe/upcnews/Parser.java index 4e5a32c..7f504b0 100644 --- a/src/com/upc/pbe/upcnews/Parser.java +++ b/src/com/upc/pbe/upcnews/Parser.java @@ -1,5 +1,6 @@ package com.upc.pbe.upcnews; +import java.io.IOException; import java.net.MalformedURLException; import java.util.ArrayList; @@ -420,41 +421,49 @@ public class Parser + " has missing arguments"); } Parser p = new Parser(URI.substring(0, URI.lastIndexOf("/") + 1), caller); - Video newList = p.parseFile(download.doInBackground(URI)).get(0).getLists().get(0); - if (!GroupID.equals(ppls.getID())) + Video newList; + try { - if (ppls.getID().equals("")) + newList = p.parseFile(download.doInBackground(URI)).get(0).getLists().get(0); + if (!GroupID.equals(ppls.getID())) { - /* - * If working with the default ParentList, - * then modify it, don't create a new one. - */ - ppls.setDefault(Default); - ppls.setName(Name); - ppls.setType(Type); - ppls.getLists().set(0, newList); - ppls.setID(GroupID); + if (ppls.getID().equals("")) + { + /* + * If working with the default ParentList, + * then modify it, don't create a new one. + */ + ppls.setDefault(Default); + ppls.setName(Name); + ppls.setType(Type); + ppls.getLists().set(0, newList); + ppls.setID(GroupID); + } + else + { + + currentList = searchID(GroupID, lists); + if (currentList == -1) + { + ParentList pl = new ParentList(GroupID); + pl.setDefault(Default); + pl.setName(Name); + pl.setType(Type); + lists.add(pl); + currentList = lists.size() - 1; + } + ppls = lists.get(currentList); + ppls.getLists().add(newList); + } } else { - - currentList = searchID(GroupID, lists); - if (currentList == -1) - { - ParentList pl = new ParentList(GroupID); - pl.setDefault(Default); - pl.setName(Name); - pl.setType(Type); - lists.add(pl); - currentList = lists.size() - 1; - } - ppls = lists.get(currentList); ppls.getLists().add(newList); } } - else + catch (IOException e) { - ppls.getLists().add(newList); + // Nunca llegará aquí la cosa, y si lo hace ES CULPA DEL SERVIDOR!! } } @@ -513,12 +522,20 @@ public class Parser */ expectList = false; Parser p = new Parser(line.substring(0, line.lastIndexOf("/") + 1), caller); - Video newList = p.parseFile(download.doInBackground(line)).get(0).getLists().get(0); - pls.setMaxDuration(newList.getMaxDuration()); - pls.setSequence(newList.getSequence()); - for (int i = 0; i < newList.getSegments().size(); i++) + Video newList; + try + { + newList = p.parseFile(download.doInBackground(line)).get(0).getLists().get(0); + pls.setMaxDuration(newList.getMaxDuration()); + pls.setSequence(newList.getSequence()); + for (int i = 0; i < newList.getSegments().size(); i++) + { + pls.getSegments().add(newList.getSegments().get(i)); + } + } + catch (IOException e) { - pls.getSegments().add(newList.getSegments().get(i)); + // Nunca llegará aquí la cosa, y si lo hace ES CULPA DEL SERVIDOR!! } } else -- libgit2 0.22.2