1

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

1
  • may use debugger in android and get detailed error from LogCat window Commented Mar 3, 2012 at 5:27

1 Answer 1

1

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();
?>
Sign up to request clarification or add additional context in comments.

7 Comments

Im still getting a Null Pointer exception! :(
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.
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?
really need a line number to determine why you're getting exception
@AaronW. when I open the php file in browser i don't see anything. No json data is displayed.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.