Android Volley JSON Request在TextView中显示空数据

时间:2016-09-09 12:52:06

标签: android json android-volley

我阅读Android官方文档并尝试在Textview中显示JSON数据。我尝试了以下编码,但是当我点击Button时,没有任何内容出现,也没有错误提示。它出什么问题了?编码主要来自Android官方文档。

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: " + response.toString());
                }
            }, new Response.ErrorListener() {

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

                }
            });
}}

192.168.1.102/web_service/test.php

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

2 个答案:

答案 0 :(得分:2)

Json Object之后的最后添加Request QueueError Listener

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

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

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

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

答案 1 :(得分:0)

你不能简单地从JSON获取数据,解析它:

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>/title>
<link rel="stylesheet" href="//code.jquery.com/ui/1.11.2/themes/smoothness/jquery-ui.css">
<script src="//code.jquery.com/jquery-1.10.2.js"></script>
<script src="//code.jquery.com/ui/1.11.2/jquery-ui.js"></script>
<style>
    .dt{}

</style>
<script>

$(document).on('focus', '.dt', function() {$(this).datepicker({ dateFormat : 'dd-mm-yy'});});  
</script>
</head>

<body>
<form name="log" method="get">
 <table>
  <tr>
   <th>Select Date</th><td><input name="date" id="date" value="" class="dt"></td>
   <tr><td><input type="submit" name="post"  id="post" value="Submit"></td></tr>
  </tr>
 </table>
</form>
</body>
</html>