Im a newbie to android, I am learning to connect to a server through android client using Php, MySql and JSON. For testing purpose im running on localhost. So for here's what I've done. Database demo.php

public class Database_demo extends ListActivity {

@Override
public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);

    String result = null;
    InputStream is = null;
    StringBuilder sb = null;
    ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
    List<String> r = new ArrayList<String>();

    try{

    //http post
    HttpClient httpclient = new DefaultHttpClient();
    HttpPost httppost = new HttpPost("http://10.0.2.2/PhpAndMySql/category.php");
    httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
    HttpResponse response = httpclient.execute(httppost);
    HttpEntity entity = response.getEntity();
    is = entity.getContent();
    }
    catch(Exception e){
        Toast.makeText(getBaseContext(),e.toString() ,Toast.LENGTH_LONG).show();
   }

    //Convert response to string  
    try
    {
      BufferedReader reader = new BufferedReader(new InputStreamReader(is,"UTF-8"));

      sb = new StringBuilder();

      String line = null;

      while ((line = reader.readLine()) != null) 
      {
         sb.append(line + "\n");
      }

      is.close();

      result = sb.toString();
    }
    catch(Exception e)
    {
        Toast.makeText(getBaseContext(),e.toString() ,Toast.LENGTH_LONG).show();
    }
    //END Convert response to string   
    try{
            JSONArray jArray = new JSONArray(result);
            JSONObject json_data=null;
            for(int i=0;i<jArray.length();i++)
            {
               json_data = jArray.getJSONObject(i);
               r.add(json_data.getString("category"));
           }
           setListAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_expandable_list_item_1, r));
        }
        catch(JSONException e1){
            Toast.makeText(getBaseContext(),e1.toString() ,Toast.LENGTH_LONG).show();
        } catch (ParseException e1) {
            Toast.makeText(getBaseContext(),e1.toString() ,Toast.LENGTH_LONG).show();
    }

}

}

category.php

<?php
mysql_connect("localhost","root","");
mysql_select_db("test");
$q=mysql_query("SELECT * FROM category ORDER BY 'category'.'category' ASC");
while($row=mysql_fetch_assoc($sql))
$output[]=$row;
print(json_encode($output));
mysql_close();
?>

MySQL

CREATE TABLE `test`.`category` (
`category_id` INT NOT NULL AUTO_INCREMENT PRIMARY KEY ,
`category` VARCHAR( 255 ) NOT NULL
 ) ENGINE = MYISAM ;

I am getting a NullPointer Exception, when I execute in android. Is the Php File correct?

Please I need your help with this! Thanks

link|improve this question

58% accept rate
may use debugger in android and get detailed error from LogCat window – krish Mar 3 at 5:27
feedback

1 Answer

up vote 1 down vote accepted

I'm thinking your php should be as follows (instead of quotes on the table.column use backticks).

<?php
mysql_connect("localhost","root","");
mysql_select_db("test");
$q=mysql_query("SELECT * FROM category ORDER BY `category` ASC");
$output = array();
while($row = mysql_fetch_assoc($sql))
    $output[]=$row;

print(json_encode($output));
mysql_close();
?>
link|improve this answer
Im still getting a Null Pointer exception! :( – yourdroid Mar 3 at 5:28
I think it is this part of the code thats throwing error! [CODE] try{ JSONArray jArray = new JSONArray(result); JSONObject json_data=null; for(int i=0;i<jArray.length();i++) { json_data = jArray.getJSONObject(i); r.add(json_data.getString("category")); } setListAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_expandable_list_item_1, r)); } [/CODE] The 'result' is not retrieved. – yourdroid Mar 3 at 5:30
1  
Does the debugger show you which line number in your code? If you bring up 10.0.2.2/PhpAndMySql/category.php in your browser does it show the json encoded string of your categories? – Aaron W. Mar 3 at 5:31
1  
really need a line number to determine why you're getting exception – Martin Mar 3 at 5:45
@AaronW. when I open the php file in browser i don't see anything. No json data is displayed. – yourdroid Mar 3 at 7:32
show 2 more comments
feedback

Your Answer

 
or
required, but never shown

Not the answer you're looking for? Browse other questions tagged or ask your own question.