Android Json数据(来自mysqli的输出)在TextView中不起作用

时间:2016-09-09 17:37:01

标签: php android json android-volley

我正在做一些基本测试,用于显示从PHP mysqli输出到TextView的json数据。

TextView mTxtDisplay;

String url = "http://192.168.1.102/web_service/test.php/";

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    mTxtDisplay = (TextView) findViewById(R.id.tv);

    JsonObjectRequest jsObjRequest = new JsonObjectRequest
            (Request.Method.GET, url, null, new Response.Listener<JSONObject>() {

                @Override
                public void onResponse(JSONObject response) {
                    mTxtDisplay.setText(response.toString());
                }
            }, new Response.ErrorListener() {

                @Override
                public void onErrorResponse(VolleyError error) {
                    // TODO Auto-generated method stub

                }
            });

    RequestQueue requestQueue = Volley.newRequestQueue(this);
    requestQueue.add(jsObjRequest);
}}

192.168.1.102/web_service/test.php

$sql="SELECT * FROM item where uid='1'";
$result = mysqli_query($conn, $sql);
$myArray = array();
if (mysqli_num_rows($result) > 0) {
    while($row = $result->fetch_array(MYSQL_ASSOC)) {
            $myArray[] = $row;
    }
    echo json_encode($myArray);
}

输出

[{"iid":"482","uid":"1","item_id":"1","item_name":"Chicken Rice","item_price":"1","item_quantity":"1","status":"0"},
{"iid":"483","uid":"1","item_id":"1","item_name":"French Fries","item_price":"1","item_quantity":"1","status":"0"},
{"iid":"484","uid":"1","item_id":"1","item_name":"apple","item_price":"1","item_quantity":"1","status":"0"},
{"iid":"492","uid":"1","item_id":"1","item_name":"western+italian","item_price":"1","item_quantity":"1","status":"0"},
{"iid":"493","uid":"1","item_id":"1","item_name":"no_cat","item_price":"1","item_quantity":"1","status":"0"}]

我认为我的json是正确的?但它根本没有显示在Textview中,它是空的!没有任何错误

之后,我意识到以下格式的数据运行良好(普通硬编码数组)。

{
"1":{"name":"Superman","date":"20160909","location":"Mars"},
"2":{"name":"Xman","date":"20160909","location":"Galaxy"},
"3":{"name":"Batman","date":"20160909","location":"Earth"}
}

这意味着没有“[]”的第二个json数据工作正常但是带有“[]”的第一个json数据不起作用。但这不应该是正确的方式吗?

编码有什么问题?

2 个答案:

答案 0 :(得分:1)

这是因为[]中的数据是JSONArray而不是JSONObject。而不是JSONObjectRequest尝试StringRequest,然后将您的响应打印到EditText / TextView。您可以通过将数组转换为JSONArray来访问数组的元素。

JSONArray arr=new JSONArray(response);

答案 1 :(得分:1)

而不是使用StringRequest尝试使用JSONObjectRequest,您的代码将是这样的:

        StringRequest jsonReq = new StringRequest(Request.Method.POST,
            URL, new Response.Listener<String>() {

        @Override
        public void onResponse(String s) {
         Log.d("Response", s);
         textView.setText(s)
        }
    }, new Response.ErrorListener() {

        @Override
        public void onErrorResponse(VolleyError error) {
         //do error code here..
        }
    }) {
        @Override
        public Map<String, String> getParams(){
            Map<String, String> params = new HashMap<String, String>();
            params.put("param1", value);
            return params;
        }
    };

getParams用于向服务器发送带有某些用户定义值的请求,或者用于在运行时获取某些特定数据。