2012-02-29 69 views
1

我正在嘗試閱讀an RSS feed。我已經嘗試了很多使用下面的代碼併成功的鏈接,但是當我嘗試使用上面的鏈接時,我得到一個FileNotFoundException。什麼可能導致這種情況,以及我如何解決我的代碼?在Android中閱讀RSS時發生FileNotFoundException

EfficientAdapter類:

package com.xmlparse; 

import java.io.IOException; 
import java.io.InputStream; 
import java.net.MalformedURLException; 
import java.net.URL; 
import java.util.ArrayList; 
import android.app.Activity; 
import android.content.Context; 
import android.graphics.Bitmap; 
import android.graphics.BitmapFactory; 
import android.graphics.drawable.Drawable; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.BaseAdapter; 
import android.widget.ImageView; 
import android.widget.TextView; 

public class EfficientAdapter extends BaseAdapter 
{ 
    private Activity activity; 
    private ArrayList<Post> data; 
    private static LayoutInflater inflater = null; 
    //public ImageLoader imageLoader; 
    ViewHolder holder; 

    EfficientAdapter(Activity a, ArrayList<Post> d) { 
      activity = a; 
      data = d; 
      inflater = (LayoutInflater) activity 
          .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
    //  imageLoader = new ImageLoader(activity.getApplicationContext()); 

    } 

    @Override 
    public int getCount() { 
      return data.toArray().length; 
    } 

    @Override 
    public Object getItem(int position) { 
      return position; 
    } 

    @Override 
    public long getItemId(int position) { 
      return position; 
    } 

    public static class ViewHolder { 
      public TextView label; 
      public TextView addr; 
      public ImageView image; 
    } 

    @Override 
    public View getView(int position, View convertView, ViewGroup parent) { 
      View vi = convertView; 

      if (convertView == null) { 
        vi = inflater.inflate(R.layout.row, null); 
        holder = new ViewHolder(); 
        holder.label = (TextView) vi.findViewById(R.id.title); 
        holder.addr = (TextView) vi.findViewById(R.id.details); 
        holder.image = (ImageView) vi.findViewById(R.id.thumb); 
        vi.setTag(holder); 
      } else 
        holder = (ViewHolder) vi.getTag(); 

      holder.label.setText(data.get(position).getTitle()); 
    //  holder.addr.setText(data.get(position).getPubDate()); 
      holder.addr.setText(data.get(position).getDescription()); 
      imageLoader.DisplayImage((data.get(position).getThumbnail()), activity, 
          holder.image, 72, 72); 
      URL url = null; 
      try { 
        url = new URL((data.get(position).getThumbnail())); 
      } catch (MalformedURLException e) { 
        // TODO Auto-generated catch block 
        e.printStackTrace(); 
      } 
      InputStream content = null; 
      try { 
        content = (InputStream)url.getContent(); 
      } catch (IOException e) { 
        // TODO Auto-generated catch block 
        e.printStackTrace(); 
      } 
      Drawable d = Drawable.createFromStream(content , "src"); 
      Bitmap mIcon1 = null; 
      try { 
        mIcon1 = 
          BitmapFactory.decodeStream(url.openConnection().getInputStream()); 
      } catch (IOException e) { 
        e.printStackTrace(); 
      } 
      holder.image.setImageBitmap(Bitmap.createScaledBitmap(mIcon1, 72, 72, false)); 


      return vi; 
    } 

} 

Post類:

package com.xmlparse; 
public class Post { 

    private String title; 
    private String thumbnail; 
    private String url; 
    private String description; 
    private String pubDate; 

    public String getTitle() { 
      return title; 
    } 

    public void setTitle(String title) { 
      this.title = title; 
    } 

    public String getThumbnail() { 
      return thumbnail; 
    } 

    public void setThumbnail(String thumbnail) { 
      this.thumbnail = thumbnail; 
    } 

    public String getUrl() { 
      return url; 
    } 

    public void setUrl(String url) { 
      this.url = url; 
    } 

    public void setDescription(String description) { 
      this.description = description; 
    } 

    public String getDescription() { 
      return description; 
    } 

    public void setPubDate(String pubDate) { 
      this.pubDate = pubDate; 
    } 

    public String getPubDate() { 
      return pubDate; 
    } 

    } 

xmlparsemain活動類:

package com.xmlparse; 

import java.io.InputStreamReader; 
import java.net.MalformedURLException; 
import java.net.URL; 
import java.net.URLConnection; 
import java.util.ArrayList; 
import java.util.HashMap; 

import javax.xml.parsers.SAXParser; 
import javax.xml.parsers.SAXParserFactory; 

import org.xml.sax.Attributes; 
import org.xml.sax.InputSource; 
import org.xml.sax.SAXException; 
import org.xml.sax.XMLReader; 
import org.xml.sax.helpers.DefaultHandler; 

import android.app.Activity; 
import android.app.ProgressDialog; 
import android.content.Intent; 
import android.net.Uri; 
import android.os.AsyncTask; 
import android.os.Bundle; 
import android.view.View; 
import android.widget.AdapterView; 
import android.widget.AdapterView.OnItemClickListener; 
import android.widget.ListView; 

public class xmlparsemain extends Activity { 
    /** Called when the activity is first created. */ 
    ListView lv1; 
    ProgressDialog ShowProgress; 
    public ArrayList<Post> PostList = new ArrayList<Post>(); 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
      super.onCreate(savedInstanceState); 
      setContentView(R.layout.main); 

      lv1 = (ListView) findViewById(R.id.listView1); 

      ShowProgress = ProgressDialog.show(xmlparsemain.this, "", 
          "Loading. Please wait...", true); 
      new loadingTask().execute("http://screenrant.com/mobile/movie-reviews-feed.php"); 

      lv1.setOnItemClickListener(new OnItemClickListener() { 
        public void onItemClick(AdapterView<?> parent, View view, 
            int position, long id) { 

          Intent intent = new Intent(Intent.ACTION_VIEW).setData(Uri 
              .parse(PostList.get(position).getUrl())); 
          startActivity(intent); 

        } 
      }); 

    } 

    class loadingTask extends AsyncTask<String, Void, String> { 

      protected String doInBackground(String... urls) { 

        SAXHelper sh = null; 
        try { 
          sh = new SAXHelper(urls[0]); 
        } catch (MalformedURLException e) { 
          e.printStackTrace(); 
        } 
        sh.parseContent(""); 
        return ""; 

      } 

      protected void onPostExecute(String s) { 
        lv1.setAdapter(new EfficientAdapter(xmlparsemain.this, PostList)); 
        ShowProgress.dismiss(); 

      } 
    } 

    class SAXHelper { 
      public HashMap<String, String> userList = new HashMap<String, String>(); 
      private URL url2; 

      public SAXHelper(String url1) throws MalformedURLException { 
        this.url2 = new URL(url1); 
      } 

      public RSSHandler parseContent(String parseContent) { 
        RSSHandler df = new RSSHandler(); 
        try { 

          SAXParserFactory spf = SAXParserFactory.newInstance(); 
          SAXParser sp = spf.newSAXParser(); 
          XMLReader xr = sp.getXMLReader(); 
          xr.setContentHandler(df); 

         /*  URLConnection con = url2.openConnection(); 
          con.setConnectTimeout(20000); 
         InputSource is = new InputSource(new InputStreamReader(con.getInputStream())); 
         xr.parse(is);*/ 
          xr.parse(new InputSource(url2.openStream())); 
        } catch (Exception e) { 
          e.printStackTrace(); 
        } 
        return df; 
      } 
    } 

    class RSSHandler extends DefaultHandler { 

      private Post currentPost = new Post(); 
      StringBuffer chars = new StringBuffer(); 

      @Override 
      public void startElement(String uri, String localName, String qName, 
          Attributes atts) { 

        chars = new StringBuffer(); 
        if (localName.equalsIgnoreCase("item")) { 

        } 
      } 

      @Override 
      public void endElement(String uri, String localName, String qName) 
          throws SAXException { 

        if (localName.equalsIgnoreCase("title") 
            && currentPost.getTitle() == null) { 
          currentPost.setTitle(chars.toString()); 

        } 
        if (localName.equalsIgnoreCase("pubDate") 
            && currentPost.getPubDate() == null) { 
          currentPost.setPubDate(chars.toString()); 

        } 
        if (localName.equalsIgnoreCase("description") 
            && currentPost.getDescription() == null) { 
          currentPost.setDescription(chars.toString()); 

        } 
        if (localName.equalsIgnoreCase("thumbnail") 
            && currentPost.getThumbnail() == null) { 
          currentPost.setThumbnail(chars.toString()); 

        } 
        if (localName.equalsIgnoreCase("link") 
            && currentPost.getUrl() == null) { 
          currentPost.setUrl(chars.toString()); 
        } 

        if (localName.equalsIgnoreCase("item")) { 
          PostList.add(currentPost); 
          currentPost = new Post(); 
        } 

      } 

      @Override 
      public void characters(char ch[], int start, int length) { 
        chars.append(new String(ch, start, length)); 
      } 

    } 

} 

我logcat的報告:

02-29 15:20:44.183: W/System.err(2470): java.io.FileNotFoundException: http://screenrant.com/mobile/movie-news-feed.php 
+1

可以張貼錯誤的完整logcat的? – MikeIsrael 2012-02-29 09:27:37

+0

它將顯示在logcat 02-29 14:58:51.104:E/RSSParser(2212):java.io.FileNotFoundException:http://screenrant.com/mobile/movie-news-feed.php – Khan 2012-02-29 09:29:37

+0

後整個logcat如果你希望人們能夠幫助你,而不僅僅是一條線,那麼錯誤就是這樣。它應該列出所有生效的行,包括代碼中拋出錯誤的行 – MikeIsrael 2012-02-29 09:34:11

回答

2

嘗試使用HttpConnection的,而不是URLConnection的,如下:

InputStream callWebErvice(String serviceURL){ 
     // http get client 
      HttpClient client=new DefaultHttpClient(); 
      HttpGet getRequest=new HttpGet(); 

      try { 
       // construct a URI object 
       getRequest.setURI(new URI(serviceURL)); 
      } catch (URISyntaxException e) { 
       Log.e("URISyntaxException", e.toString()); 
      } 

      // buffer reader to read the response 
      BufferedReader in=null; 
      // the service response 
      HttpResponse response=null; 
      try { 
       // execute the request 
       response = client.execute(getRequest); 
      } catch (ClientProtocolException e) { 
       Log.e("ClientProtocolException", e.toString()); 
      } catch (IOException e) { 
       Log.e("IO exception", e.toString()); 
      } 
      if(response!=null) 
       return response.getEntity().getContent(); 
      else 
       return null; 

    } 

和變線xr.parse(new InputSource(url2.openStream()));

xr.parse(new InputSource(callWebErvice(url2)));