首先, 解析本地的XML 参考 http://www.cnblogs.com/iMirror/p/4122082.html
学习Tomcat搭建本地服务器 参考
在Tomcat默认访问目录 webapps/ROOT下新建XMLParserTest文件夹, 并新建persons.xml
peosons..xml
打开Tomcat , 查询IP地址, 打开该 persons.xml 可以通过访问 “http://192.168.1.111:8080/XMLParserTest/persons.xml”
浏览器打开之后的截图
Genymotion打开截图
这就是说明访问该URL可以得到返回XML
修改本地访问XML程序中的XMLParseActivity.java如下
package mirror.android.XMLParseActivity; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.net.HttpURLConnection; import java.net.URL; import org.apache.http.HttpConnection; import org.apache.http.HttpEntity; import org.apache.http.HttpResponse; import org.apache.http.client.HttpClient; import org.apache.http.client.methods.HttpGet; import org.apache.http.impl.client.DefaultHttpClient; import mirror.android.Services.DOMParseService; import mirror.android.Services.PULLParseService; import mirror.android.Services.SAXParseService; import mirror.android.resolvexml.R; import android.app.Activity; import android.os.Bundle; public class XMLParseActivity extends Activity{ @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_xmlparse); new Thread(new Runnable() { @Override public void run() { try { String urlString = "http://192.168.1.111:8080/XMLParserTest/persons.xml"; URL url = new URL(urlString); HttpURLConnection con = (HttpURLConnection)url.openConnection(); if(con.getResponseCode() == 200){ InputStream in = con.getInputStream(); DOMParseService domParseService = new DOMParseService(); domParseService.getPersonsByParseXML(in); SAXParseService saxParseService = new SAXParseService(); saxParseService.getPersonsByParseXML(in2); PULLParseService pullParseService = new PULLParseService(); pullParseService.getPersonsByParseXML(in3); SAXParseService saxParseService = new SAXParseService(); saxParseService.getPersonsByParseXML(in); } } catch (Exception e) { e.getStackTrace(); } } }).start(); /*try { InputStream in1 = getAssets().open("persons.xml"); InputStream in2 = getAssets().open("persons.xml"); InputStream in3 = getAssets().open("persons.xml"); DOMParseService domParseService = new DOMParseService(); domParseService.getPersonsByParseXML(in1); SAXParseService saxParseService = new SAXParseService(); saxParseService.getPersonsByParseXML(in2); PULLParseService pullParseService = new PULLParseService(); pullParseService.getPersonsByParseXML(in3); } catch (IOException e) { e.printStackTrace(); }*/ } }
时间: 2024-11-02 11:09:18