我正在开发一个android项目,并且在如何从响应中复制数据方面陷入困境。我想知道是否有一种简单的方法,因为我的方法行不通。当我从public void onResponse(ResultItem [] response)打印元素时,我可以看到内容存在。但是,以后打印时,该元素不再存在。
public class MainActivity extends AppCompatActivity{
ResultItem[] resultItemArray;
ArrayList<String> jsonArrayList;
public void getRESTResults(View view) throws MalformedURLException, AuthFailureError {
RequestQueue queue = Volley.newRequestQueue(this);
String url = "https://rest.opensubtitles.org/search/query-" + query.getText();
GsonRequest<ResultItem[]> gsonRequest = null;
jsonArrayList = new ArrayList<>();
try {
gsonRequest = new GsonRequest<ResultItem[]>(Request.Method.GET,url, ResultItem[].class, getHeaders(), new Response.Listener<ResultItem[]>() {
@Override
public void onResponse(ResultItem[] response) {
Gson gson = new Gson();
for(ResultItem item : response){
String jsonString = gson.toJson(item);
jsonArrayList.add(jsonString);
//jsonArrayList contains 500 elements here
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
error.printStackTrace();
}
}) ;
} catch (AuthFailureError authFailureError) {
authFailureError.printStackTrace();
}
queue.add(gsonRequest);
ArrayList<ResultItem> resultItems = getResultItemArray();
for(ResultItem item: resultItems) {
System.out.println(item.toString());
//why is this arraylist empty now?
}
}
public ArrayList<ResultItem> getResultItemArray(){
ArrayList<ResultItem> resultArrayList = new ArrayList<>();
Gson gson = new Gson();
for(String str: jsonArrayList){
ResultItem item = gson.fromJson(str, ResultItem.class);
resultArrayList.add(item);
}
return resultArrayList;
}
}
这是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 Map<String, String> params;
private final Response.Listener<T> 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, Map<String, String> params,
Response.Listener<T> listener, Response.ErrorListener errorListener) {
super(method, url, errorListener);
this.clazz = clazz;
this.headers = headers;
this.params = params;
this.listener = listener;
}
/**
* Recieves header
*
* @param method
* @param url
* @param clazz
* @param params
* @param listener
* @param errorListener
*/
public GsonRequest(int method, String url, Class<T> clazz,
Map<String, String> params, Response.Listener<T> listener,
Response.ErrorListener errorListener) {
super(method,url, errorListener);
this.clazz = clazz;
this.headers = new HashMap<String, String>();
this.params = params;
this.listener = listener;
}
/**
* No params or headers
*
* @param method
* @param url
* @param clazz
* @param listener
* @param errorListener
*/
public GsonRequest(int method, String url, Class<T> clazz,
Response.Listener<T> listener, Response.ErrorListener errorListener) {
super(method, url, errorListener);
this.clazz = clazz;
this.headers = new HashMap<String, String>();
this.params = new HashMap<String, String>();
this.listener = listener;
}
@Override
public Map<String, String> getHeaders() throws AuthFailureError {
return headers != null ? headers : super.getHeaders();
}
@Override
public Map<String, String> getParams() throws AuthFailureError {
return params != null ? params : super.getParams();
}
@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));
}
}
}
这是我的ResultItem类
public class ResultItem{
String MovieHash;
String IDMovieImdb;
public ResultItem(String MovieHash, String IDMovieImdb){
this.MovieHash = MovieHash;
this.IDMovieImdb = IDMovieImdb;
}
public String getMovieHash(){
return MovieHash;
}
public String getIDMovieImdb(){
return IDMovieImdb;
}
@Override
public String toString() {
return "ResultItem{" +
"MovieHash='" + MovieHash + '\'' +
", IDMovieImdb='" + IDMovieImdb + '\'' +
'}';
}
}