gpt4 book ai didi

java - 处理 OnErrorResponse 方法 - Volley

转载 作者:行者123 更新时间:2023-11-30 10:13:54 28 4
gpt4 key购买 nike

我正在使用 volley 从服务器获取 json 格式的数据。连接到 Wifi 时一切正常。但是当用户连接到移动数据时,特别是如果移动数据连接速度很慢,就会调用 OnErrorResponse 方法。我在 OnErrorResponse 方法中放置了一条 Toast 消息。在某些设备中,此 toast 消息因 NullPointerException 而崩溃,而在其他设备中它运行良好。

此外,如果我等待了很长时间,结果还没有显示,那么应用程序会因内存不足错误而崩溃。请帮助我。

如何高效处理这些问题?

代码:

private RequestQueue songQueue = songQueue = Volley.newRequestQueue(getContext());



private void getNewSongs() {

JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.GET, newSongsUrl, null, new Response.Listener<JSONObject>() {
@SuppressLint("LongLogTag")
@Override
public void onResponse(JSONObject response) {

JSONObject artistObject = null;
String artist = "";
String albumID = "";
String artistID = null;

try {


JSONObject songListObjects = response.getJSONObject("new_song");

if(songListObjects.has("data")){

JSONObject data = songListObjects.getJSONObject("data");

if(data.has("song_list")) {

JSONArray songList = data.getJSONArray("song_list");

for (int i=0;i<5;i++){

JSONObject songListObject = songList.getJSONObject(i);

if (songListObject.has("mid")) {

Log.e("SongListObject has", "songmid");
String songID = songListObject.getString("mid");
String songTitle = songListObject.getString("name");
long duration = songListObject.getLong("interval") * 1000;
Log.e("Title:", songTitle);
Log.e("Duration:", String.valueOf(duration));

JSONObject albumObject = songListObject.getJSONObject("album");

if(albumObject.has("mid")){

albumID = albumObject.getString("mid");


}



JSONArray artistArray = songListObject.getJSONArray("singer");

for (int j = 0; j < artistArray.length(); j++) {

artistObject = artistArray.getJSONObject(j);

if (j == 0) {

if (artistObject.has("name")) {


Log.e("ArtistObject: ", "has name");

artist = artistObject.getString("name");
artistID = artistObject.getString("mid");

}
} else {

if (artistObject.has("name")) {

Log.e("ArtistObject: ", "has name");

artist = artist + " ft. " + artistObject.getString("name");

}
}
}


SongInfoModel songInfoModel = new SongInfoModel(123, songTitle,artistID, ((artist == null || (artist.length() == 0)) ? "Unknown" : artist), duration, songData, albumArtURL);
SoundCloudNewSongs.add(songInfoModel);

if (getView() != null){

getView().findViewById(R.id.onlineProgressLoad).setVisibility(View.GONE);
getView().findViewById(R.id.mainLayout).setVisibility(View.VISIBLE);

}



}

}


}
}



newSongsAdapter = new NewSongsAdapter(SoundCloudNewSongs, getContext(),listenerOnline,1);
recyclerView_newSongs.setAdapter(newSongsAdapter);


} catch (JSONException e) {


}

}
}, new Response.ErrorListener() {
@SuppressLint("LongLogTag")
@Override
public void onErrorResponse(VolleyError error) {


if(error.getMessage()==null)
Toast.makeText(getActivity(), "Timeout error :(", Toast.LENGTH_SHORT).show();
else Toast.makeText(getActivity(), "Timeout error :(", Toast.LENGTH_SHORT).show();


}
});

songQueue.add(jsonObjectRequest);

}

日志:

java.lang.NullPointerException: 
at android.widget.Toast.<init> (Toast.java:114)
at android.widget.Toast.makeText (Toast.java:277)
at android.widget.Toast.makeText (Toast.java:267)
at com.musicplayer.musicana.OnlinePlaySupport$9.onErrorResponse (Unknown Source:15)
at com.android.volley.Request.deliverError (Unknown Source:8)
at com.android.volley.ExecutorDelivery$ResponseDeliveryRunnable.run (Unknown Source:62)
at android.os.Handler.handleCallback (Handler.java:790)
at android.os.Handler.dispatchMessage (Handler.java:99)
at android.os.Looper.loop (Looper.java:164)
at android.app.ActivityThread.main (ActivityThread.java:6518)
at java.lang.reflect.Method.invoke (Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run (RuntimeInit.java:438)
at com.android.internal.os.ZygoteInit.main (ZygoteInit.java:807)

最佳答案

您可以定义成功执行的重试次数为

jsonObjectRequest.setRetryPolicy(new DefaultRetryPolicy(
1000*5,
/*DefaultRetryPolicy.DEFAULT_MAX_RETRIES*/ 3,
DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
songQueue.add(jsonObjectRequest);

关于 NPE。 It is possible that VolleyError is null所以你可以使用

if(error!=null && error.getMessage()==null)

关于java - 处理 OnErrorResponse 方法 - Volley,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51118068/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com