2013-07-03 66 views
0

我正在阅读Head First Android Development Book,这是一本很棒的书,但我在第二章(第2章)中,我无法获得这个RSS应用程序的工作。所以,基本上,这不应该是最终版本,但它迄今为止所做的只是将应用程序视为空白。听起来很愚蠢,但它不应该显示任何东西,因为我必须为应用程序设置一些权限才能允许应用程序连接到互联网并下载RSS信息。我正在为这个应用程序使用4个不同的文件(但很明显,项目中有更多的文件)。Android Development RSS Feed does not work

我制作了一个Google文件夹,这样每个人都可以看到它并下载它。我使用Eclipse。

请帮助我这是一本书,但我找不到下一章,直到找到解决方案。

再次,这不应该是最终版本,该应用程序应该被视为空,因为我需要为它设置一些权限。它给了我错误,请帮助我!

我的主要问题是这行,它说“iotdHandler无法解决”。我不知道为什么这本书说我不应该大写这个词我想我应该喜欢“IotdHandler”,但它仍然给我错误。我遵循了书中的所有内容。帮我!

protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    IotdHandler handler = new IotdHandler(); 
    handler.processFeed(); 
    resetDisplay(iotdHandler.getTitle(), iotdHandler.getDate(), iotdHandler.getImage(), iotdHandler.getDescription()); 
} 

此代码是从mainActivity.java文件

请帮助我,这是快把我逼疯了!

感谢

+0

忘了RSS源。 http://www.nasa.gov/rss/image_of_the_day.rss – user2547460

回答

0

man。我觉得你的痛苦我花了两天时间在这一章找出了错误的解决方案。 拳头,最重要的是这不是书的完成版本,它是困扰着错误,所以不要觉得不好,并牢记在心。

strong text 以下是您的问题的解决方案。

在mainActivity.java

package com.example.nasadailyimage; 

import android.iotdHandler; 
import android.os.Bundle; 
import android.app.Activity; 
import android.graphics.Bitmap; 
import android.view.Menu; 
import android.widget.ImageView; 
import android.widget.TextView; 

公共类MainActivity扩展活动{

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    IotdHandler handler = new IotdHandler(); //create handler 
    handler.processFeed(); //start parsing 
    resetDisplay(iotdHandler.getTitle(), iotdHandler.getDate(), 
      iotdHandler.getImage(), iotdHandler.getDescription()); 
} 

@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    // Inflate the menu; this adds items to the action bar if it is present. 
    getMenuInflater().inflate(R.menu.main, menu); 
    return true; 
} 

private void resetDisplay(String title, String date, String imageUrl, String description) 
{ 
    TextView titleView = (TextView)findViewById(R.id.imageTitle); 
    titleView.setText(title); 

    TextView dateView = (TextView)findViewById(R.id.imageDate); 
    dateView.setText(date); 

    ImageView imageView = (ImageView)findViewById(R.id.imageDisplay); 
    Bitmap image = null; 
    imageView.setImageBitmap(image); 

    TextView descriptionView = (TextView)findViewById(R.id.imageDescription); 
    descriptionView.setText(description); 

} 

}

其次:你需要在你的src创建一个名为IotdHandler.java文件夹。 在这个文件中,你需要创建以下getter方法

package android; 

public class iotdHandler { 

public static String getDate() { 
    // TODO Auto-generated method stub 
    return null; 
} 
public static String getTitle() { 
    // TODO Auto-generated method stub 
    return null; 
} 
public static String getImage() { 
    // TODO Auto-generated method stub 
    return null; 
} 
public static String getDescription() { 
    // TODO Auto-generated method stub 
    return null; 
} 

}

我所能

+0

请再次检查您的答案。不要浪费别人的时间 – sar

0

的主要问题是,头先作家是对象而言有点语无伦次命名为

  1. 什么对象声明没有使用。 (IotdHandler made, iotdHandler used。)java和因此android区分大小写。
  2. 有时signature parameter of methods不同于所声明方法的那个 。它应该是(String,String,Bitmap, StringBuffer),但调用者使用(String,String,String,String)。

我建议你读做编程练习Wrox的或Apress出版的Android书籍,然后去HeadFirst coding ideas to have robust code

-1

你必须要到位以下修正,你在你的mainactivity.java

resetDisplay(handler.getTitle(), handler.getDate(), handler.getImage(), handler.getDescription()); 
0

调用resetDisplay()方法,我知道这个职位是比较旧的,但也许我的回答将帮助旁边的人可能在这里找到自己,特别是因为没有被接受的答案。

如上所述,本书充满了错误,可以让android和java的新手像我一样,有点疯狂。经过几天的谷歌搜索和阅读,我设法让这个工作,所以在这里。

我不确定OP使用的是什么版本的Android,但我认为本书中的代码存在的问题之一是来自Honeycomb(3.x)和以上版本的附加问题,不允许使用昂贵在UI线程上的操作。了解更多关于它:

http://www.androiddesignpatterns.com/2012/06/app-force-close-honeycomb-ics.html 

对于这个问题,你需要使用的AsyncTask一个单独的线程运行潜在的昂贵的操作。

请注意,在捕获异常的代码中,我只是将异常错误打印到titleView TextView中,以便更容易地找出问题所在。

运行完AsyncTask中的代码后,更新显示。

startElement方法似乎也不正确,因为如果您在XML编辑器中查看RSS源详细信息,则图像所需的URL位于“enclosure”下。

我的代码如下,请留下任何问题的意见;请注意我也是一个初学者,所以这可能是一个更好的方法。

package neill.nasadailyimage; 

import java.io.*; 
import java.net.HttpURLConnection; 
import java.net.URL; 

import javax.xml.parsers.*; 

import org.xml.sax.*; 
import org.xml.sax.helpers.DefaultHandler; 

import android.support.v7.app.ActionBarActivity; 
import android.graphics.Bitmap; 
import android.graphics.BitmapFactory; 
import android.os.AsyncTask; 
import android.os.Bundle; 
import android.view.Menu; 
import android.view.MenuItem; 
import android.widget.ImageView; 
import android.widget.TextView; 

public class NasaDailyImage extends ActionBarActivity 
{ 
    IotdHandler handler = new IotdHandler(); // Create handler 

    @Override 
    protected void onCreate(Bundle savedInstanceState) 
    { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_nasa_daily_image); 

     handler.processFeed();  
    } 

    public void ResetDisplay() { 

     String title = handler.getTitle(); 
     String date = handler.getDate(); 
     String description = handler.getDescription().toString(); 

     resetDisplay(title, date, handler.getImage(), description); 
    } 

    private void resetDisplay(String title, String date, Bitmap image, String description) 
    { 
     try { 

      TextView titleView = (TextView) findViewById(R.id.imageTitle); 
      titleView.setText(title); 

      TextView dateView = (TextView)findViewById(R.id.imageDate); 
      dateView.setText(date); 

      ImageView imageView = (ImageView)findViewById(R.id.imageDisplay); 
      imageView.setImageBitmap(image); 

      TextView descriptionView = (TextView)findViewById(R.id.imageDescription); 
      descriptionView.setText(description); 

      } catch (Exception e) { 
       TextView titleView = (TextView) findViewById(R.id.imageTitle); 
       titleView.setText(e.toString()); 
      } 
    } 


    @Override 
    public boolean onCreateOptionsMenu(Menu menu) 
    { 
     // Inflate the menu; this adds items to the action bar if it is present. 
     getMenuInflater().inflate(R.menu.nasa_daily_image, menu); 
     return true; 
    } 

    @Override 
    public boolean onOptionsItemSelected(MenuItem item) 
    { 
     // Handle action bar item clicks here. The action bar will 
     // automatically handle clicks on the Home/Up button, so long 
     // as you specify a parent activity in AndroidManifest.xml. 
     int id = item.getItemId(); 
     if (id == R.id.action_settings) { 
      return true; 
     } 
     return super.onOptionsItemSelected(item); 
    } 

    public class IotdHandler extends DefaultHandler 
    { 
     private String url = "http://www.nasa.gov/rss/image_of_the_day.rss"; 

     private boolean inTitle = false; 
     private boolean inDescription = false; 
     private boolean inItem = false; 
     private boolean inDate = false; 

     private Bitmap image = null; 
     private String title = null; 
     private String date = null; 
     private StringBuffer description = new StringBuffer(); 

     public XMLReader reader = null; 

     private Bitmap getBitmap(String url) 
     { 
      try 
      { 
       HttpURLConnection connection = (HttpURLConnection) new URL(url).openConnection(); 
       connection.setDoInput(true); 
       connection.connect(); 

       InputStream input = connection.getInputStream(); 
       Bitmap bitmap = BitmapFactory.decodeStream(input); 

       input.close(); 

       return bitmap; 

      } 
      catch (IOException ioe) 
      { 
       TextView titleView = (TextView) findViewById(R.id.imageTitle); 
       titleView.setText(ioe.toString()); 

       return null; 
      } 
     } 

     public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException 
     { 
       if (localName.equals("enclosure")) { 
        image = getBitmap(attributes.getValue("url").toString()); 
       } 

       if (localName.startsWith("item")) { inItem = true; } 
       else 
       {     
        if (inItem) { 
         if (localName.equals("title")) { inTitle = true; } 
         else { inTitle = false; } 

         if (localName.equals("description")) { inDescription = true; } 
         else { inDescription = false; } 

         if (localName.equals("pubDate")) { inDate = true; } 
         else { inDate = false; } 
        } 
       }   
     } 

     public void characters(char ch[], int start, int length) 
     { 
       String chars = (new String(ch).substring(start, start + length)); 

       if (inTitle && title == null) { title = chars; } 

       if (inDescription) { description.append(chars); } 

       if (inDate && date == null) { date = chars; } 

     } 

     private class ProcessFeedTask extends AsyncTask<String, Void, InputStream> 
     {  
      @Override 
      protected InputStream doInBackground(String... params) 
      { 
       String url = params[0]; 

       InputStream inputStream = null; 

       try 
       {   
        inputStream = new URL(url).openStream(); 

        reader.parse(new InputSource(inputStream)); 

       } 
       catch (Exception e) 
       { 
        TextView titleView = (TextView) findViewById(R.id.imageTitle); 
        titleView.setText(e.toString()); 
       } 

       return inputStream; 
      } 

      @Override 
      protected void onPostExecute(InputStream result) 
      { 
       super.onPostExecute(result); 

       if (result != null) { 
        ResetDisplay(); 
       } 
      } 
     } 

     public void processFeed() 
     { 
      try 
      { 
       SAXParserFactory factory = SAXParserFactory.newInstance(); 

       SAXParser parser = factory.newSAXParser(); 

       reader = parser.getXMLReader(); 
       reader.setContentHandler(this); 

       new ProcessFeedTask().execute(url); 

      } 
      catch (Exception e) 
      { 
       TextView titleView = (TextView) findViewById(R.id.imageTitle); 
       titleView.setText(e.toString()); 
      } 
     } 

     public String getTitle() { return title ; } 
     public String getDate() { return date ; } 
     public String getDescription() { return description.toString() ; } 
     public Bitmap getImage() { return image ; } 
    } 
} 

如果你在运行一个仿真器的代码,给它一段时间的工作,我觉得仿真器的上网速度比较慢。

希望能帮助那里的人。

干杯。

尼尔