为什么Volley返回响应对象的空值 [英] Why is Volley returning null value for response object

查看:58
本文介绍了为什么Volley返回响应对象的空值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的onResponse方法

public void onResponse(SongInfo response) {

    Log.v("TAG", "Response value is "+String.valueOf(response.artworkUrl30));
    // Prints "Response value is null"
}

String.valueOf(response.artworkUrl30))应该返回一个URL

String.valueOf(response.artworkUrl30)) should return a URL

在这里设置我的请求队列单例

Here I set up my request queue singleton

Static `mRequestQueue` variable and method 

public static RequestQueue mRequestQueue;

public static RequestQueue getRequestQueue() {
    if (mRequestQueue == null) {
        mRequestQueue = Volley.newRequestQueue(MainActivity.getAppContext());
    }
    return mRequestQueue;
}

我在这里请求获取JSON对象

Here I make the request to get the the JSON object

(实际上, URL 中有多个JSON对象)

(Actually there are multiple JSON object at the URL)

getRequestQueue();

String JSONURL = "https://itunes.apple.com/search?term=michael+jackson";

GsonRequest<SongInfo> myReq = new GsonRequest<SongInfo>(
    Request.Method.GET,
    JSONURL,
    SongInfo.class,
    null,
    createMyReqSuccessListener(),
    createMyReqErrorListener());

mRequestQueue.add(myReq);

这是我使用onResponse方法的成功响应侦听器

Here is my success response listener with onResponse method

private Response.Listener<SongInfo> createMyReqSuccessListener() {
    return new Response.Listener<SongInfo>() {
        @Override
        public void onResponse(SongInfo response) {
            // Do whatever you want to do with response;
            // Like response.tags.getListing_count(); etc. etc.

            Log.v("TAG", "This is the value of the string"+String.valueOf(response.artworkUrl30));
        }
    };
}

这是我的错误监听器

private Response.ErrorListener createMyReqErrorListener() {
    return new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            // Do whatever you want to do with error.getMessage();
        }
    };
}

这是我的GsonRequest

public class GsonRequest<T> extends Request<T> {

    private final Gson gson = new Gson();
    private final Class<T> clazz;
    private final Map<String, String> headers;
    private final Response.Listener<T> listener; // success listener

    /**
     * Make a GET request and return a parsed object from JSON.
     *
     * @param url URL of the request to make
     * @param clazz Relevant class object, for Gson's reflection
     * @param headers Map of request headers
     */

    public GsonRequest(int method,
                       String url,
                       Class<T> clazz,
                       Map<String, String> headers,
                       Response.Listener<T> listener, // success listener
                       Response.ErrorListener errorListener) { // error listener

        super(method, url, errorListener); // error listener
        this.clazz = clazz;
        this.headers = headers;
        this.listener = listener; // success listener
    }

    @Override
    public Map<String, String> getHeaders() throws AuthFailureError {
        return headers != null ? headers : super.getHeaders();
    }

    @Override
    protected void deliverResponse(T response) {
        listener.onResponse(response);
    }

    @Override
    protected Response<T> parseNetworkResponse(NetworkResponse response) {
        try {
            String json = new String(
                    response.data,
                    HttpHeaderParser.parseCharset(response.headers));
            return Response.success(
                    gson.fromJson(json, clazz),
                    HttpHeaderParser.parseCacheHeaders(response));
        } catch (UnsupportedEncodingException e) {
            return Response.error(new ParseError(e));
        } catch (JsonSyntaxException e) {
            return Response.error(new ParseError(e));
        }
    }
}

这是SongInfo

public class SongInfo {

    public String wrapperType;
    public String kind;
    public Integer artistId;
    public Integer collectionId;
    public Integer trackId;
    public String artistName;
    public String collectionName;
    public String trackName;
    public String collectionCensoredName;
    public String trackCensoredName;
    public String artistViewUrl;
    public String collectionViewUrl;
    public String trackViewUrl;
    public String previewUrl;
    public String artworkUrl30;
    public String artworkUrl60;
    public String artworkUrl100;
    public Float collectionPrice;
    public Float trackPrice;
    public String releaseDate;
    public String collectionExplicitness;
    public String trackExplicitness;
    public Integer discCount;
    public Integer discNumber;
    public Integer trackCount;
    public Integer trackNumber;
    public Integer trackTimeMillis;
    public String country;
    public String currency;
    public String primaryGenreName;
    public String radioStationUrl;
    public Boolean isStreamable;
}

推荐答案

我不认为您可以将Json响应映射为完全平坦,并且所有字段都位于Json的根层次结构.

I don't think you can just map the Json response as if it's completely flat, and all fields are located at the root of the Json hierarchy.

您的SongInfo模型应该看起来像这样:

Your SongInfo model should probably look like this:

public class SongInfo {

    public int resultCount;
    public List<Results> results;
}

您将需要一个Results对象,例如:

And you'll need a Results object, something like:

public class Results {
    public String wrapperType;
    public String kind;
    .
    .
    .
    public String artworkUrl30;
}

这篇关于为什么Volley返回响应对象的空值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆