NelloRizzo

Load Cities From Web

Oct 20th, 2020 (edited)
287
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.25 KB | None | 0 0
  1. package corso.java.fiscalcodecalculator.persistence;
  2.  
  3. import android.util.Log;
  4.  
  5. import java.io.BufferedReader;
  6. import java.io.Closeable;
  7. import java.io.IOException;
  8. import java.io.InputStream;
  9. import java.io.InputStreamReader;
  10. import java.net.HttpURLConnection;
  11. import java.net.MalformedURLException;
  12. import java.net.URL;
  13. import java.net.URLConnection;
  14. import java.util.ArrayList;
  15. import java.util.List;
  16.  
  17. import corso.java.fiscalcodecalculator.model.CityModel;
  18.  
  19. public class CityLoader {
  20.  
  21.     //private final static  String istatPermalink = "https://www.istat.it/storage/codici-unita-amministrative/Elenco-comuni-italiani.csv";
  22.  
  23.     private final static int ID_FIELD = 4;
  24.     private final static int NAME_FIELD = 6;
  25.     private final static int AREA_FIELD = 9;
  26.     private final static int REGION_FIELD = 10;
  27.     private final static int PROVINCE_FIELD = 11;
  28.     private final static int CAPITAL_FIELD = 13;
  29.     private final static int ACRONYM_FIELD = 14;
  30.     private final static int FISCALCODE_FIELD = 19;
  31.  
  32.     public List<CityModel> load(InputStream s) {
  33.         List<CityModel> result = new ArrayList<>();
  34.         try {
  35. //            // 1. apro una connessione con il sito remoto
  36. //            // 1.1. creo un riferimento all'URL remoto
  37. //            URL url = new URL(istatPermalink);
  38. //            // 1.2. apro una connessione
  39. //            URLConnection conn = url.openConnection();
  40. //            // 1.3. accedo ad un canale di comunicazione con l'endpoint remoto
  41. //            //      mi interessa il canale di comunicazione che dal server arriva alla mia app
  42. //            //      devo accedere allo inputStream() che la connessione mi mette a disposizione
  43. //            //      un reader su uno stream traduce i bytes in qualcos'altro... per esempio stringhe
  44. //            // 2. leggo il file ottenuto byte a byte
  45. //            // sullo stream leggo solo bytes
  46. //            // uno streamreader trasforma i bytes in chars
  47.             InputStreamReader i = new InputStreamReader(s);
  48.             // un bufferedreader trasforma i chars in stringhe
  49.             // TRY WITH RESOURCE - utilizzabili con classi Closeable
  50. //            Closeable c = new Closeable() {
  51. //                @Override
  52. //                public void close() throws IOException {
  53. //                    
  54. //                }
  55. //            };
  56.             try (BufferedReader r = new BufferedReader(i)) { // BufferedReader fa da WRAPPER su InputStreamReader
  57.                 String nextLine = null;
  58.                 int headerLines = 3;
  59.                 while (headerLines-- > 0) r.readLine(); // leggo (e scarto) le righe di intestazione
  60.  
  61.                 while ((nextLine = r.readLine()) != null) { // questa è la PROSSIMA riga utile del file
  62.                     // devo scompormi la riga in tutti i campi che ottengo separando i ;
  63.                     // il metodo split() di una stringa separa la stringa secondo una regola di
  64.                     //           individuazione di un separatore e restituisce un array di String
  65.                     //           nel quale ci sono tutti i "pezzi" della stringa originaria!
  66.                     String[] fields = nextLine.split(";");
  67.                     long id = Long.parseLong(fields[ID_FIELD]);
  68.                     String name = fields[NAME_FIELD];
  69.                     String area = fields[AREA_FIELD];
  70.                     String region = fields[REGION_FIELD];
  71.                     String province = fields[PROVINCE_FIELD];
  72.                     boolean capital = fields[CAPITAL_FIELD].charAt(0) == '1';
  73.                     String acronym = fields[ACRONYM_FIELD];
  74.                     String fc = fields[FISCALCODE_FIELD];
  75.                     CityModel c = new CityModel.Builder()
  76.                             .withId(id).withAcronym(acronym).withArea(area).withFiscalCode(fc).withName(name)
  77.                             .withProvince(province).withRegion(region).isCapital(capital)
  78.                             .build();
  79.                     result.add(c);
  80.                 }
  81.             }
  82. //        } catch (MalformedURLException e) {
  83. //            e.printStackTrace();
  84.         } catch (IOException e) {
  85.             e.printStackTrace();
  86.         } catch (Exception e) {
  87.             e.printStackTrace();
  88.         }
  89.         return result;
  90.     }
  91. }
  92.  
Add Comment
Please, Sign In to add comment