2010-11-28 14 views
2

こんにちは皆さん 私はAndroid開発には比較的新しく、ニュースサイト用のRSSリーダーを作成しています。 私が持っている問題は、画像を取得したいRSSフィードのサイトが「<」です。img src = "http ......" " アンドロイドのコードは何ですか? ... 2クラス... RssItemとRssItem表示機能Android Rss画像の問題

public class RssItem { 

private String title; 
private String description; 
private Date pubDate; 
private String link; 
private static ImageView image; 

public RssItem(String title, String description,ImageView image, Date pubDate, String link) { 
    this.title = title; 
    this.description = description; 
    RssItem.image = image; 
    this.pubDate = pubDate; 
    this.link = link; 
} 

public String getTitle() { 
    return title; 
} 

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

public String getDescription() { 
    return description; 
} 

public void setDescription(String description) { 
    this.description = description; 
} 
public ImageView getImage(ImageView image) { 
     return this.image = image; 
}  
public void setImage(ImageView image) { 
      this.image = image;  
} 
public Date getPubDate() { 
    return pubDate; 
} 

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

public String getLink() { 
    return link; 
} 

public void setLink(String link) { 
    this.link = link; 
} 

@Override 
public String toString() { 

    SimpleDateFormat sdf = new SimpleDateFormat("MM/dd - hh:mm:ss"); 

    String result = getTitle() + " (" + sdf.format(this.getPubDate()) + ")"; 
    return result; 
} 

public static ArrayList<RssItem> getRssItems(String feedUrl) { 

    ArrayList<RssItem> rssItems = new ArrayList<RssItem>(); 

    try { 
     //open an URL connection make GET to the server and 
     //take xml RSS data 
     URL url = new URL(feedUrl); 
     HttpURLConnection conn = (HttpURLConnection) url.openConnection(); 

     if (conn.getResponseCode() == HttpURLConnection.HTTP_OK) { 
      InputStream is = conn.getInputStream(); 

      //DocumentBuilderFactory, DocumentBuilder are used for 
      //xml parsing 
      DocumentBuilderFactory dbf = DocumentBuilderFactory 
        .newInstance(); 
      DocumentBuilder db = dbf.newDocumentBuilder(); 

      //using db (Document Builder) parse xml data and assign 
      //it to Element 
      Document document = db.parse(is); 
      Element element = document.getDocumentElement(); 

      //take rss nodes to NodeList 
      NodeList nodeList = element.getElementsByTagName("item"); 

      if (nodeList.getLength() > 0) { 
       for (int i = 0; i < nodeList.getLength(); i++) { 

        //take each entry (corresponds to <item></item> tags in 
        //xml data 

        Element entry = (Element) nodeList.item(i); 

        Element _titleE = (Element) entry.getElementsByTagName(
          "title").item(0); 
        Element _descriptionE = (Element) entry 
          .getElementsByTagName("description").item(0); 
        Element _imageE = (Element) entry 
          .getElementsByTagName("image").item(0); 
        Element _pubDateE = (Element) entry 
          .getElementsByTagName("pubDate").item(0); 
        Element _linkE = (Element) entry.getElementsByTagName(
          "link").item(0); 

        String _title = _titleE.getFirstChild().getNodeValue(); 
        String _description = _descriptionE.getFirstChild().getNodeValue(); 

        // ImageView image = (ImageView)findViewbyId(R.id.MyImage); 
        // Bitmap bitmap = BitmapFactory.decodeStream((InputStream)new URL(feedUrl).getContent()); 
        // image.setImageBitmap(bitmap); 
        //} catch (MalformedURLException e) { 

        /*try { 
          //where imageUrl is what you pulled out from the rss feed 
          Bitmap bitmap = BitmapFactory.decodeStream((InputStream)new URL(feedUrl).getContent()); 
          image.setImageBitmap(bitmap); 
         } catch (MalformedURLException e) { 
         //log exception here 
         } catch (IOException e) { 
          //log exception here 
         } 

        */ 

        Date _pubDate = new Date(_pubDateE.getFirstChild().getNodeValue()); 
        String _link = _linkE.getFirstChild().getNodeValue(); 

        //create RssItemObject and add it to the ArrayList 
        RssItem rssItem = new RssItem(_title, _description, image, 
          _pubDate, _link); 

        rssItems.add(rssItem); 
       } 
      } 

     } 
    } catch (Exception e) { 
     e.printStackTrace(); 
    } 
    return rssItems; 
}  

が、私は

私もレイアウト上に表示されているImageViewのを作成SRC画像要素を配置する方法がわかりませんxml

RSS項目表示器を示し、Rクラスは、私は

public class RssItemDisplayer extends Activity { 
@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.rss_item_displayer); 

    RssItem selectedRssItem = com.AndroidRSSReader.AndroidRSSReader.selectedRssItem; 
    //Bundle extras = getIntent().getExtras(); 
    TextView titleTv = (TextView)findViewById(R.id.titleTextView); 
    TextView contentTv = (TextView)findViewById(R.id.contentTextView); 
    ImageView image=(ImageView)findViewById(R.id.MyImage); 

    String title = ""; 
    SimpleDateFormat sdf = new SimpleDateFormat("MM/dd - hh:mm:ss"); 
    title = "\n" + selectedRssItem.getTitle() + " (" 
    + sdf.format(selectedRssItem.getPubDate()) + ")\n\n"; 

    String content = ""; 
    content += selectedRssItem.getDescription() + "\n" 
      + selectedRssItem.getLink(); 

    titleTv.setText(title); 
    contentTv.setText(content); 

    image=selectedRssItem.getImage(image); 
    try { 
      String feedUrl = null; 
     //where imageUrl is what you pulled out from the rss feed 
      Bitmap bitmap = BitmapFactory.decodeStream((InputStream)new URL(feedUrl).getContent()); 
      image.setImageBitmap(bitmap); 
     } catch (MalformedURLException e) { 
     //log exception here 
     } catch (IOException e) { 
      //log exception here 
     } 

logcatは、ビットマップは、私が何を入れるべきです

11-29 00:13:44.593: WARN/System.err(2997): java.net.MalformedURLException: Protocol not found: 
11-29 00:13:44.593: WARN/System.err(2997):  at java.net.URL.<init>(URL.java:275) 
11-29 00:13:44.593: WARN/System.err(2997):  at java.net.URL.<init>(URL.java:159) 
11-29 00:13:44.593: WARN/System.err(2997):  at com.AndroidRSSReader.RssItem.getRssItems(RssItem.java:92) 
11-29 00:13:44.593: WARN/System.err(2997):  at com.AndroidRSSReader.AndroidRSSReader.refressRssList(AndroidRSSReader.java:301) 
11-29 00:13:44.593: WARN/System.err(2997):  at com.AndroidRSSReader.AndroidRSSReader.onCreate(AndroidRSSReader.java:229) 
11-29 00:13:44.593: WARN/System.err(2997):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047) 
11-29 00:13:44.593: WARN/System.err(2997):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627) 
11-29 00:13:44.593: WARN/System.err(2997):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679) 
11-29 00:13:44.593: WARN/System.err(2997):  at android.app.ActivityThread.access$2300(ActivityThread.java:125) 
11-29 00:13:44.603: WARN/System.err(2997):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033) 
11-29 00:13:44.603: WARN/System.err(2997):  at android.os.Handler.dispatchMessage(Handler.java:99) 
11-29 00:13:44.613: WARN/System.err(2997):  at android.os.Looper.loop(Looper.java:123) 
11-29 00:13:44.613: WARN/System.err(2997):  at android.app.ActivityThread.main(ActivityThread.java:4627) 
11-29 00:13:44.613: WARN/System.err(2997):  at java.lang.reflect.Method.invokeNative(Native Method) 
11-29 00:13:44.613: WARN/System.err(2997):  at java.lang.reflect.Method.invoke(Method.java:521) 
11-29 00:13:44.613: WARN/System.err(2997):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:876) 
11-29 00:13:44.613: WARN/System.err(2997):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:634) 
11-29 00:13:44.613: WARN/System.err(2997):  at dalvik.system.NativeStart.main(Native Method) 

コードで取得されていることRSSアイテムのラインに私にwarninsを与えていますこれらの2つのことは、画像を荒らし、特にRSSアイテムクラス とにかく助けてくれれば助かります

+0

それはあなたが「何を意味明確ではないのですが、私は置く方法がわかりませんsrcイメージ要素 "と呼びます。あなたはこれをより良く説明できますか?また、あなたの質問のいくつかは、必要なときにコードとしてフォーマットされていないため、読みづらいものです。 – elevine

+0

rssフィードにはこの形式の画像があります DroidAjax

+0

問題は何ですか?私の推測では、画像URLを取得してアンドロイドに表示する.widget.ImageView。それは正しいですか? – elevine

答えて

3

あなたのRssItemクラスはhav e。次:

private static ImageView image; 

でそれを置き換えます。あなたのgetRssItems方法で

private String imageUrl; 

を、imageUrlための値を取得するために_imageEを使用しています。次に、後述のようにimageUrlを使用します。

ImageViewはデバイスにローカルに保存されているイメージのみを表示するため、リモートURLに設定することはできません。一つの解決策はここで見つけることができます:に

titleTv.setText(title); 
image.setImageURI(uri); 
contentTv.setText(content); 

::だからAndroid, Make an image at a URL equal to ImageView's image 、あなたから変更したいでしょう

titleTv.setText(title); 
contentTv.setText(content); 
try { 
    //where imageUrl is what you pulled out from the rss feed 
    Bitmap bitmap = BitmapFactory.decodeStream((InputStream)new URL(imageUrl).getContent()); 
    image.setImageBitmap(bitmap); 
} catch (MalformedURLException e) { 
//log exception here 
} catch (IOException e) { 
    //log exception here 
} 
+1

私は自分のコードにあなたの答えを入れてみましたが、それはビットマップにそれを変更...と私はそれを実装する方法がわかりません。私は新しいURL(imageUrl)と言う部分に、私はfeedUrlを入れたと思います... – DroidAjax

+0

私の元の答えにいくつかのサンプルコードを追加しました – elevine

+0

ええ、これはRSSのディプレイヤークラスですそれが正しいとは思わない他のクラスではRSSアイテムはエラーなしですが、logcatには警告があります...画像が実際にレイアウト(Imageview)に表示されるRss Displayerクラスでは、URLは必須ですもう一度セットしてください.... – DroidAjax