11-06 23:12:44.940: E/Trace(8751): error opening trace file: No such file or directory(2)
11-06 23:12:51.345: E/Error:(8751): expected: /hr read: body (position:END_TAG </body>@6:8 in java.io.StringReader@431887b8)
11-06 23:12:51.360: E/AndroidRuntime(8751): FATAL EXCEPTION: AsyncTask #1
11-06 23:12:51.360: E/AndroidRuntime(8751): java.lang.RuntimeException: An error occured while executing doInBackground()
11-06 23:12:51.360: E/AndroidRuntime(8751): at android.os.AsyncTask$3.done(AsyncTask.java:299)
11-06 23:12:51.360: E/AndroidRuntime(8751): at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
11-06 23:12:51.360: E/AndroidRuntime(8751): at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
11-06 23:12:51.360: E/AndroidRuntime(8751): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
11-06 23:12:51.360: E/AndroidRuntime(8751): at java.util.concurrent.FutureTask.run(FutureTask.java:137)
11-06 23:12:51.360: E/AndroidRuntime(8751): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
11-06 23:12:51.360: E/AndroidRuntime(8751): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
11-06 23:12:51.360: E/AndroidRuntime(8751): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
11-06 23:12:51.360: E/AndroidRuntime(8751): at java.lang.Thread.run(Thread.java:856)
11-06 23:12:51.360: E/AndroidRuntime(8751): Caused by: java.lang.NullPointerException
11-06 23:12:51.360: E/AndroidRuntime(8751): at com.ambro.app.Update$connection.doInBackground(Update.java:39)
11-06 23:12:51.360: E/AndroidRuntime(8751): at com.ambro.app.Update$connection.doInBackground(Update.java:1)
11-06 23:12:51.360: E/AndroidRuntime(8751): at android.os.AsyncTask$2.call(AsyncTask.java:287)
11-06 23:12:51.360: E/AndroidRuntime(8751): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
11-06 23:12:51.360: E/AndroidRuntime(8751): ... 5 more
XML文件位于:http://www.lookedpath.tk/apps/firstapp/version.xml
更新Java代码:
public class Update extends Activity {
private TextView testo2;
@Override
public void onCreate(Bundle savedInstanceState) {
setContentView(R.layout.update);
super.onCreate(savedInstanceState);
testo2= (TextView) findViewById(R.id.textView2);
}
public void goToUpdate (View view) {
System.out.println("ciao");
new connection().execute();
}
public class connection extends AsyncTask<Void, Void, Boolean> {
protected Boolean doInBackground(Void... params) {
boolean updated=false;
String lastversion=null;
Element e=null;
final String URL = "http://www.lookedpath.tk/apps/firstapp/version.xml";
final String VERSION = "version";
final String APPLICATION = "application";
XMLParser parser = new XMLParser();
String xml = parser.getXmlFromUrl(URL); // getting XML
Document doc = parser.getDomElement(xml); // getting DOM element
NodeList nl = doc.getElementsByTagName(APPLICATION);
for (int i=0;i<nl.getLength();i++) {
e = (Element) nl.item(0);
lastversion = parser.getValue(e, VERSION); // name child value
}
String actver = getString(R.string.version);
if(actver==lastversion) updated=true;
return updated;
}
protected void onPostExecute(Boolean... result) {
if(result[0]==false){
testo2.setText(R.string.newversion);
} else {
testo2.setText(R.string.nonewversion);
}
}
};
}
因此,这是xmlparser.java文件:
public class XMLParser {
public String getXmlFromUrl(String url) {
String xml = null;
try {
// defaultHttpClient
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
HttpResponse httpResponse = httpClient.execute(httpPost);
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;
}
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 DOM
return doc;
}
public String getValue(Element item, String str) {
NodeList n = item.getElementsByTagName(str);
return this.getElementValue(n.item(0));
}
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 "";
}
}
logcat显示错误在更新类的第39行:
NodeList nl = doc.getElementsByTagName(APPLICATION);
但我无法解决这个问题。有人可以帮助我吗?
更新: 我已经修复了错误405,但我仍然遇到空指针异常。我发现程序运行正常,但由于此异常从未进入post execute方法。