PowenKo, android,Tutorial 060-3, Data, XML PList parser


<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
	<key>fruit</key>
	<array>
		<string>apple</string>
		<string>banana</string>
		<string>pinapple</string>
	</array>
	<key>states</key>
	<array>
		<string>CA</string>
		<string>NY</string>
		<string>FL</string>
	</array>
</dict>
</plist>

package com.powenko.Tutorial_internet_soapAndTableView;

import java.io.IOException;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.List;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
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 android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class Tutorial_internet_soapAndTableViewActivity extends Activity {
    /** Called when the activity is first created. */

	//TextView m_TextView01;
	 private List<News> li=new ArrayList<News>();
	  ListView myListView;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        Internet_Http_postData();
        Fun_ListView();
    }

    private void Fun_ListView(){

	    myListView=(ListView) findViewById(R.id.listView1);
	    myListView.setCacheColorHint(0);
	    myListView.setAdapter(new MyAdapter(this,li));

	    myListView
	        .setOnItemSelectedListener(new AdapterView.OnItemSelectedListener()
	        {

	         @Override
	          public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
	              long arg3)
	          {

	          }

	         @Override
	          public void onNothingSelected(AdapterView<?> arg0)
	          {

	          }

	        });

	    /* myListView�[�JOnItemClickListener */
	    myListView.setOnItemClickListener(new AdapterView.OnItemClickListener()
	    {

	    //  @Override
	      public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
	          long arg3)
	      {
			  Toast.makeText(
					  Tutorial_internet_soapAndTableViewActivity.this,"Selected",
	                          Toast.LENGTH_SHORT )
	              .show();

	      }
	    });

    }
    public void Internet_Http_postData() {

        // Create a new HttpClient and Post Header
        HttpClient httpclient = new DefaultHttpClient();
        HttpPost httppost = new HttpPost("http://www.mymobione.com.tw/MIDNTSTK01/MMOService.asmx?WSDL");//"http://www.nanonull.com/TimeService/TimeService.asmx");

        try {
        	String webServiceXml = "<?xml version=\"1.0\" encoding=\"utf-8\"?><soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\"><soap:Body><Message xmlns=\"http://www.mymobione.com.tw/\">";
        		 webServiceXml += 	   "<xml><MMOREQ><APID>20110715</APID><MSGID>M001</MSGID><DBID>0</DBID><PAGEID>0</PAGEID><AFLAG>D</AFLAG>";
        			 webServiceXml += 	   "<SFLAG>CUB</SFLAG><ITEMCOUNT>10</ITEMCOUNT></MMOREQ></xml></Message></soap:Body></soap:Envelope>";

             httppost.setHeader("content-type","text/xml; charset=utf-8");
             httppost.setHeader("SOAPAction","http://www.mymobione.com.tw/Message");//"http://www.Nanonull.com/TimeService/getOffesetUTCTime");//"http://www.example.com/WebServiceMethod1");

             httppost.setEntity(new StringEntity(webServiceXml));

             HttpResponse httpResponse = httpclient.execute(httppost);

             String t_response = EntityUtils.toString(httpResponse.getEntity());
        //     m_TextView01.setText(t_response);
             Fun_SOAP(t_response);

        }  catch (ClientProtocolException e)
        {
      // 	 m_TextView01.setText(e.getMessage().toString());
          e.printStackTrace();
        }
        catch (IOException e)
        {
      // 	 m_TextView01.setText(e.getMessage().toString());
          e.printStackTrace();
        }
        catch (Exception e)
        {
     //  	 m_TextView01.setText(e.getMessage().toString());
          e.printStackTrace();
        }
    }
    void Fun_SOAP(String i_xmlString){
    	LinearLayout layout = new LinearLayout(this);
		layout.setOrientation(1);
    	TextView name[];
		TextView website[];
		TextView category[];
    	try {

			DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
			DocumentBuilder db = dbf.newDocumentBuilder();
			//Document doc = db.parse(new InputSource(url.openStream()));

			InputSource inStream = new InputSource();
			inStream.setCharacterStream(new StringReader(i_xmlString));
			Document doc = db.parse(inStream);

			doc.getDocumentElement().normalize();

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

			int t1=nodeList.getLength();
			/** Assign textview array lenght by arraylist size */
			name = new TextView[nodeList.getLength()];
			website = new TextView[nodeList.getLength()];
			category = new TextView[nodeList.getLength()];

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

				Node node = nodeList.item(i);

				name[i] = new TextView(this);
				website[i] = new TextView(this);
				category[i] = new TextView(this);

				Element fstElmnt = (Element) node;
				NodeList nameList = fstElmnt.getElementsByTagName("TITLE");
				Element nameElement = (Element) nameList.item(0);
				nameList = nameElement.getChildNodes();
				name[i].setText("Name = "
						+ ((Node) nameList.item(0)).getNodeValue());

				NodeList websiteList = fstElmnt.getElementsByTagName("INFURL");
				Element websiteElement = (Element) websiteList.item(0);
				websiteList = websiteElement.getChildNodes();
				website[i].setText("Website = "
						+ ((Node) websiteList.item(0)).getNodeValue());

				 News news=new News();
				    news.setTitle(((Node) nameList.item(0)).getNodeValue());
				    news.setLink(((Node) websiteList.item(0)).getNodeValue());
				    li.add(news);
			}
		} catch (Exception e) {
			System.out.println("XML Pasing Excpetion = " + e);
		}
	//	setContentView(layout);

    }

}
package com.powenko.Tutorial_Data_plist;


public class News
{ 

	public String _title="";
	public String _link="";
	
  
  
  public String getTitle()
  { 
    return _title; 
  } 
  public String getLink()
  { 
    return _link; 
  }
  
  
  public void setTitle(String title)
  { 
    _title=title; 
  } 
  public void setLink(String link)
  { 
    _link=link; 
  }

  
}

sample code:

Tutorial_internet_soapAndTableView


Leave a Reply