本文介绍了用Volley获取JSONArrays的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从服务器获取JSONArray,但遇到各种错误.尝试添加所有这些导入.

I am trying to get a JSONArray from a server but getting all kinds of errors. EDIT : tried adding all these imports.

    import android.support.v7.app.AppCompatActivity;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.Response.Listener;
import com.android.volley.NetworkResponse;
import com.android.volley.ParseError;
import com.android.volley.toolbox.JsonArrayRequest;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.Volley;
import com.android.volley.Response.ErrorListener;


    public class VolleyFetcher extends AppCompatActivity
    {
        RequestQueue queue = Volley.newRequestQueue(this);
        String url = "https://xxxxxx.json"; 

        JsonObjectRequest jsObjRequest = new JsonObjectRequest(
                Request.Method.GET, url, null, new Response.Listener<JSONArray>() {
                    @Override
                    public void onResponse(JSONArray response) {
                    // TODO Auto-generated method stub
                }
                }, new Response.ErrorListener() {

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

        queue.add(jsObjRequest);    
    }

我的错误:

Response cannot be resolved to a type

JSONArray cannot be resolved to a type

Syntax error on token "jsObjRequest", VariableDeclaratorId expected after this token (//line : queue.add(jsObjRequest)

任何建议将不胜感激.

推荐答案

JSONObjectRequest.

如果希望返回JSON数组,则应使用JSONArrayRequest.此请求应包含在Volley的工具箱包中.

If you're expecting to get a JSON Array back, you should use JSONArrayRequest. This request should be included in Volley's toolbox package.

结果请求声明应如下所示:

The result request declaration should look like this:

JsonArrayRequest request = new JsonArrayRequest("http://my.url.com/", new Response.Listener<JSONArray>()
        {
            @Override
            public void onResponse(JSONArray response)
            {

            }
        }, new Response.ErrorListener()
        {
            @Override
            public void onErrorResponse(VolleyError error)
            {

            }
        });

这篇关于用Volley获取JSONArrays的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-30 05:55