0

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

'

//数据库详情

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

//连接数据库

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

//选择数据库

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;
}

}

4

1 回答 1

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");

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

于 2013-03-28T05:05:36.790 回答