Android凌空使用intentserivce两次发送数据?

时间:2015-08-04 18:29:08

标签: java android xml rss feed

    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     但是这个解决方案没有用我的代码

1 个答案:

答案 0 :(得分:1)

您的此代码

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