【问题标题】:getting JSONArrays with Volley使用 Volley 获取 JSONArrays
【发布时间】:2015-07-12 08:27:45
【问题描述】:

我正在尝试从服务器获取 JSONArray,但出现各种错误。编辑:尝试添加所有这些导入。

    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)

非常感谢任何建议。

【问题讨论】:

    标签: android json android-volley


    【解决方案1】:

    如果返回的结果是 JSON 对象,则应使用JSONObjectRequest。

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

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

    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)
                {
    
                }
            });
    

    【讨论】:

    • 谢谢。我改变了它,现在它声明 The type new Response.ErrorListener(){} must implement the inherited abstract method Response.ErrorListener.onErrorResponse(VolleyError) ,这很奇怪,因为我确实添加了那个实现..并且 queue.add(jsObjRequest) 上的错误仍然出现..
    • 我仍然得到 The type new Response.ErrorListener(){} 必须实现继承的抽象方法 Response.ErrorListener.onErrorResponse(VolleyError) 出于某种原因
    • 这很奇怪。你会有冲突的进口吗?尝试清理/重新启动 IDE?
    • 试过了。同样的事情..顺便说一句,我得到的另一个错误是 VolleyError 无法解析为类型 关于导入,我已经用我添加的所有导入编辑了帖子。
    • 我已经以某种方式解决了这个问题。现在我在queue.add(jsObjRequest); 上只有错误:Syntax error on token "request", VariableDeclaratorId expected after this token
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-10-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多