英文:
why retrofit is setting previous response on reloading data?
问题
我正在开发一个应用程序,该应用程序提供一组练习或考试问题,在测试结束时,我们提供重新测试您技能的选项,但是每当用户刷新以进行重新测试时,它显示与第一次尝试中相同的响应,但如果我尝试从php文件的URL获取数据,则显示不同的数据集。
只是在想是否与缓存有关。
连接文件:
RestAdapter.Builder builder = new RestAdapter.Builder();
builder.setEndpoint("xyz.com/Android/");
builder.setLogLevel(RestAdapter.LogLevel.FULL);
builder.setClient(new OkClient(new OkHttpClient()));
RestAdapter adapter = builder.build();
return(adapter.create(SetDataToDatabase.class));
@GET("/phpFile.php")
void abc(@Query("sub") String subject, Callback
请求数据的方法:
con.abc(subject, new Callback<BeanFile>() {
@Override
public void success(BeanFile bean, Response response) {
adapter = new TestPaperAdapter(bean.data, Test.this, id, answers, attempt, choosedAnswer);
recyclerView.setAdapter(adapter);
}
@Override
public void failure(RetrofitError error) {
if (error.getKind().equals(RetrofitError.Kind.NETWORK)) {
Toast.makeText(Test.this, "No Internet connection found.", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(Test.this, error.toString(), Toast.LENGTH_SHORT).show();
}
}
});
Logcat:
(这是一系列日志消息,显示了应用程序的操作和响应,包括网络请求、数据获取等。)
英文:
I am working on an application which provides set of question for practice or exam and at end of test we provide an adoption to retest your skills but whenever user refresh for a retest it shows same response as in the first attempt but if I try to fetch data from phpfile url It shows different set of data
just wondering that any cache to do with this.
just ignore the URLs because of restrictions all of them are edited
Connection File
RestAdapter.Builder builder=new RestAdapter.Builder();
builder.setEndpoint("xyz.com/Android/");
builder.setLogLevel(RestAdapter.LogLevel.FULL);
builder.setClient(new OkClient(new OkHttpClient()));
RestAdapter adapter=builder.build();
return(adapter.create(SetDataToDatabase.class));
@GET("/phpFile.php")
void abc(@Query("sub") String subject, Callback<BeanFile> testQuestion_beanCallback);
method requesting data
con.abc(subject, new Callback<BeanFile>() {
@Override
public void success(BeanFile bean, Response response) {
adapter = new TestPaperAdapter(bean.data, Test.this, id, answers, attempt, choosedAnswer);
recyclerView.setAdapter(adapter);
}
@Override
public void failure(RetrofitError error) {
if (error.getKind().equals(RetrofitError.Kind.NETWORK)) {
Toast.makeText(Test.this, "No Internet connection found.", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(Test.this, error.toString(), Toast.LENGTH_SHORT).show();
}
}
});
Logcat
--------- beginning of system
2020-08-27 19:44:21.494 2172-3954/system_process I/ActivityTaskManager: START u0 {cmp=com.Prince.xyz/.Test (has extras)} from uid 10137
2020-08-27 19:44:21.514 1846-8173/? W/audio_hw_generic: Not supplying enough data to HAL, expected position 24186159 , only wrote 24185990
2020-08-27 19:44:21.561 2172-3954/system_process W/InputReader: Device has associated, but no associated display id.
2020-08-27 19:44:21.577 2172-3954/system_process I/chatty: uid=1000(system) Binder:2172_12 identical 48 lines
2020-08-27 19:44:21.577 2172-3954/system_process W/InputReader: Device has associated, but no associated display id.
2020-08-27 19:44:21.584 14105-14105/com.Prince.xyz W/ActivityThread: handleWindowVisibility: no activity for token android.os.BinderProxy@cc389ba
2020-08-27 19:44:22.401 14105-14543/com.Prince.xyz D/Retrofit: ---> HTTP GET xyz.com/Android/phpFile.php?sub=mix
2020-08-27 19:44:22.401 14105-14543/com.Prince.xyz D/Retrofit: ---> END HTTP (no body)
2020-08-27 19:44:22.446 1839-14546/? E/ResolverController: No valid NAT64 prefix (100, <unspecified>/0)
2020-08-27 19:44:22.480 1856-1972/? D/gralloc_ranchu: gralloc_alloc: Creating ashmem region of size 2965504
2020-08-27 19:44:22.655 14105-14543/com.Prince.xyz W/e.xyz: Accessing hidden method Lcom/android/org/conscrypt/ConscryptFileDescriptorSocket;->setUseSessionTickets(Z)V (greylist,core-platform-api, reflection, allowed)
2020-08-27 19:44:22.655 14105-14543/com.Prince.xyz W/e.xyz: Accessing hidden method Lcom/android/org/conscrypt/ConscryptFileDescriptorSocket;->setHostname(Ljava/lang/String;)V (greylist,core-platform-api, reflection, allowed)
2020-08-27 19:44:22.655 14105-14543/com.Prince.xyz W/e.xyz: Accessing hidden method Lcom/android/org/conscrypt/OpenSSLSocketImpl;->setAlpnProtocols([B)V (greylist,core-platform-api, reflection, allowed)
2020-08-27 19:44:22.705 14105-14105/com.Prince.xyz E/RecyclerView: No adapter attached; skipping layout
2020-08-27 19:44:22.712 1856-1972/? D/gralloc_ranchu: gralloc_alloc: Creating ashmem region of size 17051648
2020-08-27 19:44:22.741 14105-14119/com.Prince.xyz I/e.xyz: Background young concurrent copying GC freed 12549(896KB) AllocSpace objects, 11(1260KB) LOS objects, 9% free, 7645KB/8484KB, paused 2.509ms total 116.491ms
2020-08-27 19:44:22.835 14105-14543/com.Prince.xyz W/e.xyz: Accessing hidden method Lcom/android/org/conscrypt/OpenSSLSocketImpl;->getAlpnSelectedProtocol()[B (greylist,core-platform-api, reflection, allowed)
2020-08-27 19:44:22.856 2172-2208/system_process W/InputReader: Device has associated, but no associated display id.
2020-08-27 19:44:22.859 2172-2208/system_process I/chatty: uid=1000(system) android.anim identical 18 lines
2020-08-27 19:44:22.859 2172-2208/system_process W/InputReader: Device has associated, but no associated display id.
2020-08-27 19:44:22.934 14105-14502/com.Prince.xyz D/FA: Connected to remote service
2020-08-27 19:44:23.101 14105-14119/com.Prince.xyz I/e.xyz: Background concurrent copying GC freed 7200(409KB) AllocSpace objects, 8(4688KB) LOS objects, 33% free, 12MB/18MB, paused 1.298ms total 290.209ms
2020-08-27 19:44:23.240 14105-14543/com.Prince.xyz D/Retrofit: <--- HTTP 200 xyz.com/Android/phpFile.php?sub=mix (838ms)
2020-08-27 19:44:23.240 14105-14543/com.Prince.xyz D/Retrofit: OkHttp-Selected-Protocol: h2
2020-08-27 19:44:23.240 14105-14543/com.Prince.xyz D/Retrofit: date: Thu, 27 Aug 2020 14:14:26 GMT
2020-08-27 19:44:23.240 14105-14543/com.Prince.xyz D/Retrofit: server: nginx/1.17.6
2020-08-27 19:44:23.240 14105-14543/com.Prince.xyz D/Retrofit: content-type: text/html; charset=UTF-8
2020-08-27 19:44:23.240 14105-14543/com.Prince.xyz D/Retrofit: vary: Accept-Encoding
2020-08-27 19:44:23.241 14105-14543/com.Prince.xyz D/Retrofit: x-server-cache: true
2020-08-27 19:44:23.241 14105-14543/com.Prince.xyz D/Retrofit: x-proxy-cache: HIT
2020-08-27 19:44:23.241 14105-14543/com.Prince.xyz D/Retrofit: OkHttp-Sent-Millis: 1598537662989
2020-08-27 19:44:23.241 14105-14543/com.Prince.xyz D/Retrofit: OkHttp-Received-Millis: 1598537663224
2020-08-27 19:44:23.249 14105-14543/com.Prince.xyz D/Retrofit: {"result":true,"data":[{"0":"432","quesId":"432","1":"Which animal is said to have fingerprints almost identical from those of human beings?","question":"Which animal is said to have fingerprints almost identical from those of human beings?","2":"Panda","optA":"Panda","3":"Raccoon","optB":"Raccoon","4":"Koala","optC":"Koala","5":"Lemur","optD":"Lemur","6":"C","answer":"C","7":"Fingerprint Examination","subject":"Fingerprint Examination"}]}
2020-08-27 19:44:23.250 14105-14543/com.Prince.xyz D/Retrofit: <--- END HTTP (4482-byte body)
2020-08-27 19:44:23.353 14105-14105/com.Prince.xyz D/Tag: 12
2020-08-27 19:44:23.370 2446-12529/com.google.android.gms.persistent E/SQLiteDatabase: Error inserting flex_time=1400000 job_id=-1 period=2801000 source=16 requires_charging=0 target_class=com.google.android.gms.measurement.PackageMeasurementTaskService user_id=0 target_package=com.google.android.gms tag=Measurement.PackageMeasurementTaskService.UPLOAD_TASK_TAG task_type=0 source_version=200414000 required_network_type=0 runtime=1598537663267 retry_strategy={"maximum_backoff_seconds":{"3600":0},"initial_backoff_seconds":{"30":0},"retry_policy":{"0":0}} last_runtime=0
android.database.sqlite.SQLiteConstraintException: UNIQUE constraint failed: pending_ops.tag, pending_ops.target_class, pending_ops.target_package, pending_ops.user_id (code 2067 SQLITE_CONSTRAINT_UNIQUE)
at android.database.sqlite.SQLiteConnection.nativeExecuteForLastInsertedRowId(Native Method)
at android.database.sqlite.SQLiteConnection.executeForLastInsertedRowId(SQLiteConnection.java:879)
at android.database.sqlite.SQLiteSession.executeForLastInsertedRowId(SQLiteSession.java:790)
at android.database.sqlite.SQLiteStatement.executeInsert(SQLiteStatement.java:88)
at android.database.sqlite.SQLiteDatabase.insertWithOnConflict(SQLiteDatabase.java:1599)
at android.database.sqlite.SQLiteDatabase.insert(SQLiteDatabase.java:1468)
at aozt.a(:com.google.android.gms@200414022@20.04.14 (040700-294335909):147)
at aozi.a(:com.google.android.gms@200414022@20.04.14 (040700-294335909):182)
at aozi.a(:com.google.android.gms@200414022@20.04.14 (040700-294335909):25)
at aozi.a(:com.google.android.gms@200414022@20.04.14 (040700-294335909):177)
at aovr.run(:com.google.android.gms@200414022@20.04.14 (040700-294335909):8)
at srw.b(:com.google.android.gms@200414022@20.04.14 (040700-294335909):14)
at srw.run(:com.google.android.gms@200414022@20.04.14 (040700-294335909):2)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
at sxw.run(:com.google.android.gms@200414022@20.04.14 (040700-294335909):0)
at java.lang.Thread.run(Thread.java:919)
2020-08-27 19:44:23.381 2172-3954/system_process E/InputDispatcher: Window handle Window{d62a60f u0 com.Prince.xyz/com.Prince.xyz.Test} has no registered input channel
2020-08-27 19:44:23.509 2446-12529/com.google.android.gms.persistent E/SQLiteDatabase: Error inserting flex_time=1399000 job_id=-1 period=2800000 source=16 requires_charging=0 target_class=com.google.android.gms.measurement.PackageMeasurementTaskService user_id=0 target_package=com.google.android.gms tag=Measurement.PackageMeasurementTaskService.UPLOAD_TASK_TAG task_type=0 source_version=200414000 required_network_type=0 runtime=1598537663420 retry_strategy={"maximum_backoff_seconds":{"3600":0},"initial_backoff_seconds":{"30":0},"retry_policy":{"0":0}} last_runtime=0
android.database.sqlite.SQLiteConstraintException: UNIQUE constraint failed: pending_ops.tag, pending_ops.target_class, pending_ops.target_package, pending_ops.user_id (code 2067 SQLITE_CONSTRAINT_UNIQUE)
at android.database.sqlite.SQLiteConnection.nativeExecuteForLastInsertedRowId(Native Method)
at android.database.sqlite.SQLiteConnection.executeForLastInsertedRowId(SQLiteConnection.java:879)
at android.database.sqlite.SQLiteSession.executeForLastInsertedRowId(SQLiteSession.java:790)
at android.database.sqlite.SQLiteStatement.executeInsert(SQLiteStatement.java:88)
at android.database.sqlite.SQLiteDatabase.insertWithOnConflict(SQLiteDatabase.java:1599)
at android.database.sqlite.SQLiteDatabase.insert(SQLiteDatabase.java:1468)
at aozt.a(:com.google.android.gms@200414022@20.04.14 (040700-294335909):147)
at aozi.a(:com.google.android.gms@200414022@20.04.14 (040700-294335909):182)
at aozi.a(:com.google.android.gms@200414022@20.04.14 (040700-294335909):25)
at aozi.a(:com.google.android.gms@200414022@20.04.14 (040700-294335909):177)
at aovr.run(:com.google.android.gms@200414022@20.04.14 (040700-294335909):8)
at srw.b(:com.google.android.gms@200414022@20.04.14 (040700-294335909):14)
at srw.run(:com.google.android.gms@200414022@20.04.14 (040700-294335909):2)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
at sxw.run(:com.google.android.gms@200414022@20.04.14 (040700-294335909):0)
at java.lang.Thread.run(Thread.java:919)
2020-08-27 19:44:23.657 1856-1972/? D/gralloc_ranchu: gralloc_alloc: Creating ashmem region of size 17051648
2020-08-27 19:44:23.811 1856-1972/? D/gralloc_ranchu: gralloc_alloc: Creating ashmem region of size 17051648
2020-08-27 19:44:24.061 14105-14119/com.Prince.xyz I/e.xyz: Background concurrent copying GC freed 2128(114KB) AllocSpace objects, 15(7912KB) LOS objects, 35% free, 11MB/17MB, paused 876us total 104.785ms
2020-08-27 19:44:24.647 1846-2019/? W/audio_hw_generic: Not supplying enough data to HAL, expected position 24460726 , only wrote 24324139
2020-08-27 19:44:26.193 14105-14119/com.Prince.xyz I/e.xyz: Background concurrent copying GC freed 1151(74KB) AllocSpace objects, 14(6832KB) LOS objects, 35% free, 11MB/17MB, paused 701us total 103.154ms
2020-08-27 19:44:29.757 14105-14119/com.Prince.xyz I/e.xyz: Background concurrent copying GC freed 1157(77KB) AllocSpace objects, 17(7680KB) LOS objects, 35% free, 11MB/17MB, paused 695us total 116.937ms
2020-08-27 19:44:31.912 14105-14119/com.Prince.xyz I/e.xyz: Background concurrent copying GC freed 1163(81KB) AllocSpace objects, 19(9032KB) LOS objects, 35% free, 11MB/17MB, paused 716us total 120.237ms
2020-08-27 19:44:32.201 1846-8173/? W/audio_hw_generic: Not supplying enough data to HAL, expected position 24324181 , only wrote 24324139
2020-08-27 19:44:32.220 14105-14550/com.Prince.xyz D/Retrofit: ---> HTTP GET xyz.com/Android/phpFile.php?sub=mix
2020-08-27 19:44:32.220 14105-14550/com.Prince.xyz D/Retrofit: ---> END HTTP (no body)
2020-08-27 19:44:32.224 1839-14551/? E/ResolverController: No valid NAT64 prefix (100, <unspecified>/0)
2020-08-27 19:44:32.525 1856-1972/? D/gralloc_ranchu: gralloc_alloc: Creating ashmem region of size 2965504
2020-08-27 19:44:33.059 14105-14550/com.Prince.xyz D/Retrofit: <--- HTTP 200 xyz.com/Android/phpFile.php?sub=mix (838ms)
2020-08-27 19:44:33.060 14105-14550/com.Prince.xyz D/Retrofit: OkHttp-Selected-Protocol: h2
2020-08-27 19:44:33.060 14105-14550/com.Prince.xyz D/Retrofit: date: Thu, 27 Aug 2020 14:14:35 GMT
2020-08-27 19:44:33.060 14105-14550/com.Prince.xyz D/Retrofit: server: nginx/1.17.6
2020-08-27 19:44:33.060 14105-14550/com.Prince.xyz D/Retrofit: content-type: text/html; charset=UTF-8
2020-08-27 19:44:33.060 14105-14550/com.Prince.xyz D/Retrofit: vary: Accept-Encoding
2020-08-27 19:44:33.060 14105-14550/com.Prince.xyz D/Retrofit: x-server-cache: true
2020-08-27 19:44:33.060 14105-14550/com.Prince.xyz D/Retrofit: x-proxy-cache: HIT
2020-08-27 19:44:33.060 14105-14550/com.Prince.xyz D/Retrofit: OkHttp-Sent-Millis: 1598537672799
2020-08-27 19:44:33.060 14105-14550/com.Prince.xyz D/Retrofit: OkHttp-Received-Millis: 1598537673057
2020-08-27 19:44:33.066 14105-14550/com.Prince.xyz D/Retrofit: {"result":true,"data":[{"0":"432","quesId":"432","1":"Which animal is said to have fingerprints almost identical from those of human beings?","question":"Which animal is said to have fingerprints almost identical from those of human beings?","2":"Panda","optA":"Panda","3":"Raccoon","optB":"Raccoon","4":"Koala","optC":"Koala","5":"Lemur","optD":"Lemur","6":"C","answer":"C","7":"Fingerprint Examination","subject":"Fingerprint Examination"}]}
2020-08-27 19:44:33.067 14105-14550/com.Prince.xyz D/Retrofit: <--- END HTTP (4482-byte body)
2020-08-27 19:44:33.077 14105-14105/com.Prince.xyz D/Tag: 12
2020-08-27 19:44:33.098 2172-3027/system_process E/InputDispatcher: Window handle Window{500f57e u0 com.Prince.xyz/com.Prince.xyz.Test} has no registered input channel
2020-08-27 19:44:33.114 2172-3954/system_process E/InputDispatcher: Window handle Window{500f57e u0 com.Prince.xyz/com.Prince.xyz.Test} has no registered input channel
2020-08-27 19:44:33.392 14105-14119/com.Prince.xyz I/e.xyz: Background concurrent copying GC freed 6277(388KB) AllocSpace objects, 14(6832KB) LOS objects, 34% free, 11MB/17MB, paused 8.099ms total 299.390ms
2020-08-27 19:44:35.366 1846-2019/? W/audio_hw_generic: Not supplying enough data to HAL, expected position 24601375 , only wrote 24463610
2020-08-27 19:44:38.524 14105-14119/com.Prince.xyz I/e.xyz: Background concurrent copying GC freed 912(97KB) AllocSpace objects, 13(6560KB) LOS objects, 34% free, 11MB/17MB, paused 714us total 109.312ms
2020-08-27 19:44:42.746 1846-8173/? W/audio_hw_generic: Not supplying enough data to HAL, expected position 24463641 , only wrote 24463610
答案1
得分: 0
我正在回答自己的问题
我刚找到了一个临时解决方法,但无法找到Retrofit和Android内部发生了什么以及原因
@GET("/phpFile.php")
void abc(@Query("sub") String subject, @Query("aaaa") int x, Callback<BeanFile> testQuestion_beanCallback);
在PHP文件中,我接收了任何变量但没有使用它
Random rand = new Random();
int n = 0;
n = rand.nextInt(anyValue);
con.abc(subject, n, new Callback<BeanFile>() {
@Override
public void success(BeanFile bean, Response response) {
adapter = new TestPaperAdapter(bean.data, Test.this, id, answers, attempt, choosedAnswer);
recyclerView.setAdapter(adapter);
}
@Override
public void failure(RetrofitError error) {
if (error.getKind().equals(RetrofitError.Kind.NETWORK)) {
Toast.makeText(Test.this, "未找到网络连接。", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(Test.this, error.toString(), Toast.LENGTH_SHORT).show();
}
}
});
英文:
I am answering my own question
I just found a temporary solution but failed to find what is happening inside retrofit and android and why
@GET("/phpFile.php")
void abc(@Query("sub") String subject,@Quesry("aaaa") int x, Callback<BeanFile> testQuestion_beanCallback);
and in PHP file I received any variable but kept it unused
Random rand = new Random();
int n = 0;
n = rand.nextInt(anyValue);
con.abc(subject,n, new Callback<BeanFile>() {
@Override
public void success(BeanFile bean, Response response) {
adapter = new TestPaperAdapter(bean.data, Test.this, id, answers, attempt, choosedAnswer);
recyclerView.setAdapter(adapter);
}
@Override
public void failure(RetrofitError error) {
if (error.getKind().equals(RetrofitError.Kind.NETWORK)) {
Toast.makeText(Test.this, "No Internet connection found.", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(Test.this, error.toString(), Toast.LENGTH_SHORT).show();
}
}
});
答案2
得分: 0
你可以根据以下文档从OkHttp中调试缓存:
事件 - https://square.github.io/okhttp/events/
缓存解释 - https://square.github.io/okhttp/caching/
你可以通过像使用虚拟参数一样覆盖缓存,或者通过明确覆盖行为来进行覆盖。
https://square.github.io/okhttp/4.x/okhttp/okhttp3/-cache/
Request request = new Request.Builder()
.cacheControl(new CacheControl.Builder().noCache().build())
.url("http://publicobject.com/helloworld.txt")
.build();
英文:
You can debug caching from OkHttp by following these docs
Events - https://square.github.io/okhttp/events/
Cache explanation - https://square.github.io/okhttp/caching/
You can override caching either as you have with a fake param, or by specifically overriding the behaviour
https://square.github.io/okhttp/4.x/okhttp/okhttp3/-cache/
Request request = new Request.Builder()
.cacheControl(new CacheControl.Builder().noCache().build())
.url("http://publicobject.com/helloworld.txt")
.build();
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论