0

`次のコードは、baseurl(Webサービス)からtextViewにデータを解析する必要があります 問題: * IOExceptionが発生します..Webサービスからは確かですが、プログラムはWebサービスからデータを取得するのに役立つエラーを表示しますか?*

public class NewParsActivity extends Activity {
    /** Called when the activity is first created. */
    static final String baseUrl="http://www.androidpeople.com/wp-content/uploads/2010/06/example.xml";

    TextView tv;
    EditText ed,ed2;
    Button b;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        tv=(TextView)findViewById(R.id.text);
        ed=(EditText)findViewById(R.id.edt);
        b=(Button) findViewById(R.id.button1);
        ed2=(EditText) findViewById(R.id.editText1);

        try{



    SAXParserFactory spf=SAXParserFactory.newInstance();
    SAXParser sp=spf.newSAXParser();
    XMLReader xr=sp.getXMLReader();
    URL link=new URL(baseUrl);
    handlXml doingWork=new handlXml();
    xr.setContentHandler(doingWork);
    xr.parse(new InputSource(link.openStream()));
    String information=doingWork.getInformation();
    tv.setText(information);
        }catch(Exception e){
         tv.setText("Error");
        }



//
public class handlXml  extends DefaultHandler {
   One  item=new One();

   public String getInformation(){
       return item.DataToString();

   }
    @Override


    public void startElement(String uri, String localName, String qName,
                Attributes attributes) throws SAXException {
            // TODO Auto-generated method stub
            if(localName.equals("website")){
                String uniN=attributes.getValue("category");
                item.setName(uniN);
            }
        }

    }

//
<i>
public class One {

    String UName;
    public void setName(String n){
    UName=n;    
    }


public String DataToString()
{
    return "In"+UName;
}
}
<i>
4

2 に答える 2

0

このチュートリアルを見てください。android.iでのxml解析に関する優れたガイドがあります。これでクエリが解決されることを願っています。

ここに私はそのtutrialコンテンツを入れています

このxmlファイルを解析する必要があると言います http://api.androidhive.info/pizza/?format=xml

これがこのxmlを解析するためのクラスです

public class AndroidXMLParsingActivity extends ListActivity {

// All static variables
static final String URL = "http://api.androidhive.info/pizza/?format=xml";
// XML node keys
static final String KEY_ITEM = "item"; // parent node
static final String KEY_ID = "id";
static final String KEY_NAME = "name";
static final String KEY_COST = "cost";
static final String KEY_DESC = "description";

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

    ArrayList<HashMap<String, String>> menuItems = new ArrayList<HashMap<String, String>>();

    XMLParser parser = new XMLParser();
    String xml = parser.getXmlFromUrl(URL); // getting XML
    Document doc = parser.getDomElement(xml); // getting DOM element

    NodeList nl = doc.getElementsByTagName(KEY_ITEM);
    // looping through all item nodes <item>
    for (int i = 0; i < nl.getLength(); i++) {
        // creating new HashMap
        HashMap<String, String> map = new HashMap<String, String>();
        Element e = (Element) nl.item(i);
        // adding each child node to HashMap key => value
        map.put(KEY_ID, parser.getValue(e, KEY_ID));
        map.put(KEY_NAME, parser.getValue(e, KEY_NAME));
        map.put(KEY_COST, "Rs." + parser.getValue(e, KEY_COST));
        map.put(KEY_DESC, parser.getValue(e, KEY_DESC));

        // adding HashList to ArrayList
        menuItems.add(map);
    }

    // Adding menuItems to ListView
    ListAdapter adapter = new SimpleAdapter(this, menuItems,
            R.layout.list_item,
            new String[] { KEY_NAME, KEY_DESC, KEY_COST }, new int[] {
                    R.id.name, R.id.desciption, R.id.cost });

    setListAdapter(adapter);

    // selecting single ListView item
    ListView lv = getListView();
            // listening to single listitem click
    lv.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View view,
                int position, long id) {
            // getting values from selected ListItem
            String name = ((TextView) view.findViewById(R.id.name)).getText().toString();
            String cost = ((TextView) view.findViewById(R.id.cost)).getText().toString();
            String description = ((TextView) view.findViewById(R.id.desciption)).getText().toString();

            // Starting new intent
            Intent in = new Intent(getApplicationContext(), SingleMenuItemActivity.class);
            in.putExtra(KEY_NAME, name);
            in.putExtra(KEY_COST, cost);
            in.putExtra(KEY_DESC, description);
            startActivity(in);

        }
    });
}

}

これに関する完全なガイドは、このリンクから見つけることができます

http://www.androidhive.info/2011/11/android-xml-parsing-tutorial/

于 2012-06-25T09:25:27.753 に答える
0

APIが正常に機能している同じ例。

XMLParsingExample.java

public class XMLParsingExample extends Activity {

    /** Create Object For SiteList Class */
    SitesList sitesList = null;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        /** Create a new layout to display the view */
        LinearLayout layout = new LinearLayout(this);
        layout.setOrientation(1);

        /** Create a new textview array to display the results */
        TextView name[];
        TextView website[];
        TextView category[];

        try {

            /** Handling XML */
            SAXParserFactory spf = SAXParserFactory.newInstance();
            SAXParser sp = spf.newSAXParser();
            XMLReader xr = sp.getXMLReader();

            /** Send URL to parse XML Tags */
            URL sourceUrl = new URL(
                    "http://www.androidpeople.com/wp-content/uploads/2010/06/example.xml");

            /** Create handler to handle XML Tags ( extends DefaultHandler ) */
            MyXMLHandler myXMLHandler = new MyXMLHandler();
            xr.setContentHandler(myXMLHandler);
            xr.parse(new InputSource(sourceUrl.openStream()));

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

        /** Get result from MyXMLHandler SitlesList Object */
        sitesList = MyXMLHandler.sitesList;

        /** Assign textview array lenght by arraylist size */
        name = new TextView[sitesList.getName().size()];
        website = new TextView[sitesList.getName().size()];
        category = new TextView[sitesList.getName().size()];

        /** Set the result text in textview and add it to layout */
        for (int i = 0; i < sitesList.getName().size(); i++) {
            name[i] = new TextView(this);
            name[i].setText("Name = "+sitesList.getName().get(i));
            website[i] = new TextView(this);
            website[i].setText("Website = "+sitesList.getWebsite().get(i));
            category[i] = new TextView(this);
            category[i].setText("Website Category = "+sitesList.getCategory().get(i));

            layout.addView(name[i]);
            layout.addView(website[i]);
            layout.addView(category[i]);
        }

        /** Set the layout view to display */
        setContentView(layout);

    }
}

XMLParsingサンプルプロジェクト

これが私の出力ウィンドウです-

出力

于 2012-06-25T10:20:42.147 に答える