Note that there are some explanatory texts on larger screens.

plurals
  1. POCall web service in android 4.1
    primarykey
    data
    text
    <p>I have this code here that worked all this time in 2.3 now we have to update it and I am getting a lot of errors like <code>NetworkOnMainThreadException</code>. I want to go and grab a xml from my web service, bring it down and parse it into an array list. Here is the code</p> <pre><code>//Gets the xml from the url specified String CallWebService(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; } //Parses the xml to get DOM element 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; } //Gets the child nodes of the xml 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 || child.getNodeType() == Node.CDATA_SECTION_NODE){ return child.getNodeValue(); } } } } return ""; } </code></pre> <p>I also have a <code>getChildElements</code> method. The problem is when I call this method. I used to do so like this:</p> <pre><code>String serviceURL = "http://webservice.example.com/"; String xml = CallWebService(serviceURL); Document doc = GetDomElement(xml); // getting DOM element NodeList nl = doc.getElementsByTagName("details"); getChildElements(nl); </code></pre> <p>But now in 4.1 I need to do this asynchronously and I don't know how. Any help will be greatly appreciated. </p> <p><strong>EDIT</strong></p> <p>Here is what i have bu the Thread does not start</p> <pre><code>final String serviceURL = "urlString"; mHandler = new Handler() { @Override public void handleMessage(Message msg) { if(msg.what == JOB_COMPLETE) { String xml = (String) msg.obj; Document doc = GetDomElement(xml); // getting DOM element NodeList nl = doc.getElementsByTagName("details"); getChildElements(nl); } super.handleMessage(msg); } }; Thread t = new Thread() { @Override public void run() { String xml = CallWebService(serviceURL); Message msg = Message.obtain(mHandler, JOB_COMPLETE, xml); msg.sendToTarget(); } }; t.start(); </code></pre> <p><strong>EDIT</strong></p> <p>So I am trying the async way and it still wont work. Its not hitting the GetDomElement at all. Here is the code.</p> <pre><code> //I call this in my onCreate() new getAppInfo().execute("http://webservice.example.com"); private class getAppInfo extends AsyncTask&lt;String, Void, String&gt; { /** The system calls this to perform work in a worker thread and * delivers it the parameters given to AsyncTask.execute() */ protected String doInBackground(String... urls) { return CallWebService(urls[0]); } /** The system calls this to perform work in the UI thread and delivers * the result from doInBackground() */ protected void onPostExecute(String xml) { Document doc = GetDomElement(xml); // getting DOM element NodeList nl = doc.getElementsByTagName("details"); getChildElements(nl); } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload