Example #1
0
      @Override
      protected String doInBackground(Void... args) {
        ArrayList<Gasolinera> algOrder = null;
        BdGas bdgas = new BdGas(mContext);

        algOrder = bdgas.readBdGas(order);
        String json = utility.getAllRestJSONGasolinera(algOrder);
        if (json.equals(EMPTY_GSOLIN_LIST)) {
          ((Activity) mContext)
              .runOnUiThread(
                  new Runnable() {
                    @Override
                    public void run() {
                      webview.loadUrl(
                          JAVASCRIPT + "showToast" + BRC_OPEN + "Sin Resultados" + BRC_CLOSE);
                    }
                  });

          // webview.loadUrl(HTML_ROOT + "indexGas.html");
        }

        return json;
      }
Example #2
0
      @Override
      protected String doInBackground(Void... args) {
        String prov = datos.getProvincia();
        String mun = datos.getMunicipio();
        String direc = datos.getDireccion();
        String num = datos.getNum();
        String cp = datos.getCp();
        String comb = datos.getCombustible();
        ;

        ArrayList<Gasolinera> alg = new ArrayList<Gasolinera>();
        if (prov.equals("") && !cp.equals("")) prov = cp.substring(0, 2);

        try {
          // final String Newurl = "http://geoportal.mityc.es/hidrocarburos/eess/searchAddress.do";
          final String Newurl = "http://www.geoportalgasolineras.es/searchAddress.do?";
          HttpClient httpclient = new DefaultHttpClient();
          /*Creamos el objeto de HttpClient que nos permitira conectarnos mediante peticiones http*/
          HttpPost httppost = new HttpPost(Newurl);
          //  httppost.addHeader("Content-Type", "application/x-www-form-urlencoded;
          // charset=UTF-8");
          // httppost.addHeader( "X-Requested-With", "XMLHttpRequest");
          httppost.addHeader("Accept-Language", "es");

          /*El objeto HttpPost permite que enviemos una peticion de tipo POST a una URL especificada*/
          // AÑADIR PARAMETROS
          // http://geoportal.mityc.es/hidrocarburos/eess/searchAddress.do?nomProvincia=28&nomMunicipio=mostoles&tipoCarburante=4&rotulo=&tipoVenta=false&nombreVia=&numVia=&codPostal=&economicas=false&tipoBusqueda=0&ordenacion=P&posicion=0
          List<NameValuePair> params = new ArrayList<NameValuePair>();
          params.add(new BasicNameValuePair("codPostal", cp));
          params.add(new BasicNameValuePair("economicas", "false"));
          params.add(new BasicNameValuePair("nomMunicipio", mun));
          params.add(new BasicNameValuePair("nomProvincia", prov));
          params.add(new BasicNameValuePair("nombreVia", direc));
          params.add(new BasicNameValuePair("numVia", num));
          params.add(new BasicNameValuePair("ordenacion", "P"));
          params.add(new BasicNameValuePair("rotulo", ""));
          params.add(new BasicNameValuePair("tipoBusqueda", "0"));
          params.add(new BasicNameValuePair("tipoCarburante", comb));
          params.add(new BasicNameValuePair("tipoVenta", "true"));
          params.add(new BasicNameValuePair("posicion", "0"));

          /*Una vez añadidos los parametros actualizamos la entidad de httppost, esto quiere decir en pocas palabras anexamos los parametros al objeto para que al enviarse al servidor envien los datos que hemos añadido*/
          httppost.setEntity(new UrlEncodedFormEntity(params));

          /*Finalmente ejecutamos enviando la info al server*/
          HttpResponse resp = httpclient.execute(httppost);

          InputStream in = resp.getEntity().getContent();

          brNew = new BufferedReader(new InputStreamReader(in));
          String text;
          String rdo = "";
          String nombre = "-";
          String direccion = "-";
          String localidad = "-";
          String provincia = "-";
          String gasolina95 = "-";
          String gasolina98 = "-";
          String gasoleo = "-";
          @SuppressWarnings("unused")
          String margen = "-";
          String fecha = "-";
          @SuppressWarnings("unused")
          String venta = "-";
          @SuppressWarnings("unused")
          String rem = "-";
          String horario = "-";
          String UPV = "-";
          String combustible;
          //	while ((text = brNew.readLine()).trim() != null) {
          while ((text = brNew.readLine()) != null) {

            rdo += text.trim();
            if ((rdo.indexOf("</tbody>") != -1)
                || (rdo.indexOf("No se han encontrado concordancias") != -1)) break;
            Log.i("INFO", text);
          }
          // brNew.close();
          int pos = 0;

          // pos = rdo.indexOf("<td class=\"tdMediumBorderLeftTable\">")+("<td
          // class=\"tdMediumBorderLeftTable\">").length(); <tbody>

          pos = rdo.indexOf("<td valign=\"middle\">");
          int pos2;
          while (pos != -1) { // añadido 13/09/2013
            pos = pos + ("<td valign=\"middle\">").length();
            pos2 = rdo.indexOf("</td>", pos + 4);
            provincia = (rdo.substring(pos, pos2));
            provincia = provincia.replaceAll("\"", "").replaceAll("\'", "");

            rdo = rdo.substring(pos2, rdo.length());
            pos = rdo.indexOf("<td valign=\"middle\">", 5);
            pos = rdo.indexOf(">", pos + 5) + 1;
            pos2 = rdo.indexOf("</td>", pos + 4);
            localidad = rdo.substring(pos, pos2);
            localidad = localidad.replaceAll("\"", "").replaceAll("\'", "");

            rdo = rdo.substring(pos2, rdo.length());
            pos = rdo.indexOf("<td valign=\"middle\">", 5);
            pos2 = rdo.indexOf("</td>", pos + 4);
            direccion = rdo.substring(pos + ("<td valign=\"middle\">").length(), pos2);
            direccion = direccion.replaceAll("\"", "").replaceAll("\'", "");

            rdo = rdo.substring(pos2, rdo.length());
            pos = rdo.indexOf("<td valign=\"middle\">", 5);
            pos2 = rdo.indexOf("</td>", pos + 4);
            margen = rdo.substring(pos + ("<td valign=\"middle\">").length(), pos2);

            rdo = rdo.substring(pos2, rdo.length());
            pos = rdo.indexOf("<td valign=\"middle\">", 5);
            pos2 = rdo.indexOf("</td>", pos + 4);
            fecha = rdo.substring(pos + ("<td valign=\"middle\">").length(), pos2);

            rdo = rdo.substring(pos2, rdo.length());
            pos = rdo.indexOf("<td valign=\"middle\">", 5);
            pos2 = rdo.indexOf("</td>", pos + 4);
            combustible = rdo.substring(pos + ("<td valign=\"middle\">").length(), pos2);
            if (comb.equals("1")) gasolina95 = combustible;
            if (comb.equals("3")) gasolina98 = combustible;
            if (comb.equals("4")) gasoleo = combustible;

            rdo = rdo.substring(pos2, rdo.length());
            pos = rdo.indexOf("<td valign=\"middle\">", 2);
            pos2 = rdo.indexOf("</td>", pos + 4);
            nombre = rdo.substring(pos + ("<td valign=\"middle\">").length(), pos2);
            nombre = nombre.replaceAll("\"", "").replaceAll("\'", "");

            rdo = rdo.substring(pos2, rdo.length());
            pos = rdo.indexOf("<td valign=\"middle\">", 5);
            pos2 = rdo.indexOf("</td>", pos + 4);
            venta = rdo.substring(pos + ("<td valign=\"middle\">").length(), pos2);

            rdo = rdo.substring(pos2, rdo.length());
            pos = rdo.indexOf("<td valign=\"middle\">", 5);
            pos2 = rdo.indexOf("</td>", pos + 4);
            rem = rdo.substring(pos + ("<td valign=\"middle\">").length(), pos2);

            rdo = rdo.substring(pos2, rdo.length());
            pos = rdo.indexOf("<td valign=\"middle\">", 5);
            // pos = rdo.indexOf(">", pos + 5) + 1;
            pos2 = rdo.indexOf("</td>", pos + 4);
            horario = rdo.substring(pos + ("<td valign=\"middle\">").length(), pos2);

            rdo = rdo.substring(pos2, rdo.length());
            pos = rdo.indexOf("onClick=\"centrar", 6) + 1;
            // pos = rdo.indexOf(">", pos + 5) + 1;
            pos2 = rdo.indexOf("</img>", pos + 1) - 6;
            UPV = rdo.substring(pos + ("onClick=\"centrar").length(), pos2);

            Gasolinera g =
                new Gasolinera(
                    UPV.trim(),
                    fecha.trim(),
                    nombre.trim(),
                    direccion.trim(),
                    localidad.trim(),
                    provincia.trim(),
                    horario.trim(),
                    gasolina95.trim(),
                    gasolina98.trim(),
                    gasoleo.trim());
            alg.add(g);

            rdo = rdo.substring(pos2, rdo.length());
            pos = rdo.indexOf("<td valign=\"middle\">");
          }

        } catch (IndexOutOfBoundsException ex) {
          // ex.printStackTrace();

        } catch (final Exception ex) {
          excep = false;
          Log.i("utility", ex.getMessage());
          ((Activity) mContext)
              .runOnUiThread(
                  new Runnable() {
                    @Override
                    public void run() {
                      webview.loadUrl(
                          JAVASCRIPT + "showToast" + BRC_OPEN + ex.getMessage() + BRC_CLOSE);
                      webview.loadUrl(HTML_ROOT + "indexGas.html");
                    }
                  });
        }
        String json = EMPTY_GSOLIN_LIST;
        try {
          BdGas bdgas = new BdGas(mContext);
          bdgas.writerBdGas(alg);
          ArrayList<Gasolinera> algOrder = bdgas.readBdGas(null);
          json = getAllRestJSONGasolinera(algOrder);
          if (json.equals(EMPTY_GSOLIN_LIST) && excep) {
            ((Activity) mContext)
                .runOnUiThread(
                    new Runnable() {
                      @Override
                      public void run() {
                        webview.loadUrl(
                            JAVASCRIPT + "showToast" + BRC_OPEN + "Sin Resultados" + BRC_CLOSE);
                        webview.loadUrl(HTML_ROOT + "indexGas.html");
                      }
                    });

          } else return json;
        } catch (Exception e) {
          ((Activity) mContext)
              .runOnUiThread(
                  new Runnable() {
                    @Override
                    public void run() {
                      webview.loadUrl(
                          JAVASCRIPT
                              + "showToast"
                              + BRC_OPEN
                              + "Problema de conexion al Servidor"
                              + BRC_CLOSE);
                      webview.loadUrl(HTML_ROOT + "indexGas.html");
                    }
                  });
        }
        return json;
      }