title: Android App connecting to JSP page
body: <p>I am trying to connect my Android App to a JSP page, to get  data from a database on the same server. My web page is working fine, here you can see the code:</p>
  
  <p>associazioni.jsp</p>
  
  <pre><code>    &lt;%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%&gt;
      &lt;%@ page import="java.sql.Connection" %&gt;
      &lt;%@ page import="java.sql.DriverManager" %&gt;
      &lt;%@ page import="java.sql.ResultSet" %&gt;
      &lt;%@ page import="java.sql.ResultSetMetaData" %&gt;
      &lt;%@ page import="java.sql.SQLException" %&gt;
      &lt;%@ page import="java.sql.Statement" %&gt;
  &lt;%
  try {
  
      Class.forName("com.mysql.jdbc.Driver").newInstance();
      // crea un collegamento con il database "csv"
      Connection conn = DriverManager.getConnection("jdbc:mysql://localhost/csv", "username", "password");
      Statement stat = conn.createStatement();
      // esegue la query di selezione
      ResultSet rs = stat.executeQuery("SELECT * FROM associazioni");
  
      // scrive che tutto è andato a buon fine
      out.println("OK");
  
      // scrive i dati
      while (rs.next()) {
          // id_associazione
          out.println(rs.getString(1));
          // nome
          out.println(rs.getString(2));
          // indirizzo
          out.println(rs.getString(3));
          // telefono
          out.println(rs.getString(4));
          // contatti
          out.println(rs.getString(5));
          // descrizione
          out.println(rs.getString(6));
      }
  
      // scrive che il processo è concluso
      out.println("END");
  
      // chiude la connessione
      stat.close();
      conn.close();
  
  // cattura le eccezioni
  } catch (Exception e) {
      // scrive il tipo di eccezione
      out.println(e.toString());
  }
  %&gt;
  </code></pre>
  
  <p>As you can prove yourself, if you go to:
  <a href="http://liceocuneoweb2.linuxd.org:8080/CSV/associazioni.jsp" rel="nofollow">http://liceocuneoweb2.linuxd.org:8080/CSV/associazioni.jsp</a>
  the web page is working, writing "OK" at the beginning and "END" at the end of the file.
  The problems come when I try to access it from the application, I write you down my code, if you can suggest me better ways to write it, or just solve the problem, I will be very grateful!</p>
  
  <p>MainActivity.java</p>
  
  <pre><code>public class MainActivity extends Activity {
  
      private TextView resultsTV;
      private BufferedReader reader;
      private String result;
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);
          resultsTV = (TextView) findViewById(R.id.results);
          new RetrieveResults().execute();
      }
  
      @Override
      public boolean onCreateOptionsMenu(Menu menu) {
          // Inflate the menu; this adds items to the action bar if it is present.
          getMenuInflater().inflate(R.menu.menu_main, menu);
          return true;
      }
  
      @Override
      public boolean onOptionsItemSelected(MenuItem item) {
          // Handle action bar item clicks here. The action bar will
          // automatically handle clicks on the Home/Up button, so long
          // as you specify a parent activity in AndroidManifest.xml.
          int id = item.getItemId();
  
          //noinspection SimplifiableIfStatement
          if (id == R.id.action_settings) {
              return true;
          }
  
          return super.onOptionsItemSelected(item);
      }
  
      private void leggi() {
          try {
              ArrayList&lt;String&gt; pagina = new ArrayList&lt;String&gt;();
              while (true) {
                  String r = this.reader.readLine();
                  if (r.equals("END")) {
                      break;
                  } else {
                      pagina.add(r);
                  }
              }
              scriviResults(pagina);
          } catch (Exception e) {
              resultsTV.setText(e.toString());
          }
      }
  
      private void scriviResults(ArrayList&lt;String&gt; pagina) {
          for (int i=0; i&lt;pagina.size(); i++) {
              resultsTV.append(pagina.get(i) + "\n");
          }
      }
  
      private class RetrieveResults extends AsyncTask {
          @Override
          protected Object doInBackground(Object[] params) {
              collegati();
              return null;
          }
  
          @Override
          protected void onPostExecute(Object o) {
              if (result.equals("OK")) {
                  leggi();
              } else {
                  resultsTV.setText("Errore");
              }
          }
      }
  
      private void collegati() {
          try {
              String link = "http://liceocuneoweb2.linuxd.org:8080/CSV/associazioni.jsp";
              URL url = new URL(link);
              URLConnection conn = url.openConnection();
              reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
              result = reader.readLine();
          } catch (Exception e) {
              resultsTV.setText(e.toString());
          }
      }
  }
  </code></pre>
  
  <p>This program is not working as I wish, it should connect to the server and start reading the lines, if the first String is "OK" then it reads the other untill "END" and put the data in a ArrayList. At the end of the program it prints the Array out via TextView.
  Sometimes it gives me "Errore", some other times it cannot connect to the server, it never give back the whole table of the database. I do not know what to try, the code seems correct to me.
  Thanks for your time and sorry for my bad english!</p>