检索RSS提要,并在文本视图显示它并在、视图、提要、文本

2023-09-08 10:18:22 作者:The sea of tears(海泪)

我想获得这个链接工作。 我想要检索的RSS提要,并在文本视图显示它,

我已经试过了code,但似乎并不奏效。 我想有一些错误的code,但我不知道在哪里有它出了问题。 我是新来的机器人。

我需要帮助,请。

感谢。

下面是我的code。

MainActivity.java

 公共类MainActivity延伸活动{

TextView的PSI;

类MyWeather {
    字符串称号;
字符串描述;


公共字符串的toString(){

 回归\正

  +条件:+标题+\ N
  +说明书+\ N的;

}
}

 / **第一次创建活动时调用。 * /
@覆盖
公共无效的onCreate(包savedInstanceState){
    super.onCreate(savedInstanceState);
    的setContentView(R.layout.activity_main);
    磅=(TextView中)findViewById(R.id.psi);


    新MyAsyncTask()执行()。
}


     公共类MyAsyncTask扩展的AsyncTask<虚空,虚空,虚空> {
            ProgressDialog对话框;
            MyWeather weatherResult;

            @覆盖
            在preExecute保护无效(){
                // TODO自动生成方法存根
                对话框=新ProgressDialog(MainActivity.this);
                dialog.setMessage(请稍候...);
                dialog.show();
                super.on preExecute();
            }

            @覆盖
            保护无效doInBackground(虚空...... PARAMS){
                串weatherString = QueryYahooWeather();
                文档weatherDoc = convertStringToDocument(weatherString);

                weatherResult = parseWeather(weatherDoc);
                文档DEST = NULL;

                DocumentBuilderFactory的dbFactory =
                  DocumentBuilderFactory.newInstance();
                DocumentBuilder的解析器;

                尝试 {
                 解析器= dbFactory.newDocumentBuilder();
               DEST = parser.parse(新ByteArrayInputStream的(src.getBytes()));
              }赶上(E1的ParserConfigurationException){
               e1.printStackTrace();
               Toast.makeText(MainActivity.this,
                   e1.toString(),Toast.LENGTH_LONG).show();
              }赶上(的SAXException E){
               e.printStackTrace();
               Toast.makeText(MainActivity.this,
                   e.toString(),Toast.LENGTH_LONG).show();
              }赶上(IOException异常E){
               e.printStackTrace();
               Toast.makeText(MainActivity.this,
                   e.toString(),Toast.LENGTH_LONG).show();
              }

                返回DEST;
               }

            私人MyWeather parseWeather(文件weatherDoc){
                MyWeather myWeather =新MyWeather();

                  //<描述>雅虎!天气纽约,NY< /描述>
                  //myWeather.description = srcDoc.getElementsByTagName(说明)
                    //.item(0)
                    // getTextContent();


                  。节点locationNode = srcDoc.getElementsByTagName(项目)项目(0);
                  myWeather.title = locationNode.getAttributes()
                  .getNamedItem(标题)
                  .getNodeValue()
                  的ToString();
                myWeather.description = locationNode.getAttributes()
                  .getNamedItem(说明)
                  .getNodeValue()
                  的ToString();

                  返回myWeather;
                 }

            }

            }

            私人文件convertStringToDocument(字符串weatherString){
                // TODO自动生成方法存根
                返回null;
            }

            私人字符串QueryYahooWeather(){
                // TODO自动生成方法存根
                字符串QRESULT =;
                  字符串查询字符串=app2.nea.gov.sg/data/rss/nea_psi.xml;

                  HttpClient的HttpClient的=新DefaultHttpClient();
                     HTTPGET HTTPGET =新HTTPGET(查询字符串);

                     尝试 {
                      HttpEntity httpEntity = httpClient.execute(HTTPGET).getEntity();

                      如果(httpEntity!= NULL){
                       InputStream中的InputStream = httpEntity.getContent();
                       读者=新的InputStreamReader(InputStream的);
                       BufferedReader中的BufferedReader =新的BufferedReader(中);
                       StringBuilder的StringBuilder的=新的StringBuilder();

                       字符串stringReadLine = NULL;

                       而((stringReadLine = bufferedreader.readLine())!= NULL){
                        stringBuilder.append(stringReadLine +\ N);
                       }

                       QRESULT = stringBuilder.toString();
                      }

                }赶上(ClientProtocolException E){
                 e.printStackTrace();
                 Toast.makeText(MainActivity.this,
                     e.toString(),Toast.LENGTH_LONG).show();
                }赶上(IOException异常E){
                 e.printStackTrace();
                 Toast.makeText(MainActivity.this,
                     e.toString(),Toast.LENGTH_LONG).show();
                }

                     返回QRESULT;


                }

            @覆盖
            保护无效onPostExecute(无效的结果){
                // TODO自动生成方法存根

                dialog.dismiss();
                psi.setText(weatherResult.toString());
                super.onPostExecute(结果);
            }
 

activity_main.xml

 < LinearLayout中的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    的xmlns:工具=htt​​p://schemas.android.com/tool​​s
    机器人:layout_width =match_parent
    机器人:layout_height =match_parent
    机器人:方向=垂直
    工具:上下文=MainActivity。>





        <的TextView
            机器人:ID =@ + ID /磅
            机器人:layout_width =FILL_PARENT
            机器人:layout_height =WRAP_CONTENT/>


< / LinearLayout中>
 
excel分列 怎么变文本格式显示而不是数值格式

解决方案

使用这种codeS ...!

 进口的java.net.URL;
进口的java.util.ArrayList;
进口javax.xml.parsers.DocumentBuilder中;
进口javax.xml.parsers.DocumentBuilderFactory中;

进口org.w3c.dom.Document中;
进口org.w3c.dom.Element中;
进口org.w3c.dom.Node中;
进口org.w3c.dom.NodeList;
进口org.xml.sax.InputSource中;

进口android.app.Activity;
进口android.content.Context;
进口android.os.Bundle;
进口android.text.Html;
进口android.view.LayoutInflater;
进口android.view.View;
进口android.view.ViewGroup;
进口android.widget.BaseAdapter;
进口android.widget.ListView;
进口android.widget.TextView;

公共类XMLParsingDOMExample延伸活动{

    ArrayList的<字符串>标题;
    ArrayList的<字符串>描述;
    ItemAdapter适配器1;
    @覆盖
    公共无效的onCreate(包savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.main);

        ListView控件列表=(ListView控件)findViewById(R.id.list);
        标题=新的ArrayList<字符串>();
        说明=新的ArrayList<字符串>();

        尝试 {

            网址URL =新的URL(
                    http://app2.nea.gov.sg/data/rss/nea_psi.xml);
            DocumentBuilderFactory的DBF = DocumentBuilderFactory.newInstance();
            DocumentBuilder的DB = dbf.newDocumentBuilder();
            文档的文档= db.parse(新的InputSource(url.openStream()));
            doc.getDocumentElement()归()。

            节点列表节点列表= doc.getElementsByTagName(项目);
            的for(int i = 0; I< nodeList.getLength();我++){

                节点node = nodeList.item(ⅰ);

                元素fstElmnt =(元)节点;
                节点列表名单= fstElmnt.getElementsByTagName(标题);
                元件nameElement =(元件)nameList.item(0);
                名单= nameElement.getChildNodes();

                title.add(+((节点)nameList.item(0))getNodeValue());

                节点列表websiteList = fstElmnt.getElementsByTagName(说明);
                元件websiteElement =(元件)websiteList.item(0);
                websiteList = websiteElement.getChildNodes();

                description.add(+((节点)websiteList.item(0))getNodeValue());

            }
        }赶上(例外五){
            的System.out.println(XML帕兴Excpetion =+ E);
        }

        适配器1 =新ItemAdapter(本);
        list.setAdapter(适配器1);
    }


    类ItemAdapter扩展了BaseAdapter {

        最后LayoutInflater mInflater;

        私有类ViewHolder {
            公众的TextView title_text;
            公众的TextView des_text;
        }

        公共ItemAdapter(上下文的背景下){
            // TODO自动生成构造函数存根
            超();
            mInflater =(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        }

        // @覆盖
        公众诠释getCount将(){
            返回title.size();
        }

        // @覆盖
        公共对象的getItem(INT位置){
            返回的位置;
        }

        // @覆盖
        众长getItemId(INT位置){
            返回的位置;
        }

        // @覆盖
        公共查看getView(最终诠释的立场,观点convertView,ViewGroup中父){
            查看查看= convertView;
            最后ViewHolder持有人;
            如果(convertView == NULL){
                鉴于= mInflater.inflate(R.layout.mainpage_listitem_activity,父母,假);
                持有人=新ViewHolder();
                holder.title_text =(TextView中)view.findViewById(R.id.title_text);
                holder.des_text =(TextView中)view.findViewById(R.id.des_text);

                view.setTag(保持器);
            } 其他 {
                支架=(ViewHolder)view.getTag();
            }

            holder.title_text.setText(+ title.get(位置));

            holder.des_text.setText(+ Html.fromHtml(description.get(位置)));

        返回查看;
        }
    }
}
 

main.xml中

 < XML版本=1.0编码=UTF-8&GT?;
< LinearLayout中的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    机器人:方向=垂直
    机器人:layout_width =FILL_PARENT
    机器人:layout_height =FILL_PARENT
    >


    <的ListView
        机器人:ID =@ + ID /列表
        机器人:layout_width =match_parent
        机器人:layout_height =WRAP_CONTENT
                >

    < / ListView控件>
< / LinearLayout中>
 

mainpage_listitem_activity.xml

 < XML版本=1.0编码=UTF-8&GT?;
< LinearLayout中的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    机器人:layout_width =FILL_PARENT
    机器人:layout_height =FILL_PARENT
    机器人:方向=垂直
     >

            <的TextView
                机器人:ID =@ + ID / title_text
                机器人:layout_width =WRAP_CONTENT
                机器人:layout_height =WRAP_CONTENT
                机器人:文本=称号
                机器人:layout_margin =5DP
                机器人:TEXTSIZE =22dp
                机器人:文字颜色=#FFFFFF/>

             <的TextView
                机器人:ID =@ + ID / des_text
                机器人:layout_width =WRAP_CONTENT
                机器人:layout_height =WRAP_CONTENT
                机器人:重力=中心
                机器人:文本=说明
                机器人:layout_margin =5DP
                机器人:TEXTSIZE =18dp
                机器人:文字颜色=#FFFFFF/>

< / LinearLayout中>
 

I would like to get this link " " working. I want to retrieve the RSS feed and display it in a text view,

I've tried out the code but doesn't seems to be working. I guess there is something wrong with the code but I'm not sure where has it gone wrong. I'm new to android.

I need help, please.

Thanks.

Here are my code.

MainActivity.java

  public class MainActivity extends Activity {

TextView psi;

class MyWeather{
    String title;
String description;


public String toString(){

 return "\n- " 

  + "Condition: " + title + "\n"
  + description +"\n";

}
}

 /** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    psi = (TextView)findViewById(R.id.psi);


    new MyAsyncTask().execute();
}


     public class MyAsyncTask extends AsyncTask<Void, Void, Void> {
            ProgressDialog dialog;
            MyWeather weatherResult;

            @Override
            protected void onPreExecute() {
                // TODO Auto-generated method stub
                dialog = new ProgressDialog(MainActivity.this);
                dialog.setMessage("Please wait...");
                dialog.show();
                super.onPreExecute();
            }

            @Override
            protected Void doInBackground(Void... params) {
                String weatherString = QueryYahooWeather();
                Document weatherDoc = convertStringToDocument(weatherString);

                weatherResult = parseWeather(weatherDoc);
                Document dest = null;

                DocumentBuilderFactory dbFactory =
                  DocumentBuilderFactory.newInstance();
                DocumentBuilder parser;

                try {
                 parser = dbFactory.newDocumentBuilder();
               dest = parser.parse(new ByteArrayInputStream(src.getBytes()));
              } catch (ParserConfigurationException e1) {
               e1.printStackTrace();
               Toast.makeText(MainActivity.this,
                   e1.toString(), Toast.LENGTH_LONG).show();
              } catch (SAXException e) {
               e.printStackTrace();
               Toast.makeText(MainActivity.this,
                   e.toString(), Toast.LENGTH_LONG).show();
              } catch (IOException e) {
               e.printStackTrace();
               Toast.makeText(MainActivity.this,
                   e.toString(), Toast.LENGTH_LONG).show();
              }

                return dest;
               }

            private MyWeather parseWeather(Document weatherDoc) {
                MyWeather myWeather = new MyWeather();

                  //<description>Yahoo! Weather for New York, NY</description>
                  //myWeather.description = srcDoc.getElementsByTagName("description")
                    //.item(0)
                    //.getTextContent();


                  Node locationNode = srcDoc.getElementsByTagName("item").item(0);
                  myWeather.title = locationNode.getAttributes()
                  .getNamedItem("title")
                  .getNodeValue()
                  .toString();
                myWeather.description = locationNode.getAttributes()
                  .getNamedItem("description")
                  .getNodeValue()
                  .toString();

                  return myWeather;
                 }

            }

            }

            private Document convertStringToDocument(String weatherString) {
                // TODO Auto-generated method stub
                return null;
            }

            private String QueryYahooWeather() {
                // TODO Auto-generated method stub
                String qResult = "";
                  String queryString = "app2.nea.gov.sg/data/rss/nea_psi.xml";

                  HttpClient httpClient = new DefaultHttpClient();
                     HttpGet httpGet = new HttpGet(queryString);

                     try {
                      HttpEntity httpEntity = httpClient.execute(httpGet).getEntity();

                      if (httpEntity != null){
                       InputStream inputStream = httpEntity.getContent();
                       Reader in = new InputStreamReader(inputStream);
                       BufferedReader bufferedreader = new BufferedReader(in);
                       StringBuilder stringBuilder = new StringBuilder();

                       String stringReadLine = null;

                       while ((stringReadLine = bufferedreader.readLine()) != null) {
                        stringBuilder.append(stringReadLine + "\n");
                       }

                       qResult = stringBuilder.toString();
                      }

                } catch (ClientProtocolException e) {
                 e.printStackTrace();
                 Toast.makeText(MainActivity.this,
                     e.toString(), Toast.LENGTH_LONG).show();
                } catch (IOException e) {
                 e.printStackTrace();
                 Toast.makeText(MainActivity.this,
                     e.toString(), Toast.LENGTH_LONG).show();
                }

                     return qResult;


                }

            @Override
            protected void onPostExecute(Void result) {
                // TODO Auto-generated method stub

                dialog.dismiss();
                psi.setText(weatherResult.toString());
                super.onPostExecute(result);
            }

activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity" >





        <TextView
            android:id="@+id/psi"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content" />


</LinearLayout>

解决方案

Use this Codes...!

    import java.net.URL;
import java.util.ArrayList;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

public class XMLParsingDOMExample extends Activity {

    ArrayList<String> title;
    ArrayList<String> description;
    ItemAdapter adapter1;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        ListView list = (ListView) findViewById(R.id.list);
        title = new ArrayList<String>();
        description = new ArrayList<String>();  

        try {

            URL url = new URL(
                    "http://app2.nea.gov.sg/data/rss/nea_psi.xml");
            DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
            DocumentBuilder db = dbf.newDocumentBuilder();
            Document doc = db.parse(new InputSource(url.openStream()));
            doc.getDocumentElement().normalize();

            NodeList nodeList = doc.getElementsByTagName("item");
            for (int i = 0; i < nodeList.getLength(); i++) {

                Node node = nodeList.item(i);       

                Element fstElmnt = (Element) node;
                NodeList nameList = fstElmnt.getElementsByTagName("title");
                Element nameElement = (Element) nameList.item(0);
                nameList = nameElement.getChildNodes();         

                title.add(""+ ((Node) nameList.item(0)).getNodeValue());

                NodeList websiteList = fstElmnt.getElementsByTagName("description");
                Element websiteElement = (Element) websiteList.item(0);
                websiteList = websiteElement.getChildNodes();

                description.add(""+ ((Node) websiteList.item(0)).getNodeValue());           

            }
        } catch (Exception e) {
            System.out.println("XML Pasing Excpetion = " + e);
        }

        adapter1 = new ItemAdapter(this);
        list.setAdapter(adapter1);
    }


    class ItemAdapter extends BaseAdapter {

        final LayoutInflater mInflater;

        private class ViewHolder {
            public TextView title_text;
            public TextView des_text;
        }

        public ItemAdapter(Context context) {
            // TODO Auto-generated constructor stub
            super();
            mInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);     
        }

        //@Override
        public int getCount() {
            return title.size();
        }

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

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

        //@Override
        public View getView(final int position, View convertView, ViewGroup parent) {
            View view = convertView;
            final ViewHolder holder;
            if (convertView == null) {
                view = mInflater.inflate(R.layout.mainpage_listitem_activity, parent, false);
                holder = new ViewHolder();
                holder.title_text = (TextView) view.findViewById(R.id.title_text);
                holder.des_text = (TextView) view.findViewById(R.id.des_text);

                view.setTag(holder);
            } else {
                holder = (ViewHolder) view.getTag();
            }

            holder.title_text.setText(""+title.get(position));

            holder.des_text.setText(""+Html.fromHtml(description.get(position)));

        return view;
        }
    }
}

main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >


    <ListView 
        android:id="@+id/list"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
                >

    </ListView>
</LinearLayout>

mainpage_listitem_activity.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
     >

            <TextView 
                android:id="@+id/title_text"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"     
                android:text="title"
                android:layout_margin="5dp"
                android:textSize="22dp"
                android:textColor="#FFFFFF"/>   

             <TextView 
                android:id="@+id/des_text"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"     
                android:gravity="center"
                android:text="description "
                android:layout_margin="5dp"
                android:textSize="18dp"
                android:textColor="#FFFFFF"/>   

</LinearLayout>