4

im trying to connect mysql in android app. Below is my code. Once i run the code i get 'Error parsing data org.json.JSONException: End of input at character 0 of' error. I was using this tutorial

code

public class Test extends Activity{
    /** Called when the activity is first created. */
     @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            //setContentView(R.layout.mainabout);
            String result = "";
          //the year data to send
          ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
          nameValuePairs.add(new BasicNameValuePair("year","1980"));
          InputStream is = null;               
          //http post
          try{
                  HttpClient httpclient = new DefaultHttpClient();

                  HttpGet httppost = new HttpGet("http://www.pherma.net84.net/admin/getAllPeopleBornAfter.php");
                 // httppost.s//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 = 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());
          }

          try
          {
              JSONArray jArray = new JSONArray(result);
              for(int i=0;i<jArray.length();i++){
                      JSONObject 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")
                      );
              }

          }
          catch(JSONException e){
              Log.e("log_tag", "Error parsing data "+e.toString());
          } 
        }
    }

logcat enter image description here

php

<?php
mysql_connect(".com","a4055820_root","");
mysql_select_db("a4055820_pherma");

$q=mysql_query("SELECT * FROM people WHERE birthyear>'".$_REQUEST['year']."'");
while($e=mysql_fetch_assoc($q))
        $output[]=$e;

print(json_encode($output));

mysql_close();
?>
chinna_82
  • 6,353
  • 17
  • 79
  • 134
  • Go through following links: 1. http://stackoverflow.com/questions/6996000/error-in-http-connection 2. http://stackoverflow.com/questions/8202048/org-json-json-exception-end-of-input-at-character-0 – Somnath Muluk May 23 '12 at 08:54

3 Answers3

2

The problem was that I forgot to give Internet access permission in the manifest.

mskfisher
  • 3,291
  • 4
  • 35
  • 48
chinna_82
  • 6,353
  • 17
  • 79
  • 134
1

The code seems to fail at the first try catch block meaning the rest wont work.

Are you sure that the right URL to the php file you created?

What does your php file look like?

  • From what you have given all I can assume is that the connection isnt working. Are you sure the php file is located here: "http://www.pherma.net84.net/admin/getAllPeopleBornAfter.php". Also there are values in the database? – James Michael Lucas May 21 '12 at 23:14
  • Actually just noticed this line of code, why is it commented it out, with this commented out it might fail. // httppost.s//setEntity(new UrlEncodedFormEntity(nameValuePairs)); – James Michael Lucas May 21 '12 at 23:20