[英]Can't Parse localhost XML file (NullPointerException)
I tried to make XML parser in android, the xml file is on my localhost server. 我试图在android中创建XML解析器,该xml文件在我的本地主机服务器上。 I got NullPointerException error when I set the URL to my localhost-ed xml file, but when I upload the xml file to dropbox, my parser is working.
将URL设置为由localhost编辑的xml文件时出现NullPointerException错误,但是当我将xml文件上传到Dropbox时,解析器正在工作。
the error message: 错误消息:
...
Caused by: java.lang.NullPointerException
at java.io.StringReader.<init>(StringReader.java:47)
at com.learn.dhemas.franchise.XMLParser.getDomElement(XMLParser.java:74)
at com.learn.dhemas.franchise.fragmentLowongan$getDataOrder.doInBackground(fragmentLowongan.java:72)
...
XMLParser,java: XMLParser,java:
public class XMLParser {
// constructor
public XMLParser() {
}
/**
* Getting XML from URL making HTTP request
* @param url string
* */
public String getXmlFromUrl(String url) {
String xml = null;
try {
// defaultHttpClient
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpGet httpGet = new HttpGet(url);
HttpResponse httpResponse = httpClient.execute(httpGet);
HttpEntity httpEntity = httpResponse.getEntity();
xml = EntityUtils.toString(httpEntity);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
// return XML
return xml;
}
/**
* Getting XML DOM element
* @param XML string
* */
public Document getDomElement(String xml){
Document doc = null;
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
try {
DocumentBuilder db = dbf.newDocumentBuilder();
InputSource is = new InputSource();
is.setCharacterStream(new StringReader(xml));
doc = db.parse(is);
} catch (ParserConfigurationException e) {
Log.e("Error: ", e.getMessage());
return null;
} catch (SAXException e) {
Log.e("Error: ", e.getMessage());
return null;
} catch (IOException e) {
Log.e("Error: ", e.getMessage());
return null;
}
return doc;
}
/** Getting node value
* @param elem element
*/
public final String getElementValue( Node elem ) {
Node child;
if( elem != null){
if (elem.hasChildNodes()){
for( child = elem.getFirstChild(); child != null; child = child.getNextSibling() ){
if( child.getNodeType() == Node.TEXT_NODE ){
return child.getNodeValue();
}
}
}
}
return "";
}
/**
* Getting node value
* @param Element node
* @param key string
* */
public String getValue(Element item, String str) {
NodeList n = item.getElementsByTagName(str);
return this.getElementValue(n.item(0));
}
}
and lines of code from my activity (fragment class) : 和我活动中的代码行(片段类) :
...
@Override
protected Void doInBackground(Void... params) {
//Call parser
XMLParser parser = new XMLParser();
//String xml = parser.getXmlFromUrl("https://drive.google.com/uc?export=download&id=0B5bM7szIFKrpY2lPRHdCUUNscjA");
String xml = parser.getXmlFromUrl("http://localhost/GetData.xml");
Document doc = parser.getDomElement(xml);
...
Any help will be apreciated 任何帮助将不胜感激
So the problem is when I point to localhost from the app then it will be the localhost on the emulator, instead of localhost in my web server. 所以问题是当我从应用程序指向localhost时,它将是模拟器上的localhost,而不是Web服务器中的localhost。 To solve this I just need to create a virtual local network and give my computer an IP address so I can access the IP from emulator.
为了解决这个问题,我只需要创建一个虚拟本地网络并为我的计算机提供一个IP地址,以便可以从仿真器访问该IP。
您可以尝试在浏览器中打开URL: http://localhost/GetData.xml
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.