0

我想在 asynctask 中解析我的 xml,因为在 android 4.0 + 上没有 asynctask 它将无法工作。问题是我无法在我的 asynctask 的 listview 中写入我的字符串。

这是我的 xml 解析活动:

package de.heron.cloudbox;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.HashMap;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParserFactory;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;

import android.app.ListActivity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;

public class AndroidXMLParsingActivity extends ListActivity{

    String dwnload_file_path = "http://h.t-softec.de/heron.website.app/android/everhomeapp.xml";
    String dest_file_path = "http://h.t-softec.de/heron.website.app/android/everhomeapp.xml";
    Button b1;
    ProgressDialog dialog = null;
    ///storage/emulated/0/everhomeapp.xml
    // All static variables
    static final String URL = "http://h.t-softec.de/heron.website.app/android/everhomeapp.xml";
//file:///storage/emulated/0/everhomeapp.xml
    // XML node keys
    static final String KEY_ITEM = "device"; // parent node
    static final String KEY_ID = "deviceid";
    static final String KEY_NAME = "devicename";
    static final String KEY_actionid = "actions";
    static final String KEY_actionid2 = "actions2";
    static final String KEY_DESC = "devicetype";
    static final String KEY_texton = "texton";
    static final String KEY_textoff = "textoff";
    ArrayList<String> mImageLink;
    String[] actions = new String[] {
            "Alle Räume",
            "Wohnzimmer",
            "Küche",
            "Gäste-Wc"
        };
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        overridePendingTransition( R.anim.leftright, R.anim.rightleft );
        setTitle("Geräte");
        getActionBar().setDisplayHomeAsUpEnabled(true);

        ArrayList<HashMap<String, String>> menuItems = new ArrayList<HashMap<String, String>>();

try{
        XMLParserLocal parser = new XMLParserLocal();
          File file = new File("/sdcard/everhomeapp.xml");
          InputStream is = new FileInputStream(file.getPath());
          DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
          DocumentBuilder db = dbf.newDocumentBuilder();
          Document doc = db.parse(new InputSource(is));
          doc.getDocumentElement().normalize();


        NodeList nl = doc.getElementsByTagName(KEY_ITEM);
        for (int i = 0; i < nl.getLength(); i++) {
            HashMap<String, String> map = new HashMap<String, String>();
            Element e = (Element) nl.item(i);
            // adding each child node to HashMap key => value
            map.put(KEY_ID, parser.getValue(e, KEY_ID));
            map.put(KEY_NAME, parser.getValue(e, KEY_NAME));
            map.put(KEY_actionid, parser.getValue(e, KEY_actionid));
            map.put(KEY_actionid2, parser.getValue(e, KEY_actionid2));
            map.put(KEY_DESC, parser.getValue(e, KEY_DESC));
            map.put(KEY_texton, parser.getValue(e, KEY_texton));
            map.put(KEY_textoff, parser.getValue(e, KEY_textoff));
            menuItems.add(map);
        }
 } catch (Exception e) {
    System.out.println("XML Pasing Excpetion = " + e);
}
        // Adding menuItems to ListView
        ListAdapter adapter2 = new SimpleAdapter(this, menuItems,
                R.layout.list_item,
                new String[] { KEY_NAME, KEY_DESC, KEY_ID, KEY_actionid, KEY_texton, KEY_textoff}, new int[] {
                        R.id.name, R.id.desciption, R.id.cost, R.id.deviceid,R.id.on,R.id.off });

        setListAdapter(adapter2);

        // selecting single ListView item
        ListView lv = getListView();

        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.cost)).getText().toString();
                String cost2 = ((TextView) view.findViewById(R.id.cost)).getText().toString();
                String deviceid = ((TextView) view.findViewById(R.id.deviceid)).getText().toString();
                String description = ((TextView) view.findViewById(R.id.desciption)).getText().toString();
                String on = ((Button) view.findViewById(R.id.on)).getText().toString();
                String off = ((Button) view.findViewById(R.id.off)).getText().toString();

                // Starting new intent
                Intent in = new Intent(getApplicationContext(), SingleMenuItemActivity.class);
                in.putExtra(KEY_NAME, name);
                in.putExtra(KEY_actionid, cost);
                in.putExtra(KEY_actionid2, cost2);
                in.putExtra(KEY_DESC, description);
                in.putExtra(KEY_ID, deviceid);
                in.putExtra(KEY_texton, on);
                in.putExtra(KEY_textoff, off);
                startActivity(in);
            }
        });
        try {
        URL url = new URL("http://h.t-softec.de/heron.website.app/android/everhomeapp.xml");
        HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
        urlConnection.setRequestMethod("GET");
        urlConnection.setDoOutput(true);
        urlConnection.connect();
        File SDCardRoot = Environment.getExternalStorageDirectory();
        File file = new File(SDCardRoot,"everhomeapp.xml");
        FileOutputStream fileOutput = new FileOutputStream(file);
        InputStream inputStream = urlConnection.getInputStream();
        int totalSize = urlConnection.getContentLength();
        int downloadedSize = 0;
        byte[] buffer = new byte[1024];
        int bufferLength = 0;
        while ( (bufferLength = inputStream.read(buffer)) > 0 ) {
                fileOutput.write(buffer, 0, bufferLength);
                downloadedSize += bufferLength;
        }
        parseFile();
        fileOutput.close();
} catch (MalformedURLException e) {
        e.printStackTrace();
        parseFile();
} catch (IOException e) {
        e.printStackTrace();
        parseFile();
}
    }

    public void parseFile(){


        ArrayList<HashMap<String, String>> menuItems = new ArrayList<HashMap<String, String>>();
try{
        XMLParserLocal parser = new XMLParserLocal();
          File file = new File("/sdcard/everhomeapp.xml");
          InputStream is = new FileInputStream(file.getPath());
          DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
          DocumentBuilder db = dbf.newDocumentBuilder();
          Document doc = db.parse(new InputSource(is));
          doc.getDocumentElement().normalize();

        NodeList nl = doc.getElementsByTagName(KEY_ITEM);

        for (int i = 0; i < nl.getLength(); i++) {

            HashMap<String, String> map = new HashMap<String, String>();
            Element e = (Element) nl.item(i);

            map.put(KEY_ID, parser.getValue(e, KEY_ID));
            map.put(KEY_NAME, parser.getValue(e, KEY_NAME));
            map.put(KEY_actionid, parser.getValue(e, KEY_actionid));
            map.put(KEY_actionid2, parser.getValue(e, KEY_actionid2));
            map.put(KEY_DESC, parser.getValue(e, KEY_DESC));
            map.put(KEY_texton, parser.getValue(e, KEY_texton));
            map.put(KEY_textoff, parser.getValue(e, KEY_textoff));

            menuItems.add(map);
        }
 } catch (Exception e) {
    System.out.println("XML Pasing Excpetion = " + e);
}
        ListAdapter adapter = new SimpleAdapter(this, menuItems,
                R.layout.list_item,
                new String[] { KEY_NAME, KEY_DESC, KEY_ID, KEY_actionid, KEY_texton, KEY_textoff}, new int[] {
                        R.id.name, R.id.desciption, R.id.cost, R.id.deviceid,R.id.on,R.id.off });

        setListAdapter(adapter);

        ListView lv = getListView();

        lv.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                    int position, long id) {
                String name = ((TextView) view.findViewById(R.id.name)).getText().toString();
                String cost = ((TextView) view.findViewById(R.id.cost)).getText().toString();
                String deviceid = ((TextView) view.findViewById(R.id.deviceid)).getText().toString();
                String description = ((TextView) view.findViewById(R.id.desciption)).getText().toString();
                String on = ((Button) view.findViewById(R.id.on)).getText().toString();
                String off = ((Button) view.findViewById(R.id.off)).getText().toString();

                Intent in = new Intent(getApplicationContext(), SingleMenuItemActivity.class);
                in.putExtra(KEY_NAME, name);
                in.putExtra(KEY_actionid, cost);
                in.putExtra(KEY_DESC, description);
                in.putExtra(KEY_ID, deviceid);
                in.putExtra(KEY_texton, on);
                in.putExtra(KEY_textoff, off);
                startActivity(in);

            }
        });

    }
    public void parsexmllocal(){

         mImageLink = new ArrayList<String>();

         try{

         File file = new File("mnt/sdcard/kursywalut.xml");
         InputStream is = new FileInputStream(file.getPath());
         DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
         DocumentBuilder db = dbf.newDocumentBuilder();
         Document doc = db.parse(new InputSource(is));
         doc.getDocumentElement().normalize();

         NodeList nodeList = doc.getElementsByTagName("image");

         for (int i = 0; i < nodeList.getLength(); i++) {

             Node node = nodeList.item(i);

             Element fstElmnt = (Element) node;

             mImageLink.add(fstElmnt.getAttribute("link"));
         }
         } catch (Exception e) {
                System.out.println("XML Pasing Excpetion = " + e);
            }
    }
      public void downloadFile(String url, String dest_file_path) {
          try {
              File dest_file = new File(dest_file_path);
              URL u = new URL(url);
              URLConnection conn = u.openConnection();
              int contentLength = conn.getContentLength();
              DataInputStream stream = new DataInputStream(u.openStream());
              byte[] buffer = new byte[contentLength];
              stream.readFully(buffer);
              stream.close();
              DataOutputStream fos = new DataOutputStream(new FileOutputStream(dest_file));
              fos.write(buffer);
              fos.flush();
              fos.close();
              hideProgressIndicator();

          } catch(FileNotFoundException e) {
              hideProgressIndicator();
              return; 
          } catch (IOException e) {
              hideProgressIndicator();
              return; 
          }
    }

    void hideProgressIndicator(){
        runOnUiThread(new Runnable() {
            public void run() {
                dialog.dismiss();
            }
        });  
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.activity_main,  menu);
        return super.onCreateOptionsMenu(menu);
    }
    public void clicksound(View view){

          MediaPlayer mp = MediaPlayer.create(this, R.raw.clicksound);  
          mp.start();

        }
    public boolean onMenuItemSelected(int featureId, MenuItem item) {

        int itemId = item.getItemId();
        switch (itemId) {
        case R.id.refresh:  
            try {
                URL url = new URL("http://h.t-softec.de/heron.website.app/android/everhomeapp.xml");
                HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
                urlConnection.setRequestMethod("GET");
                urlConnection.setDoOutput(true);
                urlConnection.connect();
                File SDCardRoot = Environment.getExternalStorageDirectory();
                File file = new File(SDCardRoot,"everhomeapp.xml");
                FileOutputStream fileOutput = new FileOutputStream(file);
                InputStream inputStream = urlConnection.getInputStream();
                int totalSize = urlConnection.getContentLength();
                int downloadedSize = 0;
                byte[] buffer = new byte[1024];
                int bufferLength = 0;
                while ( (bufferLength = inputStream.read(buffer)) > 0 ) {
                        fileOutput.write(buffer, 0, bufferLength);
                        downloadedSize += bufferLength;
                }
                Toast.makeText(this, "Geräteliste erfolgreich aktualisiert!", Toast.LENGTH_LONG).show();
                fileOutput.close();

        } catch (MalformedURLException e) {
                e.printStackTrace();
        } catch (IOException e) {
                e.printStackTrace();
        }
            Handler handler4 = new Handler();
            handler4.postDelayed(new Runnable()
            {
                 @Override
                 public void run()
                 {
            parseFile();
                 }
            }, 100);

            break;
        case R.id.change:   
              Intent i = new Intent(this, SettingsActivity.class);
              startActivity(i);
            break;
        case android.R.id.home: 
            Intent i2 = new Intent(this, AndroidXMLParsingActivity2.class);
            startActivity(i2);
            break;
        }
        return true;
    }
}

这是我的单身活动:

package de.heron.cloudbox;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class SingleMenuItemActivity  extends Activity {
    EditText txtCode;
    // XML node keys
    static final String KEY_NAME = "name";
    static final String KEY_actionid = "cost";
    static final String KEY_actionid2 = "cost2";
    static final String KEY_DESC = "description";
    static final String KEY_ID = "deviceid";
    static final String KEY_texton = "texton";
    static final String KEY_textoff = "textoff";
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.single_list_item);

        // getting intent data
        Intent in = getIntent();

        // Get XML values from previous intent
        String name = in.getStringExtra(KEY_NAME);
        String cost = in.getStringExtra(KEY_actionid);
        String cost2 = in.getStringExtra(KEY_actionid2);
        String description = in.getStringExtra(KEY_DESC);
        String deviceid = in.getStringExtra(KEY_ID);
        String on = in.getStringExtra(KEY_texton);
        String off = in.getStringExtra(KEY_textoff);

        // Displaying all values on the screen
        TextView lblName = (TextView) findViewById(R.id.name_label);
        TextView lblCost = (TextView) findViewById(R.id.cost_label);
        TextView lblDesc = (TextView) findViewById(R.id.description_label);
        TextView lblid = (TextView) findViewById(R.id.id_label);
       // TextView lblon = (Button) findViewById(R.id.on);
       // TextView lbloff = (Button) findViewById(R.id.off);
        Button lblon = (Button)findViewById(R.id.on);
        lblon.getText().toString();
        lblon.setOnClickListener(new View.OnClickListener() {
                public void onClick(View v) {
                    SharedPreferences settings = getSharedPreferences("UserInfo", 0);
                    txtCode.setText(settings.getString("Code", "").toString());
                    String[] url={"http://everhome.de/api/applive/", (settings.getString("Code", "")), "/" + KEY_ID,"/" + KEY_actionid};
                    new onoff(SingleMenuItemActivity.this).execute(url);
                }});
        Button lbloff = (Button)findViewById(R.id.off);
        lbloff.getText().toString();
        lbloff.setOnClickListener(new View.OnClickListener() {
                public void onClick(View v) {
                    String[] url={"http://everhome.de/api/applive/", "8ef43502ad3dc04f87b4a48b993878c0","/" + KEY_ID,"/" + KEY_actionid2};
                    new onoff(SingleMenuItemActivity.this).execute(url);
                }
                });
        lblName.setText(name);
        lblCost.setText(cost);
        lblCost.setText(cost2);
        lblDesc.setText(description);
        lblid.setText(deviceid);
        lblon.setText(on);
        lbloff.setText(off);
        getActionBar().setDisplayHomeAsUpEnabled(true);
        getActionBar().setIcon(R.drawable.ic_launcher);
        setTitle("Geräte");

        Button devices = (Button) findViewById(R.id.devices);
        devices.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
        Intent i = new Intent(SingleMenuItemActivity.this, AndroidXMLParsingActivity.class);
        startActivity(i);
        }
        });
        Button scenes = (Button) findViewById(R.id.scenes);
        scenes.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
        Intent i = new Intent(SingleMenuItemActivity.this, AndroidXMLParsingActivity2.class);
        startActivity(i);
        }
        });


    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.activity_main,  menu);
        return super.onCreateOptionsMenu(menu);
    }


    public boolean onMenuItemSelected(int featureId, MenuItem item) {

        int itemId = item.getItemId();
        switch (itemId) {
        case R.id.refresh:  

            break;
        }
        return true;
    }

}

我希望任何人都可以帮助我解决我的问题。更新:

那是使用 asynctask 的测试:

class xmlparse extends AsyncTask<Void, Void, Void> {
    static final String KEY_ITEM = "device"; // parent node
    static final String KEY_ID = "deviceid";
    static final String KEY_NAME = "devicename";
    static final String KEY_actionid = "actions";
    static final String KEY_actionid2 = "actions2";
    static final String KEY_DESC = "devicetype";
    static final String KEY_texton = "texton";
    static final String KEY_textoff = "textoff";
    @Override
    protected Void doInBackground(Void... params) {
        /*
                    make connection & download XML here, 
                    use your XML parser class object to parse the xml from here

                    create ArrayList & etc. from here...

                */
        ArrayList<HashMap<String, String>> menuItems = new ArrayList<HashMap<String, String>>();

        //
        //File xml = parser.getXmlFromUrl(URL); // getting XML
        //Document doc = parser.getDomElement(xml); // getting DOM element
try{
        XMLParserLocal parser = new XMLParserLocal();
          File file = new File("/sdcard/everhomeapp.xml");
          InputStream is = new FileInputStream(file.getPath());
          DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
          DocumentBuilder db = dbf.newDocumentBuilder();
          Document doc = db.parse(new InputSource(is));
          doc.getDocumentElement().normalize();

        NodeList nl = doc.getElementsByTagName(KEY_ITEM);
        // looping through all item nodes <item>
        for (int i = 0; i < nl.getLength(); i++) {
            // creating new HashMap
            HashMap<String, String> map = new HashMap<String, String>();
            Element e = (Element) nl.item(i);
            // adding each child node to HashMap key => value
            map.put(KEY_ID, parser.getValue(e, KEY_ID));
            map.put(KEY_NAME, parser.getValue(e, KEY_NAME));
            map.put(KEY_actionid, parser.getValue(e, KEY_actionid));
            map.put(KEY_actionid2, parser.getValue(e, KEY_actionid2));
            map.put(KEY_DESC, parser.getValue(e, KEY_DESC));
            map.put(KEY_texton, parser.getValue(e, KEY_texton));
            map.put(KEY_textoff, parser.getValue(e, KEY_textoff));

            // adding HashList to ArrayList
            menuItems.add(map);
        }
 } catch (Exception e) {
    System.out.println("XML Pasing Excpetion = " + e);
}
        return null;
    }
    @Override
    protected void onPostExecute(Void result) {
        // postexecute logic
        super.onPostExecute(result);
    }

    @Override
    protected void onPreExecute() {
        // pre execute logic
        super.onPreExecute();
    }
}

但问题是我想用 listadapter 将字符串(菜单项)放在 listview 中:

ListAdapter adapter2 = new SimpleAdapter(this, menuItems,
                    R.layout.list_item,
                    new String[] { KEY_NAME, KEY_DESC, KEY_ID, KEY_actionid, KEY_texton, KEY_textoff}, new int[] {
                            R.id.name, R.id.desciption, R.id.cost, R.id.deviceid,R.id.on,R.id.off });

            setListAdapter(adapter2);

            // selecting single ListView item
            ListView lv = getListView();

            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.cost)).getText().toString();
                    String cost2 = ((TextView) view.findViewById(R.id.cost)).getText().toString();
                    String deviceid = ((TextView) view.findViewById(R.id.deviceid)).getText().toString();
                    String description = ((TextView) view.findViewById(R.id.desciption)).getText().toString();
                    String on = ((Button) view.findViewById(R.id.on)).getText().toString();
                    String off = ((Button) view.findViewById(R.id.off)).getText().toString();

而且我不知道当解析器在异步任务中运行时如何做到这一点。使解析器将菜单项放入列表适配器:

menuItems.add(map);
4

2 回答 2

0

在初始化WeakReference您的Asynctask.

参数化AsyncTask以返回您需要返回的任何内容,例如 a List<String>。连接和数据拉取发生在doInBackground.

onPostExecute中,检查您WeakReference.get的不是null并填充适配器。

于 2013-08-05T20:46:32.343 回答
-1

您可以使用droidQuery库异步下载XML Document给定的 a URL. 例如:

$.ajax(new AjaxOptions().url("http://www.example.com")
                        .type("GET")
                        .context(this)
                        .dataType("xml")
                        .success(new Function() {
                            @Override
                            public void invoke($ droidQuery, Object... params) {
                                Document xml = (Document) params[0];
                                //TODO parse xml here. This is the UI Thread, so you can also modify your TextViews here.
                            }
                        })
                        .error(new Function() {
                            @Override
                            public void invoke($ droidQuery, Object... params) {
                                AjaxError e = (AjaxError) params[0];
                                droidQuery.alert("Error: " + e.status + ": " + e.error);
                            }
                        }));
于 2013-08-05T20:36:02.540 回答