Android asynchronous OS exception

I am making an application that needs to get an XML file from a URL and parse it. I always find this log cat (PS: I cannot find the correct sintax code highlighting for google prettify):

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 can be found at: http://www.lookedpath.tk/apps/firstapp/version.xml

code update.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); } } }; } 

so this is the xmlparser.java file:

 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 says the error is on line 39 of the update class:

 NodeList nl = doc.getElementsByTagName(APPLICATION); 

but I can not solve the problem. can someone help me?

UPDATE: I fixed error 405, but I still have a null pointer exception. I found that the program is working correctly, but never introduces the post post method due to this exception.

+4
source share
6 answers

I tried your code, no problem, that's fine. The problem is in the link.

In a browser, it looks like

 <?xml version="1.0" encoding="UTF-8"?> <menu> <application> <version>1.5</version> </application> </menu> 

But when I print the xml variable, it gives me

 <html> <head><title>405 Not Allowed</title></head> <body bgcolor="white"> <center><h1>405 Not Allowed</h1></center> <hr><center>nginx</center> </body> </html> 

and then in the dom variable you got null, and when you try to find an element in the nl variable that is null, you got an error ..,.

I tried my code using some other XML feed, it works fine ..,.

+3
source

Hey, look at my Library .

You can easily manipulate XML with it;)

0
source

You can check this link for parsing xml from xml. http://www.androidhive.info/2012/02/android-custom-listview-with-image-and-text/

In your activity, you are developing AsyncTask , but you forgot to override the onPreExecute method. See this example on how to develop AsyncTask in your business.

 package com.androidhive.jsonparsing; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.UnsupportedEncodingException; import java.util.ArrayList; import java.util.HashMap; import org.apache.http.HttpEntity; import org.apache.http.HttpResponse; import org.apache.http.client.ClientProtocolException; import org.apache.http.client.methods.HttpPost; import org.apache.http.impl.client.DefaultHttpClient; import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; import android.app.ListActivity; import android.app.ProgressDialog; import android.content.Intent; import android.os.AsyncTask; import android.os.Bundle; import android.util.Log; import android.view.View; import android.widget.AdapterView; import android.widget.AdapterView.OnItemClickListener; import android.widget.ListAdapter; import android.widget.ListView; import android.widget.SimpleAdapter; import android.widget.TextView; public class AndroidJSONParsingActivity extends ListActivity { // url to make request private static String url = "http://api.androidhive.info/contacts/"; // JSON Node names private static final String TAG_CONTACTS = "contacts"; private static final String TAG_ID = "id"; private static final String TAG_NAME = "name"; private static final String TAG_EMAIL = "email"; private static final String TAG_ADDRESS = "address"; private static final String TAG_GENDER = "gender"; private static final String TAG_PHONE = "phone"; private static final String TAG_PHONE_MOBILE = "mobile"; private static final String TAG_PHONE_HOME = "home"; private static final String TAG_PHONE_OFFICE = "office"; static InputStream is = null; static JSONObject jObj = null; static String json = ""; // contacts JSONArray JSONArray contacts = null; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); new GetEventsTask().execute(""); } protected class GetEventsTask extends AsyncTask<String, Integer, ArrayList<HashMap<String, String>>> { protected ArrayList<HashMap<String, String>> contactList; private final ProgressDialog dialog = new ProgressDialog( AndroidJSONParsingActivity.this); //PreExecute Method protected void onPreExecute() { this.dialog.setMessage("Loading, Please Wait.."); this.dialog.setCancelable(false); this.dialog.show(); } //doInBackground Method @Override protected ArrayList<HashMap<String, String>> doInBackground( String... params) { contactList = new ArrayList<HashMap<String, String>>(); // Making HTTP request try { // defaultHttpClient DefaultHttpClient httpClient = new DefaultHttpClient(); HttpPost httpPost = new HttpPost(url); HttpResponse httpResponse = httpClient.execute(httpPost); HttpEntity httpEntity = httpResponse.getEntity(); is = httpEntity.getContent(); BufferedReader reader = new BufferedReader( new InputStreamReader(is, "iso-8859-1"), 8); StringBuilder sb = new StringBuilder(); String line = null; while ((line = reader.readLine()) != null) { sb.append(line + "\n"); } is.close(); json = sb.toString(); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } catch (ClientProtocolException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } // try parse the string to a JSON object try { jObj = new JSONObject(json); Log.i("json objects",""+json); } catch (JSONException e) { Log.e("JSON Parser", "Error parsing data " + e.toString()); } try { // Getting Array of Contacts contacts = jObj.getJSONArray(TAG_CONTACTS); // looping through All Contacts for (int i = 0; i < contacts.length(); i++) { JSONObject c = contacts.getJSONObject(i); // Storing each json item in variable String id = c.getString(TAG_ID); String name = c.getString(TAG_NAME); String email = c.getString(TAG_EMAIL); String address = c.getString(TAG_ADDRESS); String gender = c.getString(TAG_GENDER); // Phone number is agin JSON Object JSONObject phone = c.getJSONObject(TAG_PHONE); String mobile = phone.getString(TAG_PHONE_MOBILE); String home = phone.getString(TAG_PHONE_HOME); String office = phone.getString(TAG_PHONE_OFFICE); // creating new HashMap HashMap<String, String> map = new HashMap<String, String>(); // adding each child node to HashMap key => value map.put(TAG_ID, id); map.put(TAG_NAME, name); map.put(TAG_EMAIL, email); map.put(TAG_PHONE_MOBILE, mobile); // adding HashList to ArrayList contactList.add(map); } } catch (JSONException e) { e.printStackTrace(); } return contactList; } //onPostExecute Method protected void onPostExecute(ArrayList<HashMap<String, String>> result) { ListAdapter adapter = new SimpleAdapter(getApplicationContext(), contactList, R.layout.list_item, new String[] { TAG_NAME, TAG_EMAIL, TAG_PHONE_MOBILE }, new int[] { R.id.name, R.id.email, R.id.mobile }); // selecting single ListView item ListView lv = getListView(); lv.setAdapter(adapter); // Launching new screen on Selecting Single ListItem lv.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { // getting values from selected ListItem String name = ((TextView) view.findViewById(R.id.name)).getText().toString(); String cost = ((TextView) view.findViewById(R.id.email)).getText().toString(); String description = ((TextView) view.findViewById(R.id.mobile)).getText().toString(); // Starting new intent Intent in = new Intent(getApplicationContext(),SingleMenuItemActivity.class); in.putExtra(TAG_NAME, name); in.putExtra(TAG_EMAIL, cost); in.putExtra(TAG_PHONE_MOBILE, description); startActivity(in); } }); if (this.dialog.isShowing()) { this.dialog.dismiss(); } } } } 
0
source

From the logs, I see that you have NullPointer in Update.java, line 39. You can start from now on. I believe this is not related to AsyncTask.

0
source

you get only zero xml at this position, so release your application, first check any xml request u get or not ????

if u gets null xml then use the following code to parse xml

 import org.xml.sax.Attributes; import org.xml.sax.SAXException; import org.xml.sax.helpers.DefaultHandler; import android.util.Log; public class XMLHandler extends DefaultHandler { String elementValue = null; Boolean elementOn = false; userdata d = userdata.getInstance(); public static XMLGettersSetters data = null; public static XMLGettersSetters getXMLData() { return data; } public static void setXMLData(XMLGettersSetters data) { XMLHandler.data = data; } /** * This will be called when the tags of the XML starts. **/ @Override public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException { elementOn = true; if (localName.equals("application")) { data = new XMLGettersSetters(); } } /** * This will be called when the tags of the XML end. **/ @Override public void endElement(String uri, String localName, String qName) throws SAXException { elementOn = false; /** * Sets the values after retrieving the values from the XML tags * */ if (localName.equalsIgnoreCase("version")){ System.out.println("ststus" + elementValue); d.setIsregi(elementValue); } /** * This is called to get the tags value **/ @Override public void characters(char[] ch, int start, int length) throws SAXException { if (elementOn) { elementValue = new String(ch, start, length); elementOn = false; } } } 
0
source

You must add the correct DTD to your XML document (defining the type of document), try it and you will see that the parsing will work smoothly. Step 1: Create a DTD patch Step 2. Add it to your XML file and save it. Step 3: upload your XML file, parse it and enjoy.

0
source

Source: https://habr.com/ru/post/1444480/


All Articles