title: connecting android to mysql database
body: <p>I have been trying this tutorial. It doent show any errors but on emulator only the url that i gave is appearing!</p>
  
  <p>my connection string was:</p>
  
  <pre><code>public static final String KEY_121 = "http://10.0.2.2/index.php";
  </code></pre>
  
  <p>(i used 10.0.2.2 as i am working on local host)</p>
  
  <p>when i checked my logcat it showed me dis:</p>
  
  <pre><code>error parsing data org.json.JsonException: Value &lt;br of type java.lang.String cannot be converted to JSONArray
  </code></pre>
  
  <p>I really dont know whats wrong with my code. Can anyone please help me. I really need help on this for my final year project.</p>
  
  <p>This is my android java code</p>
  
  <pre><code>import java.io.BufferedReader;
  import java.io.InputStream;
  import java.io.InputStreamReader;
  import java.util.ArrayList;
  
  import org.apache.http.HttpEntity;
  import org.apache.http.HttpResponse;
  import org.apache.http.NameValuePair;
  import org.apache.http.client.HttpClient;
  import org.apache.http.client.methods.HttpPost;
  import org.apache.http.impl.client.DefaultHttpClient;
  import org.apache.http.message.BasicNameValuePair;
  import org.json.JSONArray;
  import org.json.JSONException;
  import org.json.JSONObject;
  
  import android.app.Activity;
  import android.os.Bundle;
  import android.util.Log;
  import android.widget.LinearLayout;
  import android.widget.TextView;
  
  public class main extends Activity {
      /** Called when the activity is first created. */
  
      TextView txt1, txt2;
  
      @Override
      public void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.main);
          // Create a crude view - this should really be set via the layout
          // resources
          // but since its an example saves declaring them in the XML.
          txt1 = (TextView) findViewById(R.id.ip);
  
          LinearLayout rootLayout = new LinearLayout(getApplicationContext());
          txt1 = new TextView(getApplicationContext());
          rootLayout.addView(txt1);
          setContentView(rootLayout);
  
          // Set the text and call the connect function.
          txt1.setText("Connecting...");
          // call the method to run the data retreival
          txt1.setText(getServerData(KEY_121));
  
      }
  
      public static final String KEY_121 = "http://10.0.2.2/index.php";
  
      private String getServerData(String returnString) {
  
          InputStream is = null;
  
          String result = "";
          // the year data to send
          ArrayList&lt;NameValuePair&gt; nameValuePairs = new ArrayList&lt;NameValuePair&gt;();
          nameValuePairs.add(new BasicNameValuePair("year", "1970"));
  
  
          try {
              HttpClient httpclient = new DefaultHttpClient();
              HttpPost httppost = new HttpPost(KEY_121);
              HttpResponse response = httpclient.execute(httppost);
              HttpEntity entity = response.getEntity();
              is = entity.getContent();
  
          } catch (Exception e) {
              Log.e("log_tag", "Error in http connection " + e.toString());
          }
  
          // convert response to string
          try {
              BufferedReader reader = new BufferedReader(new InputStreamReader(
                      is, "iso-8859-1"), 8);
              StringBuilder 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 result " + e.toString());
          }
          // parse json data
  
          try {
              JSONArray jArray = new JSONArray(result);
              for (int i = 0; i &lt; jArray.length(); i++) {
                  JSONObject json_data = jArray.getJSONObject(i);
                  Log.i("log_tag",
                          "longitude: " + json_data.getDouble("longitude")
                                  + ", latitude: "
                                  + json_data.getDouble("latitude"));
                  // Get an output to the screen
                  returnString += "\n\t" + jArray.getJSONObject(i);
              }
          } catch (JSONException e) {
              Log.e("log_tag", "Error parsing data " + e.toString());
          }
  
          return returnString;
      }
  
  }
  </code></pre>
  
  <p>This is my php code</p>
  
  <pre><code>&lt;?php
  
  mysql_connect("localhost","root","sugi");
  mysql_select_db("android");
  
  $q=mysql_query("SELECT * FROM people WHERE birthyear&gt;'".$_REQUEST['year']."'");
  
  while($e=mysql_fetch_assoc($q))
          $output[]=$e;
  
  print(json_encode($output));
  
  mysql_close();
  ?&gt;
  </code></pre>
  
  <p>This is mysql query</p>
  
  <pre><code>CREATE TABLE people (
  
    id INT NOT NULL AUTO_INCREMENT PRIMARY KEY ,
  
    name VARCHAR( 100 ) NOT NULL ,
  
    sex BOOL NOT NULL DEFAULT '1',
  
    birthyear INT NOT NULL
  
    )
  </code></pre>
  
  <p>This is what its display when i type "<a href="http://10.0.2.2/index.php" rel="nofollow noreferrer">http://10.0.2.2/index.php</a>" on IE</p>
  
  <p><img src="https://i.stack.imgur.com/S49cb.png" alt="enter image description here"></p>