Android Volley StringRequest有时无法正常工作 [英] Android Volley StringRequest not working sometimes

查看:87
本文介绍了Android Volley StringRequest有时无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用以下代码从API获取一些本地视频网址.

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);
}

onCreate()内部,我这样初始化RequestQueue.

And inside onCreate(), I initialize RequestQueue like this.

queue = Volley.newRequestQueue(this);

有时候,getVideoAds()可以工作.有时,它不起作用.奇怪的是,即使在catch()上使用Exception之后,也没有出现任何错误.如果不起作用,请执行Clean Project -> Rebuild Project -> Make Project,然后再次起作用.但是经过几次构建后,大多数只是再构建1次,它再次停止工作.

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.

如果您想了解有关playLoopVideo的更多信息,请查看

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.

如果需要,这是我的build.gradle(app).

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'
}

更新:

这真的很奇怪.我的getVideoAds()方法在另一个方法内部调用,该方法调用在同一服务器上的不同API上生成StringRequest的方法.为了使其更容易理解,下面是它的代码.

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);
}

当前,getMessageAds()调用的API不会返回任何内容.我试图评论它和下面的处理程序,终于奏效了.即使是视频播放也可以.然后,我尝试取消注释getMessageAds()并在服务器上添加一些消息,一切正常.但是删除邮件后,它将再次停止工作.

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?

推荐答案

在齐射中添加

  stringRequest.setRetryPolicy(DefaultRetryPolicy(20000, 3, 1.0f));

这篇关于Android Volley StringRequest有时无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆