将json对象的数组从php传递给android

时间:2013-03-20 11:19:53

标签: json arrays

我正在尝试通过json对象将一组数据从php传递给android。但我得到“解析错误:语法错误”。请帮助我.thanx

”        

// db详细信息

$dbhost="localhost";
$dbuser="root";
$dbpass="";
$dbdb="opac";

//连接到db

$con=mysql_connect($dbhost, $dbuser, $dbpass) or die("connection error");

//选择db

mysql_select_db($dbdb) or die("db selecction error");
$stk=$_POST[‘stock’];

$query=mysql_query("SELECT Title FROM books WHERE Stock>’$stk’”);

    if(!$query)
    {
           die('Could not get data: ' . mysql_error());
    }
    $index=0;
    while($row = mysql_fetch_array($query, MYSQL_ASSOC)) 
    {
           $output[$index]=$row;
           $index++;
    }

    echo json_encode($output);
     mysql_close();
    ?>  

对应的android文件是....当android文件被执行时解析错误显示“不幸的是,已经停止”

   public class BooksActivity extends ListActivity {

HttpClient httpclient;
HttpPost httppost;
ArrayList<NameValuePair> namevaluepairs;
HttpResponse httpresponse;
HttpEntity httpentity;
ArrayList<NameValuePair> nameValuePairs;
InputStream is;
String result = "";

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
    StrictMode.setThreadPolicy(policy);
    setContentView(R.layout.activity_books);
    nameValuePairs = new ArrayList<NameValuePair>();
    nameValuePairs.add(new BasicNameValuePair("stock","5"));

    //httppost
    try{
        httpclient = new DefaultHttpClient();
        httppost = new HttpPost("http://10.0.2.2:80/books.php");
        httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
        httpresponse = httpclient.execute(httppost);
        httpentity = httpresponse.getEntity();
        is = httpentity.getContent();
    }
    catch(Exception e){
           // Log.e("log_tag", "Error in http connection "+e.toString());
        Toast.makeText(getBaseContext(), "http post error"+e, Toast.LENGTH_SHORT ).show();
    }

    //result conversion
    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 in converting result "+e.toString());
        Toast.makeText(getBaseContext(), "result conversion error"+e, Toast.LENGTH_SHORT ).show();
    }

    //parsing json data

    try{
        String[] returnString = new String[100];
        JSONArray jArray = new JSONArray(result);           
        ListView listView = (ListView) findViewById(R.id.list);
        for(int i=0;i<jArray.length();i++){
                JSONObject json_data = jArray.getJSONObject(i);
                returnString[i] = json_data.getString("Title");              
        }
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
                  android.R.layout.simple_list_item_1, android.R.id.list, returnString);
        listView.setAdapter(adapter);
    }catch(JSONException e){
        //Log.e("log_tag", "Error parsing data "+e.toString());
        Toast.makeText(getBaseContext(), "parsing error"+e, Toast.LENGTH_SHORT ).show();
    }

}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.activity_books, menu);
    return true;
}

}

1 个答案:

答案 0 :(得分:0)

你在这里使用反引号吗?

$stk=$_POST[‘stock’];

他们需要引用标记

$stk=$_POST['stock'];

此行也有一个utf-8花式引用

$query=mysql_query("SELECT Title FROM books WHERE Stock>`$stk”);

应该是ascii引用

$query=mysql_query("SELECT Title FROM books WHERE Stock>$stk");

从博客中复制代码时,这些是常见错误;)