gpt4 book ai didi

java - Android volley 使用 intentservice 发送数据两次?

转载 作者:行者123 更新时间:2023-11-30 01:58:39 25 4
gpt4 key购买 nike

    package com.newsak.services;

import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;

import android.app.IntentService;
import android.content.Intent;
import android.os.Bundle;
import android.os.ResultReceiver;
import android.util.Log;

import com.android.volley.Cache;
import com.android.volley.DefaultRetryPolicy;
import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.newsak.controller.NewsakContoller;
import com.newsak.data.FeedItem;
import com.newsak.parse.XmlParser;
import com.newsak.constants.SportsUrls;
public class FetchBackgroundData extends IntentService {

private List<FeedItem> feedItems;
public static List<FeedItem> largeFeedItems;
boolean cachedFalg = false;
public static final int FINISHED_STATE = 0;
ResultReceiver receiver;
int counter = 0 ;
public String [] MY_URLS = SportsUrls.SPORTS_URLS;
public FetchBackgroundData() {
super("FetchBackgroundData");

}

@Override
protected void onHandleIntent(Intent intent) {

receiver = intent.getParcelableExtra("receiver");
GO();
}

public void GO() {
feedItems = new ArrayList<FeedItem>();
largeFeedItems = new ArrayList<FeedItem>();
// check for the cache
Cache cache = NewsakContoller.getInstance().getRequestQueue().getCache();
List<Cache.Entry> entry = new ArrayList<Cache.Entry>();
for(String url : MY_URLS){
entry.add(cache.get(url));
}

for (Cache.Entry en : entry) {

if (en != null) {
// fetch the data from the cache ...
try {
String data = new String(en.data, "UTF-8");
feedItems = XmlParser.getItem(data);
largeFeedItems.addAll(feedItems);
cachedFalg = true;
Log.d("cache_start", "cache start");
if(feedItems.size() > 0){
counter++;
feedItems = null ;
if(counter == 7){
receiver.send(FINISHED_STATE, Bundle.EMPTY);
}

}


} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
}
}

if (!cachedFalg) {
for(String url : MY_URLS){
getRequest(url);

}
Log.d("without_cache_start", "cache start");
}
}

public void getRequest(String url) {
StringRequest request = new StringRequest(Request.Method.GET, url,
new Response.Listener<String>() {

public void onResponse(String result) {
feedItems = XmlParser.getItem(result);
largeFeedItems.addAll(feedItems);
if(feedItems.size() > 0){
counter++;
feedItems = null ;
if(counter == 7){
receiver.send(FINISHED_STATE, Bundle.EMPTY);
}

}
}
}, new Response.ErrorListener() {
public void onErrorResponse(VolleyError arg0) {
}
});
//handle return twice data
request.setRetryPolicy(new DefaultRetryPolicy( 0,
DefaultRetryPolicy.DEFAULT_MAX_RETRIES ,
DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
NewsakContoller.getInstance().addToRequestQueue(request);
}
}

这是我的 intentservice 通过 xml 解析器获取数据。所以谁能帮我弄清楚问题出在哪里??我用这个 Android volley sending data twice 但是这个解决方案不适用于我的代码

最佳答案

你的代码

request.setRetryPolicy(new DefaultRetryPolicy(
0,
DefaultRetryPolicy.DEFAULT_MAX_RETRIES,
DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));

替换这段代码

request.setRetryPolicy(new DefaultRetryPolicy(
30000,
DefaultRetryPolicy.DEFAULT_MAX_RETRIES,
DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));

关于java - Android volley 使用 intentservice 发送数据两次?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31816648/

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