如何创建一个XML API,这将使数据以我的Andr​​oid应用程序 [英] how to create an xml api that will give data to my android app

查看:78
本文介绍了如何创建一个XML API,这将使数据以我的Andr​​oid应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗯,我有一个Android应用程序,需要与数据更新中... 我知道该怎么做在Android终端的XML解析... 但是当我试图通过follwing XML文件解析的数据(一)用我的应用程序的StringBuilder(二)我没有得到的数据...

well i have an android app that needs to be updated with data... i know what to do on android-end for xml parsing... but when i try to parse the data through the follwing xml file(a) using stringbuilder(b) i am not getting data in my app...

(一)data.xml中

(a)data.xml

<?xml version="1.0" encoding="utf-8"?>
<document version="first">
<stuff code="firststuff">
<item1 build="first">This is first item.</item1>
<item2 build="second">This is second item.</item2>
<item3 build="third">This is third item.</item3>
</stuff>
<stuff code="secondtstuff">
<item1 build="first">This is first item.</item1>
<item2 build="second">This is second item.</item2>
<item3 build="third">This is third item.</item3>
</stuff>
<stuff code="thirdstuff">
<item1 build="first">This is first item.</item1>
<item2 build="second">This is second item.</item2>
<item3 build="third">This is third item.</item3>
</stuff>
</document>

(二),我在我的Andr​​oid应用程序中使用的字符串...

(b) string that i use in my android app...

http://www.xyz.com/data.xml?build=second

下面的3个文件(C)

Java的code主(四)HandlingXMLStuff(五)XMLDataCollected

java code below in 3 files (c)Main (d)HandlingXMLStuff (e)XMLDataCollected

(三)主要

package com.xyz.xmlpar1;

import java.net.URL;

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

import org.xml.sax.InputSource;
import org.xml.sax.XMLReader;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class Main extends Activity implements OnClickListener {
    static final String BaseURL="http://www.xyz.com/data.xml?code=";
    Button b;
    TextView tv;
    EditText et1;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        et1=(EditText) findViewById(R.id.editText1);
        b=(Button) findViewById(R.id.button1);
        tv=(TextView) findViewById(R.id.textView1);
        b.setOnClickListener(this);

    }

    @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;
    }

    @Override
    public void onClick(View v) {

        // TODO Auto-generated method stub
        String item=et1.getText().toString();
        StringBuilder URL=new StringBuilder(BaseURL);
        URL.append(item);
        String fullURL=URL.toString();
        try{
            URL website=new URL(fullURL);
            SAXParserFactory spf=SAXParserFactory.newInstance();
            SAXParser sp=spf.newSAXParser();
            XMLReader xr=sp.getXMLReader();
            HandlingXMLStuff doingWork=new HandlingXMLStuff();
            xr.setContentHandler(doingWork);
            xr.parse(new InputSource(website.openStream()));
            String information=doingWork.getInformation();
            tv.setText(information);

        }catch(Exception e){
            tv.setText("error");
        }


    }

}

(四)HandlingXMLStuff

(d)HandlingXMLStuff

package com.xyz.xmlpar1;

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

public class HandlingXMLStuff extends DefaultHandler {

    XMLDataCollected info=new XMLDataCollected();

    public String getInformation(){
        return info.datatoString();
    }

    @Override
    public void startElement(String uri, String localName, String qName,
            Attributes attributes) throws SAXException {
        // TODO Auto-generated method stub
        if(localName.equals("code")){
            String element=attributes.getValue("build");
            info.setData(element);
        }
    }

}

(五)XMLDataCollected

(e)XMLDataCollected

package com.xyz.xmlpar1;

public class XMLDataCollected {

    String datac=null;

    public void setData(String c){
        datac=c;
    }

    public String datatoString(){
        return datac;
    }

}

要获取第二的数据项。

我要去哪里错了???

where am i going wrong ???

推荐答案

您应该按照我贴在这里这个问题重复教程:<一href="http://stackoverflow.com/questions/14417475/parsed-xml-data-not-retrieving-in-android/14418003#14418003">Parsed XML数据未检索机器人

You should follow the tutorial that I posted on the duplicate of this question here: Parsed XML data not retrieving in android

虽然该教程并没有表现出越来越属性。

Although that tutorial does not show getting attributes.

总之,你的的startElement 应该是这个样子:

In short, your startElement should look something like this:

@Override
    public void startElement(String uri, String localName, String qName,
            Attributes attributes) throws SAXException {

          if(localName.equals("stuff")){
            String element=attributes.getValue("code");
            info.setData(element); //element will hold "firststuff" on the first execution

        }
    }

您将需要一个单独的如果为您要相互匹配的名字。

You will need a separate if for each name you are trying to match.

在通用的XML文件被称为像这样(用你的命名约定):

Xml files in general are referred to like this (using your naming convention):

&LT;的localName的attributeName =的AttributeValue&GT; textValue&LT; /的localName&GT; 所以如果上述声明的意志处理行&LT;东西code =firststuff&GT; &LT; ...secondstuff&GT; 等等,但你必须增加更多的处理的每个不同的的行。

<localName attributeName="attributeValue">textValue</localName> So the if statement above will handle the lines <stuff code="firststuff"> <..."secondstuff"> etc. but you will have to add more to handle each different line.

我强烈建议你按照教程获得这个成就,虽然,你一定会通过试验和错误中学到很多东西。

I strongly suggest you follow a tutorial to get this accomplished, although, you will definitely learn a lot through trial and error.

这篇关于如何创建一个XML API,这将使数据以我的Andr​​oid应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆