Android 尝试使用Volley和GSON从空数组读取

Android 尝试使用Volley和GSON从空数组读取,android,gson,android-volley,Android,Gson,Android Volley,我正在尝试制作一个音乐播放器 下面是我如何定义音乐播放器中的歌曲。我有一个带有一些属性的“歌曲对象” SongObject { public String albumArtURI; // Some other attributes } 要获取albumArtURI,我需要查询它。如果URI不存在,我会尝试从internet上下拉图像URL,但会出现以下错误: 尝试从空数组中读取 下面是从internet获取图像url并将其分配给song对象的代码 if(songObje

我正在尝试制作一个音乐播放器

下面是我如何定义音乐播放器中的歌曲。我有一个带有一些属性的“歌曲对象”

SongObject {

    public String albumArtURI;

    // Some other attributes 
}
要获取albumArtURI,我需要查询它。如果URI不存在,我会尝试从internet上下拉图像URL,但会出现以下错误:

尝试从空数组中读取

下面是从internet获取图像url并将其分配给song对象的代码

 if(songObject.albumArtURI != null){

                    // The URI exists, so we leave as is

                } else {

                    // The album art URI does not exist,
                    // so we try to pull an album art .jpg URL down from iTunes
                    ServiceHandler serviceHandler = new ServiceHandler(MainActivity.this);
                    serviceHandler.getJSON_URL();               

                    songObject.albumArtURI = serviceHandler.JSONObjectsList[0].artworkUrl30; 
                    // Throws error "Attempt to read from a null array"
                }
从上面的代码可以看出,这一行抛出了错误

songObject.albumArtURI = serviceHandler.JSONObjectsList[0].artworkUrl30; 
// Gives error "Attempt to read from a null array"
但是,
serviceHandler.JSONObjectsList[0].artworkUrl30
不应为空数组

从后续类中的行可以看出,通过使用print语句,所述变量不是null

Log.v("TAG",String.valueOf(JSONObjectsList[0].artworkUrl30)); 
// Prints http://is4.mzstatic.com/image/thumb/Music6/v4/68/b5/27/68b5273f-7044-8dbb-4ad1-82473837a136/source/30x30bb.jpg    
下面是课程本身:

public class ServiceHandler {

    Context ctx;
    SongInfo[] JSONObjectsList;
    String albumArtURI;
    String url = "https://itunes.apple.com/search?term=michael+jackson";

    public ServiceHandler(Context ctx){

        this.ctx = ctx;
    }

    public void getJSON_URL(){

        RequestQueue requestQueue = Volley.newRequestQueue(ctx);

        JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(url, new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject response) {

                // If there is a response, do this
                if (response != null) {

                    // Get the number of JSON objects on the web-page
                    int resultCount = response.optInt("resultCount");

                    // If there is a JSON object on the web-page, do this
                    if (resultCount > 0) {

                        // Get a gson object
                        Gson gson = new Gson();

                        // Get a JSONArray from the results
                        JSONArray jsonArray = response.optJSONArray("results");

                        // If the array exists, do this
                        if (jsonArray != null) {

                            // Convert the JSONArray into a Java object array 
                            JSONObjectsList = gson.fromJson(jsonArray.toString(), SongInfo[].class);

                            // Prints http://is4.mzstatic.com/image/thumb/Music6/v4/68/b5/27/68b5273f-7044-8dbb-4ad1-82473837a136/source/30x30bb.jpg
                            Log.v("TAG",String.valueOf(JSONObjectsList[0].artworkUrl30));                                                          
                        }
                    }
                }
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                Log.e("LOG", error.toString());
            }
        });

        requestQueue.add(jsonObjectRequest);
    }

    public class SongInfo {

        // Some attributes

        public String artworkUrl30;

        // Some more attributes
    }
}
公共类ServiceHandler{
上下文ctx;
SongInfo[]JSONObjectsList;
字符串albumArtURI;
字符串url=”https://itunes.apple.com/search?term=michael+杰克逊”;
公共服务处理程序(上下文ctx){
this.ctx=ctx;
}
public void getJSON_URL(){
RequestQueue RequestQueue=Volley.newRequestQueue(ctx);
JsonObjectRequest JsonObjectRequest=新的JsonObjectRequest(url,new Response.Listener()){
@凌驾
公共void onResponse(JSONObject响应){
//如果有响应,请执行此操作
if(响应!=null){
//获取网页上JSON对象的数量
int resultCount=response.optInt(“resultCount”);
//如果网页上有JSON对象,请执行此操作
如果(结果计数>0){
//获取一个gson对象
Gson Gson=新的Gson();
//从结果中获取JSONArray
JSONArray JSONArray=response.optJSONArray(“结果”);
//如果数组存在,请执行此操作
if(jsonArray!=null){
//将JSONArray转换为Java对象数组
JSONObjectsList=gson.fromJson(jsonArray.toString(),SongInfo[].class);
//印刷品http://is4.mzstatic.com/image/thumb/Music6/v4/68/b5/27/68b5273f-7044-8dbb-4ad1-82473837a136/source/30x30bb.jpg
Log.v(“TAG”,String.valueOf(JSONObjectsList[0].artworkUrl30));
}
}
}
}
},new Response.ErrorListener(){
@凌驾
公共无效onErrorResponse(截击错误){
Log.e(“Log”,error.toString());
}
});
add(jsonObjectRequest);
}
公共类歌曲信息{
//一些属性
公共字符串artworkUrl30;
//更多属性
}
}
所以我认为问题与截击的异步性有关

我试图在数组被“填充”之前读取它


当截击完成时,数组最终会被填充吗?

你的假设是正确的,这是因为异步。当截击完成它的任务(即调用并执行onResponse方法)时,数组将具有数据。您可以使用接口从中获取数据

定义接口

public interface MyInterface {
    public void myTask (SongInfo[] songInfo)
}
然后在getJSON_URL方法中将该接口用作参数

getJSON_URL (MyInterface myInterface) {
...
@Override
public void onResponse(JSONObject response) {
.....
myInterface.myTask(JSONObjectsList);
}
然后在你的主课上

...
serviceHandler.getJSON_URL(new MyInterface);
@Override 
public void myTask (SongInfo[] songInfo) {
...
songObject.albumArtURI = serviceHandler.JSONObjectsList[0].artworkUrl30;
...
}
...

谢谢有趣的是,当我这样做时,
albumArtURI=JSONObjectsList[0],然后
songObject.albumArtURI=serviceHandler.albumArtURI