簡體   English   中英

解析xml asynctask並寫入listview

[英]parsing xml asynctask and writing in listview

我想在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();

而且我不知道解析器在asynctask中運行時如何做到這一點。 使解析器將菜單項放入listadapter:

menuItems.add(map);

初始化Asynctask時,通過WeakReference引用您的適配器。

AsyncTask進行參數化以返回您需要返回的任何內容,例如List<String> 連接和數據提取發生在doInBackground

onPostExecute ,檢查您的WeakReference.get不為null並填充適配器。

您可以使用droidQuery庫以給定URL異步下載XML Document 例如:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM