2012-03-03 72 views
1

我是一个新手到android,我正在学习通过android客户端使用Php,MySql和JSON连接到服务器。为了测试目的,我在本地主机上运行。 所以这是我所做的。 数据库demo.phpAndroid,连接到MySQL使用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中执行。 Php文件是否正确?

请帮我解决这个问题! 感谢

+0

可能会在android中使用调试器,并从LogCat窗口获得详细错误 – Riskhan 2012-03-03 05:27:33

回答

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

我仍然得到一个空指针异常! :( – yourdroid 2012-03-03 05:28:49

+0

我认为这是代码的这一部分抛出错误! [代码]尝试{0} {0} {0} {0} JSONArray jArray = new JSONArray(result); JSONObject json_data = NULL; for(int i = 0; i (此,android.R.layout。 simple_expandable_list_item_1,R));} [/ CODE] 的“结果”没有检索 – yourdroid 2012-03-03 05:30:24

+1

是否在你的代码调试器告诉你哪一行号,如果你带了http://10.0.2.2/PhpAndMySql/category? .php i你的浏览器是否显示你的类别的json编码的字符串? – 2012-03-03 05:31:31