downloadUrl返回null Stream

时间:2014-06-03 05:01:37

标签: android rss

我正在尝试解析rss Feed。但它返回一个空值。我调试了&检查。在onpostExecute()中 - 结果的值为null。 这是我尝试过的代码:

这是我的RssFragment.java

    package com.example.samplerssfeed;

    import java.io.IOException;
    import java.io.InputStream;
    import java.net.HttpURLConnection;
    import java.net.URL;
    import java.util.ArrayList;
    import java.util.List;

    import org.xmlpull.v1.XmlPullParserException;

    import android.os.AsyncTask;
    import android.os.Bundle;
    import android.support.v4.app.Fragment;
    import android.util.Log;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.ListView;
    import android.widget.Toast;


        public class RSSFragment extends Fragment {
            private ListView mListview;
            private static final String URL = "https://news.google.com/?output=rss";

            @Override
            public View onCreateView(LayoutInflater inflater, ViewGroup container,
                    Bundle savedInstanceState) {
                View view=inflater.inflate(R.layout.fragment_rs, container, false);
                mListview=(ListView)view.findViewById(R.id.listView);
                DownloadXmlTask xmltask=new DownloadXmlTask();
                xmltask.execute(URL);
                return view;

            }   
            private class DownloadXmlTask extends AsyncTask<String, Void, List<Item>>{

                @Override
                protected List<Item> doInBackground(String... urls) {
                    List<Item> items = new ArrayList<Item>();

                            try {
                                items=loadXmlFromNetwork(urls[0]);
                            } catch (XmlPullParserException e) {
                                // TODO Auto-generated catch block
                                e.printStackTrace();
                            } catch (IOException e) {
                                // TODO Auto-generated catch block
                                e.printStackTrace();
                            }
                            return items;

            }
                @Override
                protected void onPostExecute(List<Item> result) {
                    if(result==null){
                        Toast.makeText(getActivity(), "Parsing Faied", Toast.LENGTH_LONG).show();
                    }
                    else{
                    RssAdapter adapter=new RssAdapter(getActivity(),result);
                    Log.d("Parsing", "Calling Adapter");
                    mListview.setAdapter(adapter);
                    }
                }

        }

            private List<Item> loadXmlFromNetwork(String urlString) throws XmlPullParserException, IOException{
                InputStream stream=null;
                RSSParser rssParser=new RSSParser();
                List<Item> items=null;
                try {
                    stream = downloadUrl(urlString);        
                    items = rssParser.parse(stream);
                // Makes sure that the InputStream is closed after the app is
                // finished using it.
                } finally {
                    if (stream != null) {
                        stream.close();
                    } 
                 }
             return items;
            }
                // Given a string representation of a URL, sets up a connection and gets
                // an input stream.
                private InputStream downloadUrl(String urlString) throws IOException {
                    URL url = new URL(urlString);
                    HttpURLConnection conn = (HttpURLConnection) url.openConnection();
                    conn.setReadTimeout(10000 /* milliseconds */);
                    conn.setConnectTimeout(15000 /* milliseconds */);
                    conn.setRequestMethod("GET");
                    conn.setDoInput(true);
                    // Starts the query
                    conn.connect();
                    return conn.getInputStream();
                }
            }

这是我的片段类,我使用了Async任务。但是这个方法items=loadXmlFromNetwork(urls[0])返回一个空值

这是我的RssParser.java

 package com.example.samplerssfeed;

    import java.io.IOException;
    import java.io.InputStream;
    import java.util.ArrayList;
    import java.util.List;

    import org.xmlpull.v1.XmlPullParser;
    import org.xmlpull.v1.XmlPullParserException;

    import android.util.Xml;

    public class RSSParser {
    private static final String ns=null;
    //Instantiating the pull parser
    public List<Item> parse(InputStream in) throws XmlPullParserException,IOException{
        try{
            XmlPullParser parser=Xml.newPullParser();
            parser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, false);
            parser.setInput(in,null);
            parser.nextTag();
            return reedChannel(parser);

        }
        finally{
            in.close();
        }
    }

    //Reading the feed
    private List<Item> reedChannel(XmlPullParser parser) throws XmlPullParserException, IOException{
        List<Item> items = new ArrayList<Item>();

        parser.require(XmlPullParser.START_TAG, ns, "channel");
        while (parser.next() != XmlPullParser.END_TAG) {
            if (parser.getEventType() != XmlPullParser.START_TAG) {
                continue;
            }
            String name = parser.getName();
            // Starts by looking for the item tag
            if (name.equals("item")) {
                items.add(readItem(parser));
            } else {
                skip(parser);
            }
        }  
        return items;
    }

         private Item readItem(XmlPullParser parser) throws XmlPullParserException, IOException{
             parser.require(XmlPullParser.START_TAG, ns, "item");
             String title=null;
             String link=null;
             String summary=null;
             while (parser.next() != XmlPullParser.END_TAG) {
                    if (parser.getEventType() != XmlPullParser.START_TAG) {
                        continue;
                    }
                    String name = parser.getName();
                    if (name.equals("title")) {
                        title = readTitle(parser);
                    } else if (name.equals("description")) {
                        summary = readDescription(parser);
                    } else if (name.equals("link")) {
                        link = readLink(parser);
                    } else {
                        skip(parser);
                    }
                }
                return new Item(title, summary, link);
         }

        // Processes title tags in the feed.
         private String readTitle(XmlPullParser parser) throws IOException, XmlPullParserException {
             parser.require(XmlPullParser.START_TAG, ns, "title");
             String title = readText(parser);
             parser.require(XmlPullParser.END_TAG, ns, "title");
             return title;
         }

         // Processes link tags in the feed.
         private String readLink(XmlPullParser parser) throws IOException, XmlPullParserException {
             String link = "";
             parser.require(XmlPullParser.START_TAG, ns, "link");
             String tag = parser.getName();
             String relType = parser.getAttributeValue(null, "rel");  
             if (tag.equals("link")) {
                 if (relType.equals("alternate")){
                     link = parser.getAttributeValue(null, "href");
                     parser.nextTag();
                 } 
             }
             parser.require(XmlPullParser.END_TAG, ns, "link");
             return link;
         }

         // Processes summary tags in the feed.
         private String readDescription(XmlPullParser parser) throws IOException, XmlPullParserException {
             parser.require(XmlPullParser.START_TAG, ns, "description");
             String summary = readText(parser);
             parser.require(XmlPullParser.END_TAG, ns, "description");
             return summary;
         }

         // For the tags title and summary, extracts their text values.
         private String readText(XmlPullParser parser) throws IOException, XmlPullParserException {
             String result = "";
             if (parser.next() == XmlPullParser.TEXT) {
                 result = parser.getText();
                 parser.nextTag();
             }
             return result;
         }

         private void skip(XmlPullParser parser) throws XmlPullParserException, IOException {
                if (parser.getEventType() != XmlPullParser.START_TAG) {
                    throw new IllegalStateException();
                }
                int depth = 1;
                while (depth != 0) {
                    switch (parser.next()) {
                    case XmlPullParser.END_TAG:
                        depth--;
                        break;
                    case XmlPullParser.START_TAG:
                        depth++;
                        break;
                    }
                }
             }
        }

这是我的RssAdapter.java

package com.example.samplerssfeed;

import java.util.List;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

public class RssAdapter extends ArrayAdapter<Item>{
   private List<Item> mItems;
   private Context mContext;
   private LayoutInflater mInflater;
    public RssAdapter(Context context, List<Item> items) {
        super(context, R.layout.rss_items, items);
        mContext=context;
        mItems=items;
        mInflater=(LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }
    private static class Rssholder{
        TextView mTextview1,mTextview2;

    }
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        Rssholder holder;

        if (convertView == null) {
            holder = new Rssholder();
            convertView = mInflater.inflate(R.layout.rss_items, parent,
                    false);
            holder.mTextview1 = (TextView) convertView
                    .findViewById(R.id.textview1);
            holder.mTextview2 = (TextView) convertView
                    .findViewById(R.id.textview2);
            Log.d("Parsing", "Setting it to the listview");
            convertView.setTag(holder);
        } else {
            holder = (Rssholder) convertView.getTag();
        }
        String Title=mItems.get(position).toString();
        String Description=mItems.get(position).toString();
        holder.mTextview1.setText(Title);
        holder.mTextview2.setText(Description);
        Log.d("Parsing", "Return the views");

        return convertView;
    }

}

这是我的Item.java

 package com.example.samplerssfeed;

    public  class Item {
         public final String title;
         public final String link;
         public final String summary;
         public Item(String title,String link,String summary){
             this.title=title;
             this.link=link;
             this.summary=summary;

         }
    }

任何人都可以告诉我为什么它返回一个空值

我的日志猫条目:

06-03 04:57:14.848: D/dalvikvm(385): GC_FOR_ALLOC freed 846K, 22% free 6130K/7804K, paused 263ms, total 275ms
06-03 04:57:15.258: D/AndroidRuntime(2959): >>>>>> AndroidRuntime START com.android.internal.os.RuntimeInit <<<<<<
06-03 04:57:15.268: D/AndroidRuntime(2959): CheckJNI is ON
06-03 04:57:15.638: D/dalvikvm(2959): Trying to load lib libjavacore.so 0x0
06-03 04:57:15.678: D/dalvikvm(2959): Added shared lib libjavacore.so 0x0
06-03 04:57:15.748: D/dalvikvm(2959): Trying to load lib libnativehelper.so 0x0
06-03 04:57:15.758: D/dalvikvm(2959): Added shared lib libnativehelper.so 0x0
06-03 04:57:15.758: D/dalvikvm(2959): No JNI_OnLoad found in libnativehelper.so 0x0, skipping init
06-03 04:57:15.928: D/LightsService(385): Excessive delay setting light: 1003ms
06-03 04:57:16.438: W/RecognitionManagerService(385): no available voice recognition services found for user 0
06-03 04:57:16.778: D/dalvikvm(2959): Note: class Landroid/app/ActivityManagerNative; has 179 unimplemented (abstract) methods
06-03 04:57:16.848: D/dalvikvm(544): GC_FOR_ALLOC freed 577K, 37% free 4677K/7324K, paused 146ms, total 157ms
06-03 04:57:16.968: I/Choreographer(544): Skipped 304 frames!  The application may be doing too much work on its main thread.
06-03 04:57:27.448: D/AndroidRuntime(2968): >>>>>> AndroidRuntime START com.android.internal.os.RuntimeInit <<<<<<
06-03 04:57:27.478: D/AndroidRuntime(2968): CheckJNI is ON
06-03 04:57:27.678: D/dalvikvm(2968): Trying to load lib libjavacore.so 0x0
06-03 04:57:27.688: D/dalvikvm(2968): Added shared lib libjavacore.so 0x0
06-03 04:57:27.748: D/dalvikvm(2968): Trying to load lib libnativehelper.so 0x0
06-03 04:57:27.748: D/dalvikvm(2968): Added shared lib libnativehelper.so 0x0
06-03 04:57:27.748: D/dalvikvm(2968): No JNI_OnLoad found in libnativehelper.so 0x0, skipping init
06-03 04:57:28.338: D/dalvikvm(2968): Note: class Landroid/app/ActivityManagerNative; has 179 unimplemented (abstract) methods
06-03 04:57:29.648: E/memtrack(2968): Couldn't load memtrack module (No such file or directory)
06-03 04:57:29.648: E/android.os.Debug(2968): failed to load memtrack module: -2
06-03 04:57:30.278: D/AndroidRuntime(2968): Calling main entry com.android.commands.pm.Pm
06-03 04:57:30.448: D/AndroidRuntime(2968): Shutting down VM
06-03 04:57:30.468: D/dalvikvm(2968): Debugger has detached; object registry had 1 entries
06-03 04:57:31.728: D/AndroidRuntime(2979): >>>>>> AndroidRuntime START com.android.internal.os.RuntimeInit <<<<<<
06-03 04:57:31.738: D/AndroidRuntime(2979): CheckJNI is ON
06-03 04:57:31.928: D/dalvikvm(2979): Trying to load lib libjavacore.so 0x0
06-03 04:57:31.938: D/dalvikvm(2979): Added shared lib libjavacore.so 0x0
06-03 04:57:32.018: D/dalvikvm(2979): Trying to load lib libnativehelper.so 0x0
06-03 04:57:32.018: D/dalvikvm(2979): Added shared lib libnativehelper.so 0x0
06-03 04:57:32.028: D/dalvikvm(2979): No JNI_OnLoad found in libnativehelper.so 0x0, skipping init
06-03 04:57:32.528: D/dalvikvm(2979): Note: class Landroid/app/ActivityManagerNative; has 179 unimplemented (abstract) methods
06-03 04:57:33.858: E/memtrack(2979): Couldn't load memtrack module (No such file or directory)
06-03 04:57:33.858: E/android.os.Debug(2979): failed to load memtrack module: -2
06-03 04:57:34.518: D/AndroidRuntime(2979): Calling main entry com.android.commands.am.Am
06-03 04:57:34.688: I/ActivityManager(385): START u0 {act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10000000 cmp=com.example.samplerssfeed/.RSSActivity} from pid 2979
06-03 04:57:34.778: D/gralloc(49): Registering a buffer in the process that created it. This may cause memory ordering problems.
06-03 04:57:34.778: E/libEGL(49): called unimplemented OpenGL ES API
06-03 04:57:34.788: E/libEGL(49): called unimplemented OpenGL ES API
06-03 04:57:34.788: E/libEGL(49): called unimplemented OpenGL ES API
06-03 04:57:34.788: E/libEGL(49): called unimplemented OpenGL ES API
06-03 04:57:34.788: E/SurfaceFlinger(49): glCheckFramebufferStatusOES error 1945814895
06-03 04:57:34.788: E/SurfaceFlinger(49): got GL_FRAMEBUFFER_COMPLETE_OES error while taking screenshot
06-03 04:57:34.788: E/libEGL(49): called unimplemented OpenGL ES API
06-03 04:57:34.788: E/libEGL(49): called unimplemented OpenGL ES API
06-03 04:57:34.828: W/WindowManager(385): Screenshot failure taking screenshot for (164x246) to layer 21010
06-03 04:57:34.928: D/AndroidRuntime(2979): Shutting down VM
06-03 04:57:34.948: D/jdwp(2979): Got wake-up signal, bailing out of select
06-03 04:57:34.948: D/dalvikvm(2979): Debugger has detached; object registry had 1 entries
06-03 04:57:35.088: I/ActivityManager(385): Start proc com.example.samplerssfeed for activity com.example.samplerssfeed/.RSSActivity: pid=2990 uid=10067 gids={50067, 3003}
06-03 04:57:35.158: D/dalvikvm(2990): Not late-enabling CheckJNI (already on)
06-03 04:57:35.218: I/Choreographer(385): Skipped 47 frames!  The application may be doing too much work on its main thread.
06-03 04:57:35.538: I/Choreographer(385): Skipped 43 frames!  The application may be doing too much work on its main thread.
06-03 04:57:35.648: I/dalvikvm(2990): CheckJNI enabled: not enabling JNI app bug workarounds.
06-03 04:57:36.108: I/Choreographer(385): Skipped 66 frames!  The application may be doing too much work on its main thread.
06-03 04:57:36.228: I/Choreographer(385): Skipped 30 frames!  The application may be doing too much work on its main thread.
06-03 04:57:36.338: I/Choreographer(385): Skipped 63 frames!  The application may be doing too much work on its main thread.
06-03 04:57:36.448: I/Choreographer(385): Skipped 66 frames!  The application may be doing too much work on its main thread.
06-03 04:57:37.498: I/Choreographer(2990): Skipped 145 frames!  The application may be doing too much work on its main thread.
06-03 04:57:37.548: D/gralloc_goldfish(2990): Emulator without GPU emulation detected.
06-03 04:57:37.678: I/ActivityManager(385): Displayed com.example.samplerssfeed/.RSSActivity: +2s714ms
06-03 04:57:37.778: I/Choreographer(2990): Skipped 51 frames!  The application may be doing too much work on its main thread.
06-03 04:57:37.788: I/Choreographer(385): Skipped 39 frames!  The application may be doing too much work on its main thread.
06-03 04:57:37.908: I/Choreographer(385): Skipped 43 frames!  The application may be doing too much work on its main thread.
06-03 04:57:37.998: I/Choreographer(385): Skipped 40 frames!  The application may be doing too much work on its main thread.
06-03 04:57:38.258: D/dalvikvm(2990): GC_FOR_ALLOC freed 83K, 5% free 3128K/3276K, paused 207ms, total 213ms
06-03 04:57:41.768: D/HttpExample(2990): The response is:200
06-03 04:57:41.838: D/dalvikvm(2990): GC_FOR_ALLOC freed 373K, 12% free 3259K/3700K, paused 57ms, total 57ms
06-03 04:57:42.638: D/LightsService(385): Excessive delay setting light: 69ms
06-03 04:57:42.818: D/LightsService(385): Excessive delay setting light: 178ms
06-03 04:57:42.948: D/LightsService(385): Excessive delay setting light: 103ms
06-03 04:57:43.038: D/LightsService(385): Excessive delay setting light: 64ms
06-03 04:57:43.398: D/dalvikvm(2990): GC_FOR_ALLOC freed 487K, 15% free 3275K/3832K, paused 45ms, total 46ms
06-03 04:57:43.478: D/Parsing(2990): Calling Adapter
06-03 04:58:00.178: I/Choreographer(544): Skipped 38 frames!  The application may be doing too much work on its main thread.
06-03 04:58:35.628: D/LightsService(385): Excessive delay setting light: 82ms
06-03 04:58:35.678: D/LightsService(385): Excessive delay setting light: 54ms
06-03 04:58:35.788: D/LightsService(385): Excessive delay setting light: 69ms
06-03 04:58:35.908: D/LightsService(385): Excessive delay setting light: 102ms
06-03 05:00:01.018: D/ConnectivityService(385): Sampling interval elapsed, updating statistics ..
06-03 05:00:01.068: D/ConnectivityService(385): Done.
06-03 05:00:01.078: D/ConnectivityService(385): Setting timer for 720seconds
06-03 05:04:27.788: D/dalvikvm(944): GC_FOR_ALLOC freed 845K, 15% free 5225K/6140K, paused 66ms, total 67ms
06-03 05:04:36.198: D/dalvikvm(523): GC_FOR_ALLOC freed 591K, 17% free 3240K/3900K, paused 48ms, total 50ms

1 个答案:

答案 0 :(得分:0)

这似乎是罪魁祸首:

items = rssParser.parse(stream);

或许,parse似乎会返回null