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

时间:2015-11-30 04:50:09

标签: android android-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))应该返回一个网址

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

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

这是我的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;
}

2 个答案:

答案 0 :(得分:1)

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

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

public class SongInfo {

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

你需要一个结果对象,例如:

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

答案 1 :(得分:0)

如果你期望JsonObject作为Response使用Volley JsonObjectRequest,否则 JsonArray作为回应,你必须制作Volley JsonArrayRequest。

获得响应后,让Gson使用SongInfo类处理响应数据。

如果您打算使用其他图书馆进行网络通话,我建议您这样做, enter link description here