我的应用程序出现了一个奇怪的问题。 我的服务器上有一个PHP文件,它回应了JSON,我从Android应用程序中获取它。
如果我更改PHP文件,那么它也会提供相同的JSON我不知道为什么以及如何。
如果我从浏览器中获取文件,它会显示正确的JSON。
现在,当我重命名文件时,它会显示正确的输出。
我厌倦了这种行为。 有人请帮忙。
答案 0 :(得分:0)
尝试添加此
httpGet.addHeader("Cache-Control", "no-cache");
答案 1 :(得分:0)
这听起来很像缓存问题。您可以尝试使用多种不同的选项来破坏缓存。
一种几乎总是有效的方法是将随机查询字符串值放在URL的末尾。像import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import static org.junit.Assert.assertNotNull;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = CDPlayerConfig.class)
public class CDPlayerTest {
@Rule
public final SystemOutRule log = new SystemOutRule.enableLog();
@Autowired
private MediaPlayer player;
@Autowired
private CompactDisc disc;
@Test
public void checkNotNull() {
assertNotNull(player);
assertNotNull(disc);
}
@Test
public void play() {
player.play();
}
}
之类的东西是一个很好的选择,因为它总是新的,但随机字符串也可以。
例如,如果您的网址是:
timestamp
新网址为:
https://my.site/getJSON.php
其他选项包括添加https://my.site/getJSON.php?t=<timestamp or random string>
标题(以及更多),但查询字符串技巧有效且易于实现。