为什么 Volley 为响应对象返回空值

Why is Volley returning null value for response object

这是我的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)) 应该 return 一个 URL

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

Static `mRequestQueue` variable and method 

public static RequestQueue mRequestQueue;

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

这里我请求获取 JSON 对象

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

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 方法的成功响应侦听器

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

这是我的GsonRequestclass

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

这里是SongInfoclass

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

如果您期望 JsonObject 作为响应,请使用 Volley JsonObjectRequest,否则 JsonArray 作为响应,你必须发出 Volley JsonArrayRequest。

得到响应后让Gson用SongInfo处理响应数据class。

如果您正在考虑使用其他库进行网络调用,我建议您这样做, enter link description here

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

您的 SongInfo 模型应该如下所示:

public class SongInfo {

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

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

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