0

我需要从服务器解析 XML 文件。有人可以帮帮我吗。

我的代码:

public class XML2Parser extends Activity {

TextView antonis1, antonis2, antonis3, tryans1, tryans2, tryans3;

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_xml2parser);
    findViewById();

    String myurl = "http://www.playhard.cu.cc/tabel.xml";
    new XMLParsing().execute(myurl);
}

private void findViewById() {
    // TODO Auto-generated method stub
    antonis1 = (TextView) findViewById(R.id.tvxml2pantonis1);
    antonis2 = (TextView) findViewById(R.id.tvxml2pantonis2);
    antonis3 = (TextView) findViewById(R.id.tvxml2pantonis3);
    tryans1 = (TextView) findViewById(R.id.tvxml2ptryans1);
    tryans2 = (TextView) findViewById(R.id.tvxml2ptryans2);
    tryans3 = (TextView) findViewById(R.id.tvxml2ptryans3);     
}

public class XMLParsing extends AsyncTask<String,Void,String>{

    @Override
    protected String doInBackground(String...url) {
        // TODO Auto-generated method stub
        try {
            String website = url[0].toString();
            URL fullurl = new URL(website);

            // mengaktifkan parse pada xml reader
            SAXParserFactory spf = SAXParserFactory.newInstance();
            SAXParser sp = spf.newSAXParser();
            XMLReader xr = sp.getXMLReader();
            XML2ParserHandler Handler = new XML2ParserHandler();
            xr.setContentHandler(Handler);
            InputSource is = new InputSource(fullurl.openStream());
            xr.parse(is);
            String information = Handler.getInfo().toString();
            return information;
        } catch (Exception e) {
            e.printStackTrace();
        }
        String error = "error";
        return error;
    }

    @Override
    protected void onPostExecute(String result) {
        // TODO Auto-generated method stub
        antonis1.setText(result);
    }
}

}

处理程序类:

package com.projectx0001.antoni;

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

public class XML2ParserHandler extends DefaultHandler{

    String testtext;

    public String getInfo(){
        return testtext;
    }

    @Override
    public void startDocument() throws SAXException {
        // TODO Auto-generated method stub
        testtext = "It's Work Just Fine!";
        super.startDocument();
    }
}

日志猫:

01-19 17:25:41.211: D/OpenGLRenderer(3294): Enabling debug mode 0
01-19 17:25:46.609: D/OpenGLRenderer(3294): Flushing caches (mode 0)
01-19 17:25:48.398: W/System.err(3294): java.net.UnknownHostException: Unable to resolve host "www.playhard.cu.cc": No address associated with hostname
01-19 17:25:48.398: W/System.err(3294):     at java.net.InetAddress.lookupHostByName(InetAddress.java:426)
01-19 17:25:48.398: W/System.err(3294):     at java.net.InetAddress.getAllByNameImpl(InetAddress.java:242)
01-19 17:25:48.398: W/System.err(3294):     at java.net.InetAddress.getAllByName(InetAddress.java:220)
01-19 17:25:48.398: W/System.err(3294):     at libcore.net.http.HttpConnection.<init>(HttpConnection.java:71)
01-19 17:25:48.398: W/System.err(3294):     at libcore.net.http.HttpConnection.<init>(HttpConnection.java:50)
01-19 17:25:48.398: W/System.err(3294):     at libcore.net.http.HttpConnection$Address.connect(HttpConnection.java:351)
01-19 17:25:48.398: W/System.err(3294):     at libcore.net.http.HttpConnectionPool.get(HttpConnectionPool.java:86)
01-19 17:25:48.398: W/System.err(3294):     at libcore.net.http.HttpConnection.connect(HttpConnection.java:128)
01-19 17:25:48.398: W/System.err(3294):     at libcore.net.http.HttpEngine.openSocketConnection(HttpEngine.java:308)
01-19 17:25:48.398: W/System.err(3294):     at libcore.net.http.HttpEngine.connect(HttpEngine.java:303)
01-19 17:25:48.398: W/System.err(3294):     at libcore.net.http.HttpEngine.sendSocketRequest(HttpEngine.java:282)
01-19 17:25:48.398: W/System.err(3294):     at libcore.net.http.HttpEngine.sendRequest(HttpEngine.java:232)
01-19 17:25:48.398: W/System.err(3294):     at libcore.net.http.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:273)
01-19 17:25:48.406: W/System.err(3294):     at libcore.net.http.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:168)
01-19 17:25:48.406: W/System.err(3294):     at java.net.URL.openStream(URL.java:462)
01-19 17:25:48.406: W/System.err(3294):     at com.projectx0001.antoni.XML2Parser$XMLParsing.doInBackground(XML2Parser.java:62)
01-19 17:25:48.406: W/System.err(3294):     at com.projectx0001.antoni.XML2Parser$XMLParsing.doInBackground(XML2Parser.java:1)
01-19 17:25:48.406: W/System.err(3294):     at android.os.AsyncTask$2.call(AsyncTask.java:264)
01-19 17:25:48.406: W/System.err(3294):     at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
01-19 17:25:48.406: W/System.err(3294):     at java.util.concurrent.FutureTask.run(FutureTask.java:137)
01-19 17:25:48.406: W/System.err(3294):     at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:208)
01-19 17:25:48.406: W/System.err(3294):     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
01-19 17:25:48.406: W/System.err(3294):     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
01-19 17:25:48.406: W/System.err(3294):     at java.lang.Thread.run(Thread.java:856)
01-19 17:25:48.406: W/System.err(3294): Caused by: libcore.io.GaiException: getaddrinfo failed: EAI_NODATA (No address associated with hostname)
01-19 17:25:48.414: W/System.err(3294):     at libcore.io.Posix.getaddrinfo(Native Method)
01-19 17:25:48.414: W/System.err(3294):     at libcore.io.ForwardingOs.getaddrinfo(ForwardingOs.java:55)
01-19 17:25:48.414: W/System.err(3294):     at java.net.InetAddress.lookupHostByName(InetAddress.java:411)
01-19 17:25:48.414: W/System.err(3294):     ... 23 more
01-19 17:25:48.469: D/dalvikvm(3294): GC_CONCURRENT freed 430K, 8% free 7011K/7559K, paused 2ms+3ms
01-19 17:25:48.734: D/OpenGLRenderer(3294): Flushing caches (mode 0)
4

1 回答 1

1

如日志:

MalformedURLException:找不到协议:[Ljava.lang.String;@413191b8

因为目前您正在尝试将 varags转换为 URL .change

 String website = url.toString();
  URL fullurl = new URL(website);

 @Override
        protected String doInBackground(String...url) {
            // TODO Auto-generated method stub
            try {
                String website = url[0].toString();
                URL fullurl = new URL(website);
                 //.....
于 2013-01-19T08:12:44.280 回答