【问题标题】:Android, Connecting to MySQL using PHP: Null Pointer exceptionAndroid,使用 PHP 连接到 MySQL:空指针异常
【发布时间】:2012-03-03 05:20:32
【问题描述】:

我是 android 新手,我正在学习使用 Php、MySql 和 JSON 通过 android 客户端连接到服务器。出于测试目的,我在 localhost 上运行。 所以这就是我所做的。 数据库 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 ;

当我在 android 中执行时,我得到一个 NullPointer 异常。 php文件是否正确?

我需要你的帮助! 谢谢

【问题讨论】:

  • 可以在android中使用调试器并从LogCat窗口获取详细错误

标签: php android mysql json web-services


【解决方案1】:

我认为你的 php 应该如下(而不是 table.column 上的引号使用反引号)。

<?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();
?>

【讨论】:

  • 我仍然收到空指针异常! :(
  • 我认为是这部分代码抛出错误! [代码] try{ JSONArray jArray = new JSONArray(result); JSONObject json_data=null; for(int i=0;i(this, android.R.layout.simple_expandable_list_item_1, r)); } [/CODE] 未检索到“结果”。
  • 调试器是否显示代码中的行号?如果您在浏览器中打开10.0.2.2/PhpAndMySql/category.php,它会显示您的类别的 json 编码字符串吗?
  • 确实需要一个行号来确定为什么会出现异常
  • @AaronW。当我在浏览器中打开 php 文件时,我什么也看不到。没有显示任何 json 数据。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-04-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多