1

I want to parse xml so i wrote code it works upto 2.3 but on latest version it doesn't work so i read about asynctask and I modified my code but my imageview still is blank and I dont get any errors on logcat that means there is a problem in the implimentation of the Asynctask .

{
    setContentView(R.layout.actionbar);
    String URL = "http://ws.audioscrobbler.com/2.0/?method=artist.search&artist=ricky_martin&api_key=97855e265470056987425832aa9aa81&limit=" + 1 + "&page=" + 1;
    new FetchXMLAsyncTask().execute(URL);
    icon = (ImageView) findViewById(R.id.icon);
}

class FetchXMLAsyncTask extends AsyncTask<String, Void, Void> {
    String name;
    @Override
    protected Void doInBackground(String... params) {
        try {
            // TODO Auto-generated method stub
            final String Artist = "artist";
            final String KEY_NAME = "name";
            final String API = "b25b959554ed76058ac220b7b2e0a026";
            final String KEY_IMAGE ="image";
            //final String KEY_COST = "cost";
            //final String KEY_DESC = "description";
            String URL = params[0]; 
            XmlParser parser = new XmlParser();
            String xml = parser.getXmlFromUrl(URL); // getting XML
            Document doc = parser.getDomElement(xml); // getting DOM element
            XPathFactory xPathfactory = XPathFactory.newInstance();
            XPath xpath = xPathfactory.newXPath();
            XPathExpression expr;
            expr = xpath.compile("//image[@size=\"large\"]");
            NodeList nl = (NodeList) expr.evaluate(doc, XPathConstants.NODESET);
            // looping through all item nodes <item>
            for (int i = 0; i < nl.getLength(); i++) {
                Element e = (Element) nl.item(i);
                String name = parser.getValue(e, KEY_NAME);// name child value
                image = parser.getValue(e, KEY_IMAGE);
                // Toast.makeText(this, "name"+name+"url"+image, Toast.LENGTH_SHORT).show();
            } }catch (XPathExpressionException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }
        return null;
    }
    @Override
    protected void onPostExecute(Void result) {
        super.onPostExecute(result);
        //do something after parsing is done
        URL thumb_u;
        try {
            thumb_u = new URL(image);
            Drawable thumb_d = Drawable.createFromStream(thumb_u.openStream(), "src");
            icon.setImageDrawable(thumb_d);
        } catch (MalformedURLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }   
}
Ahmad
  • 69,608
  • 17
  • 111
  • 137
Swap-IOS-Android
  • 4,363
  • 6
  • 49
  • 77
  • Take a look at http://stackoverflow.com/questions/9671546/asynctask-android-example/9671602#9671602 and also have some debug messages print to logcat so we can see how far it gets. – Graham Smith Sep 21 '12 at 12:05
  • I follow that tutorial before posting here but i couldn't find the way to fix this implementation. and when i run this code on device i dont get any logcat error or debug error .. i think it doenst call my FetchXmlparser class or it doesn't execute my postExecute method..can you please take a look at code – Swap-IOS-Android Sep 21 '12 at 12:11
  • As I have said before put some debug messages into the code. You SHOULD know if it fires the asynctask or not. Initially I can see that `URL` via a browser (http://ws.audioscrobbler.com/2.0/?method=artist.search&artist=ricky_martin&api_key=97855e265470056987425832aa9aa81) return "Invalid API key - You must be granted a valid key by last.fm". The `API` value seems to work though - http://ws.audioscrobbler.com/2.0/?method=artist.search&artist=ricky_martin&api_key=b25b959554ed76058ac220b7b2e0a026 – Graham Smith Sep 21 '12 at 12:31
  • I modified my api in this post "b25b959554ed76058ac220b7b2e0a026" working api – Swap-IOS-Android Sep 21 '12 at 12:45
  • hello I made modification in code with asynctask and now its showing image on 2.3 and lower android .. but still no luck for ICS Is there any other thing that i forgot like sticky thread? – Swap-IOS-Android Sep 21 '12 at 13:22

1 Answers1

1

Starting Honeycomb AsyncTasks are launched one at the time by default. Maybe you have some other task pending and blocking this one from running (new is waiting for old to finish, because it will woun't run in paralell).

Starting android 3.0 you should launch tasks in THREAD_POOL_EXECUTOR

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
    task.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, params);
} else {
    task.execute(params);
}
Yaroslav Mytkalyk
  • 16,950
  • 10
  • 72
  • 99