无法解析从MySQL数据库JSON数据 [英] Cannot parse JSON data from MySQL Database

查看:671
本文介绍了无法解析从MySQL数据库JSON数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发,需要我连接到MySQL数据库并从中检索信息Android应用程序。我使用的是PHP脚本连接到服务器和查询,然后将结果输出JSON格式:
    

I am developing an android application that requires me to connect to a mysql database and retrieve information from it. I am using a php script to connect to the server and query it and then output the results in json format:

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 code使用HTTPGET来获取数据,并将其转换为字符串。然后,它的目的是分析数据,并显示在屏幕上。

Here is my java code that uses HttpGet to get the data and convert it to a string. It is then meant to parse the data and show it on the screen.

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;

    }

}

}

这是做的一切我想除了分析数据。它只是显示在屏幕上的数据库的全部内容时,我只希望食品names.Can谁能帮助?谢谢

It is doing everything I want except to parse the data. It is just showing the entire contents of the database on the screen when I only want the food names.Can anyone help?Thanks

编辑:这是我的JSON输出的截图从我的PHP脚本

edit: Here is a screenshot of my json output from my php script

推荐答案

尝试

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

而不是

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

我认为这将只显示食品名称。我现在不能测试它,因为我写在我的手机上。

I think this will only show foods name. I cant test it right now, because im writing this on my mobile phone.

这篇关于无法解析从MySQL数据库JSON数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆