這裏是我的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
在這裏,我建立了我的請求隊列單
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));
}
}
}
這裏是
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;
}
我沒那麼熟悉'GSON',但我的猜測是你試圖將你的'JSON'響應塞進一個模型('SongInfo'),這並沒有說明'artworkUrl30'在一個名爲'results'的JsonArray中。您的模型可能需要一個'List'結果字段,而'Result'對象將包含'artworkUrl30'字段。 –