title: Android connectivity with MySQL
body: <p>I am developing a final year project where I need to connect Android emulator with MySQL database in order to retrieve values. Java file:</p>
  
  <pre><code>public class connectivity extends Activity {
      /** Called when the activity is first created. */
      TextView txt;
      public static final String KEY_121 = "http://10.0.2.2/mysqlcon.php";
  
      @Override
      public void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.main);
          LinearLayout rootLayout = new LinearLayout(getApplicationContext());
          txt = new TextView(getApplicationContext());
          rootLayout.addView(txt);
          setContentView(rootLayout);
          // Set the text and call the connect function.
          txt.setText("Connecting...");
          // call the method to run the data retreival
          txt.setText(getServerData(KEY_121));
      }
  
      private String getServerData(String returnString) {
          InputStream is = null;
          String result = null;
          // the year data to send
          ArrayList&lt;NameValuePair&gt; nameValuePairs = new ArrayList&lt;NameValuePair&gt;();
          nameValuePairs.add(new BasicNameValuePair("year", "1970"));
          // http post
          try {
              HttpClient httpclient = new DefaultHttpClient();
              HttpPost httppost = new HttpPost(KEY_121);
              httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
              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 = "0";
              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);
              JSONObject json_data = null;
              for (int i = 0; i &lt; jArray.length(); i++) {
                  json_data = jArray.getJSONObject(i);
                  Log.i("log_tag", "id: " + json_data.getInt("id") + ", name: " + json_data.getString("name") + ", sex: " + json_data.getInt("sex") + ", birthyear: " + json_data.getInt("birthyear"));
                  // 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>I have also given an internet permission in my Android manifest file. But after running the application I get the following error in logcat:</p>
  
  <blockquote>
    <p>ERROR PARSING DATA org.json.JSONException:A JSONArraytext must start with '[' at character 0</p>
  </blockquote>
  
  <p>I think this goes to show that a null value is being returned. Please help me out as this is my final year project. I have spent hours trying to find the solutions but it has been of no use.</p>
  
  <p>I am currently using Android 2.2. The wamp server is on the localhost so I am using the address 10.0.2.2 which is a special alias to localhost (127.0.0.1). Any help will be really appreciated.</p>
  
  <p>Here is the PHP code:</p>
  
  <pre><code>&lt;?php
  mysql_connect("127.0.0.1","root","chetan");
  mysql_select_db("db1");
  $q=mysql_query("SELECT * FROM people WHERE birthyear&gt;'".$_REQUEST['year']."'");
  while($e=mysql_fetch_assoc($q))
  $output[]=$e;
  print(json_encode($output,JSON_FORCE_OBJECT));
  mysql_close();
  </code></pre>