java - 我想在 android Activity 中显示 json 对象的值

标签 java android json database

我已经开始进行 Android 开发,因此我正在从数据库请求 JSON 格式的数据,并且我得到了类似“{”users”:[{“child_name”:“John”}]}”的返回值,这是我用来显示响应的代码:

公开课欢迎 Activity 延伸至 Activity {

private static final String URL = "http://www.schools.weavearound.com/android_api/user_fetch.php";
private StringRequest request;
private RequestQueue requestQueue;

private Button log_out;


@Override
protected void onCreate (Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.welcome_activity);
    // email = (EditText) findViewById(R.id.email);
    final String strJson="";
    final TextView testTextView = (TextView) findViewById(R.id.display);

    requestQueue = Volley.newRequestQueue(this);

    final TextView nameView = (TextView) findViewById(R.id.result_txt);
    nameView.setText(getIntent().getExtras().getString("username"));

    // fetch the db here
    request = new StringRequest(Request.Method.POST, URL, new Response.Listener<String>() {
        @Override
        public void onResponse(String response) {
            //Log.e("tag", e.getMessage());
            String data = "";
            try {
                JSONObject jsonObject = new JSONObject(response);



                //Get the instance of JSONArray that contains JSONObjects
                JSONArray jsonArray = jsonObject.optJSONArray("users");

                //Iterate the jsonArray and print the info of JSONObjects
                for (int i = 0; i < jsonArray.length(); i++) {
                    JSONObject jsonObject1 = jsonArray.getJSONObject(i);


                    String name = jsonObject1.optString("child_name").toString();


                    data += "Node" + i + " : name= " + name +" \n ";
                }

                testTextView.setText(data);
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
    },new Response.ErrorListener() {

        @Override
        public void onErrorResponse(VolleyError error) {


        }
    });
}

}

但 Activity 中没有显示任何值。请帮助我。

最佳答案

请求创建后添加到requestQueue中,如下所示:

requestQueue.add(request);

关于java - 我想在 android Activity 中显示 json 对象的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35926121/

相关文章:

c# - 无法从 json 字符串中获取值

json - Hive表错误加载JSON数据

java - 在java中实现以Object作为参数的方法

java - 删除后台 Activity 堆栈

android - NullPointerException 调用 Dialog Fragment 时

android - 显示从源到目的地的路线图

json - 我可以在 mgo 中使用 json 标签作为 bson 标签吗?

java - 如何解决此错误 "Type mismatch: cannot convert from List<String> to Iterator<String>"

java - 如何推断 的类型参数?在接口(interface)声明中扩展类

安卓布局问题