title: Connect Android App to MySQL db through PHP/JSON
body: <p>I have a MySQL database with one unique table: <strong>geopoints</strong>, that has 3 columns: <strong>id</strong>, <strong>lat</strong> and <strong>long</strong>, and this table is filled up with 4 entrys already, 4 values of lat and long (1E6 because its to use on a mapview).</p>
  
  <p>My objective is to mark on a mapview all these 4 geopoints dynamicly using a php script that generates json array, and with a httpPost etc...</p>
  
  <p>This is the code I have so far:</p>
  
  <pre><code>getjson = (Button) findViewById(R.id.button1);
  
      getjson.setOnClickListener(new View.OnClickListener() {
  
          public void onClick(View v) {
  
              JSONArray jArray;
              String result = null;
              InputStream is = null;
              StringBuilder sb = null;
              ArrayList&lt;NameValuePair&gt; nameValuePairs = new ArrayList&lt;NameValuePair&gt;();//Whats this for?
  
              try {
  
                  HttpClient httpclient = new DefaultHttpClient();
                  HttpPost httpPost = new HttpPost(
                          "http://????????.com/????/getGeopoints.php");
                  HttpResponse response = httpclient.execute(httpPost);
                  httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
                  HttpEntity entity = response.getEntity();
                  is = entity.getContent();
  
              } catch (Exception e) {
                  Log.e("log_tag", "Error connecting to http " + e.toString());
              }
  
              try {
                  BufferedReader reader = new BufferedReader(
                          new InputStreamReader(is, "iso-8859-1"), 8);
                  sb = new StringBuilder();
                  String line = null;
                  while ((line = reader.readLine()) != null) {
                      sb.append(line + "\n");
                  }
                  is.close();
  
                  result = sb.toString();
              } catch (Exception e) {
                  Log.e("log_tag", "Error converting  " + e.toString());
              }
  
              try {
  
                  jArray = new JSONArray(result);
  
                  for (int i = 0; i &lt;= jArray.length(); i++) {
  
                      JSONObject json_data = jArray.getJSONObject(i);
  
                      Log.i("log_tag", "id: " + json_data.getInt("id")
                              + ", latitude: " + json_data.getInt("lat")
                              + ", longitude: " + json_data.getInt("long"));
  
  
                  }
              } catch (JSONException e) {
                  Log.e("log_tag", "Error parsing data " + e.toString());
              }
          }
      });
  </code></pre>
  
  <p>This is the php file:</p>
  
  <pre><code> &lt;?php
  mysql_connect("?????.com", "username", "password");
  mysql_select_db("database_name");
  
  $q = mysql_query("SELECT * FROM geopoints");
  while ($e = mysql_fetch_assoc($q))
      $output[] = $e;
  
  print(json_encode($output));
  mysql_close();
  ?&gt;
  </code></pre>
  
  <p>I have this file running right because as I enter the url on a browser it generates the exact json array with the exact information I want.
  There is only think I don't understand, what's HttpEntity for? Because I followed up a tutorial, and in there it uses an array with information to filter from the db, as I don't want  to filter any info (I want all the database table entrys, not just one or two) I just leaved it blank.</p>
  
  <p>What's wrong with my code?
  The error I get in logcat is all the catch messages "11-29 12:47:29.662: E/log_tag(898): Error connecting to http Http android.os.NetworkOnMainThreadException".</p>