2013-01-19 59 views
0

我需要从服务器解析XML文件。有人可以帮帮我吗。Android中的SAX解析器错误

mycode的:

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(); 
    } 
} 

logcat的:

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) 

回答

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); 
       //..... 
+0

我已将代码更改为 String website = url [0] .toString(); URL fullurl = new URL(website); 但是,尽管括号仍然返回一个异常(返回“错误”),分析器仍然无法正常工作。 – Moonhint

+0

logcat给我:01-19 15:23:00.521:W/System.err(2386):java.net.UnknownHostException:无法解析主机“www.playhard.cu.cc”:没有与主机名关联的地址 – Moonhint

+0

01-19 15:23:00.521:W/System.err(2386):\t at java.net.InetAddress.getAllByName(InetAddress.java:220) 01-19 15:23:00.528:W/System.err 2386):\t at libcore.net.http.HttpConnection。 (HttpConnection.java:71) 01-19 15:23:00.528:W/System.err(2386):\t at libcore.net.http.HttpConnection。 (HttpConnection.java:50) 01-19 15:23:00.528:W/System.err(2386):\t at libcore.net.http.HttpConnection $ Address.connect(HttpConnection.java:351) – Moonhint