问题
I'm using the code below to get some local video URL from an API.
public void getVideoAds(String serverURL){
String url = "http://"+serverURL+"/video/";
StringRequest stringRequest = new StringRequest(url, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
try {
JSONObject jsonObj = new JSONObject(response);
JSONObject defaultObj = jsonObj.getJSONObject("default");
JSONArray videoObj = jsonObj.getJSONArray("videos");
defaultUrl = defaultObj.getString("url");
for (int i = 0; i < videoObj.length(); i++){
JSONObject video = videoObj.getJSONObject(i);
VideoAd v = new VideoAd();
v.setName(getVideoNameFromLink(video.getString("url")));
v.setUrl(video.getString("url"));
v.setVideoId(video.getInt("id"));
v.setVersion(video.getInt("version"));
v.setLocalPath("");
vidArrLst.add(v);
}
playLoopVideo();
} catch (Exception e) {
Log.d(TAG, "getVidAds res err: " + e.getMessage());
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(ctx, "VideoAds Err: "+error.getLocalizedMessage(), Toast.LENGTH_LONG).show();
Log.d(TAG, "getVidAds err: " + error.getMessage());
}
});
queue.add(stringRequest);
}
And inside onCreate(), I initialize RequestQueue like this.
queue = Volley.newRequestQueue(this);
Sometimes, getVideoAds() works. Sometimes, it doesn't work. The weird thing as well is that I'm not getting any error even after using Exception on my catch(). If it doesn't work, I do a Clean Project -> Rebuild Project -> Make Project and it works again. But after a few builds, mostly just 1 more build, it stops working again.
If you want to know more about playLoopVideo, look at this SO question I made. It's about the video playing part of my app. It currently doesn't play the video if I try to play it inside the onResponse of getVideoAds that's why I made a different question for it because maybe if I fix my Volley the video would play again.
This is my build.gradle(app) if you need it.
apply plugin: 'com.android.application'
android {
compileSdkVersion 28
defaultConfig {
applicationId "biz.net.com.streamer"
minSdkVersion 17
targetSdkVersion 28
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
compileOptions {
sourceCompatibility 1.8
targetCompatibility 1.8
}
}
dependencies {
implementation fileTree(include: ['*.jar'], dir: 'libs')
implementation 'com.android.support:appcompat-v7:28.0.0'
implementation 'com.android.support.constraint:constraint-layout:1.1.3'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
implementation 'com.google.android.exoplayer:exoplayer:2.9.1'
implementation 'com.android.volley:volley:1.1.1'
}
UPDATE:
This is really weird. My getVideoAds() method is called inside another method that calls a method the makes a StringRequest on a different API on the same server. To make it easier to understand below is the code for it.
private void getAdvertisements(final String serverURL){
getVideoAds(serverURL);
getMessageAds(serverURL);
new android.os.Handler().postDelayed(
new Runnable() {
public void run() {
Log.d(TAG, "Updating ads marquee");
getMessageAds(serverURL);
}
},
300000);
}
Currently, the API that getMessageAds() calls don't return anything. I tried to comment it and the handler below and it finally worked. Even the video playing worked. I then tried to uncomment getMessageAds() and add a few messages on the server, everything is working fine. But after I remove the messages, it stops working again.
What could be the reason for this to happen?
回答1:
Add this in volley
stringRequest.setRetryPolicy(DefaultRetryPolicy(20000, 3, 1.0f));
来源:https://stackoverflow.com/questions/55292686/android-volley-stringrequest-not-working-sometimes