首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >我正在尝试获取JSON数据并将其用于android应用程序,使用volley库,但它无法工作,甚至找不到我的代码中的错误所在

我正在尝试获取JSON数据并将其用于android应用程序,使用volley库,但它无法工作,甚至找不到我的代码中的错误所在
EN

Stack Overflow用户
提问于 2018-06-21 10:19:06
回答 3查看 59关注 0票数 0

//这里我们从setOnClickListener方法调用一个方法调用jsoneParse

private void jsonParse(){

//在这里我插入了我的Url

    String url = "https://api.myjson.com/bins/hrtn6";   

//json对象请求

    final JsonObjectRequest request = new JsonObjectRequest(Request.Method.POST, url, null, new Response.Listener<JSONObject>() {
        @Override
        public void onResponse(JSONObject response) {

            try {

//这里是解析json object JSONArray jsonArray =response.getJSONArray(“students”)的重要位置;

                for(int i = 0; i < jsonArray.length();i++){
                    JSONObject student = jsonArray.getJSONObject(i);
                    String firstname = student.getString("firstname");
                    String lastname = student.getString("lastname");
                    int age = student.getInt("age");


                    detailsText.append(firstname+" "+lastname+" "+String.valueOf(age)+"\n");
                }
            } 

//这些都是那些错误处理

           catch (JSONException e) {
                e.printStackTrace();
            }

        }
    }, new Response.ErrorListener() {

        @Override
        public void onErrorResponse(VolleyError error) {
            error.printStackTrace();
        }
    });

//这里我们将请求插入到请求队列中

    mQueue.add(request);
}
EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2018-06-21 14:40:16

只需将request method更改为get,如下所示

final JsonObjectRequest request = new JsonObjectRequest(Request.Method.GET, url, null, new Response.Listener<JSONObject>() {
.
.
.
票数 0
EN

Stack Overflow用户

发布于 2018-06-21 14:43:15

改变这一点

 final JsonObjectRequest request = new JsonObjectRequest(Request.Method.POST, url, null, new Response.Listener<JSONObject>() {

要这样做:

final JsonObjectRequest request = new JsonObjectRequest(Request.Method.GET, url, null, new Response.Listener<JSONObject>() {
票数 0
EN

Stack Overflow用户

发布于 2018-06-21 15:09:08

将requestbody设置为string而不是null (url后的参数)

final JsonObjectRequest request = new JsonObjectRequest(Request.Method.POST, url, "", new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject response) {
                .....
            }

     }

这对我很有帮助,希望这能帮助你,快乐的编码

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50959426

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档