android - 无法解析 MySQL 数据库中的 JSON 数据

标签 android mysql json getjson

我正在开发一个 Android 应用程序,需要我连接到 mysql 数据库并从中检索信息。我使用 php 脚本连接到服务器并查询它,然后以 json 格式输出结果:

if(!mysql_connect($mysql_host,$mysql_user,$mysql_pass) || !mysql_select_db($mysql_db)){
die($conn_error);
}



$q=mysql_query("SELECT * FROM food");

  while($e=mysql_fetch_assoc($q))

          $output[]=$e;

       print(json_encode($output));

mysql_close();




?>

这是我的java代码,它使用HttpGet来获取数据并将其转换为字符串。然后它会解析数据并将其显示在屏幕上。

public class HttpExample extends Activity {

TextView httpStuff;

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.httpex);
    httpStuff = (TextView) findViewById(R.id.tvHttp);

    GetMethodEx test = new GetMethodEx();
    String returned;
    try {
        returned = test.getInternetData();
        httpStuff.setText(returned);
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

}

public class GetMethodEx {

    public String getInternetData() throws Exception {

        BufferedReader in = null;
        String data = "";
        String returnString = null;

        // httpGet

        try {

            HttpClient client = new DefaultHttpClient();
            URI website = new URI("http://10.0.2.2/connect.php");
            HttpGet request = new HttpGet();
            request.setURI(website);
            HttpResponse response = client.execute(request);
            in = new BufferedReader(new InputStreamReader(response
                    .getEntity().getContent()));
            StringBuffer sb = new StringBuffer("");
            String l = "";
            String nl = System.getProperty("line.separator");

            while ((l = in.readLine()) != null) {
                sb.append(l + nl);
            }
            in.close();
            data = sb.toString();
            // return data;
        } catch (Exception e) {
            Log.e("log_tag", "Error converting result " + e.toString());
        }
        // parse json data
        try {
            JSONArray jArray = new JSONArray(data);
            for (int i = 0; i < jArray.length(); i++) {
                JSONObject json_data = jArray.getJSONObject(i);
                Log.i("log_tag", "Foodname: " + json_data.getString("food"));
                // Get an output to the screen
                returnString += "\n\t" + jArray.getJSONObject(i);
            }
        } catch (JSONException e) {
            Log.e("log_tag", "Error parsing data " + e.toString());
        }
        return returnString;

    }

}

}

除了解析数据之外,它正在做我想做的一切。当我只想要食物名称时,它只是在屏幕上显示数据库的全部内容。有人可以帮忙吗?谢谢

编辑:这是我的 php 脚本的 json 输出的屏幕截图

enter image description here

最佳答案

尝试

returnString += "Foodname: " + json_data.getString("food") + "\n";

而不是

returnString += "\n\t" + jArray.getJSONObject(i);

我认为这只会显示食物名称。我现在无法测试,因为我是在手机上写的。

关于android - 无法解析 MySQL 数据库中的 JSON 数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11970450/

相关文章:

android - 为什么 Linearlayout 需要包裹在 scrollview 中才能使其可滚动。?

android - 使用 Retrofit2 解析 GSON 自定义字段 - Android

php - 使用 PHP 更新 MySQL 中的 blob

sql - 通过自左连接传递聚合函数列

javascript - Json 解析并总结 json 内的值

android - 无法在 Android 项目中实现 Google map

android - MVVM - 如何跨多个 ViewModel 共享单个存储库类

mysql - 即使对于解释计划,也要努力应对长时间运行的查询,这也花费了很多时间

php - json_encode() 不能很好地显示阿拉伯字符

javascript - json 解析中的错误控制字符错误