2012-06-25 115 views
0

數據`下一個代碼應該從baseURL時(網絡服務)收杆數據的TextView 問題: * 它給IOException異常..我從web服務,但該程序仍肯定給出錯誤任何一個幫助從Web服務獲取數據? *獲得從WebService IOException異常

public class NewParsActivity extends Activity { 
    /** Called when the activity is first created. */ 
    static final String baseUrl="http://www.androidpeople.com/wp-content/uploads/2010/06/example.xml"; 

    TextView tv; 
    EditText ed,ed2; 
    Button b; 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 

     tv=(TextView)findViewById(R.id.text); 
     ed=(EditText)findViewById(R.id.edt); 
     b=(Button) findViewById(R.id.button1); 
     ed2=(EditText) findViewById(R.id.editText1); 

     try{ 



    SAXParserFactory spf=SAXParserFactory.newInstance(); 
    SAXParser sp=spf.newSAXParser(); 
    XMLReader xr=sp.getXMLReader(); 
    URL link=new URL(baseUrl); 
    handlXml doingWork=new handlXml(); 
    xr.setContentHandler(doingWork); 
    xr.parse(new InputSource(link.openStream())); 
    String information=doingWork.getInformation(); 
    tv.setText(information); 
     }catch(Exception e){ 
     tv.setText("Error"); 
     } 



// 
public class handlXml extends DefaultHandler { 
    One item=new One(); 

    public String getInformation(){ 
     return item.DataToString(); 

    } 
    @Override 


    public void startElement(String uri, String localName, String qName, 
       Attributes attributes) throws SAXException { 
      // TODO Auto-generated method stub 
      if(localName.equals("website")){ 
       String uniN=attributes.getValue("category"); 
       item.setName(uniN); 
      } 
     } 

    } 

// 
<i> 
public class One { 

    String UName; 
    public void setName(String n){ 
    UName=n;  
    } 


public String DataToString() 
{ 
    return "In"+UName; 
} 
} 
<i> 
+1

安置自己的logcat這裏 – Praveenkumar

+0

什麼錯誤???? – Rajnikant

+0

其中是endElement,字符.. – Nikhil

回答

0

同樣的例子可以找到。

XMLParsingExample.java

public class XMLParsingExample extends Activity { 

    /** Create Object For SiteList Class */ 
    SitesList sitesList = null; 

    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 

     /** Create a new layout to display the view */ 
     LinearLayout layout = new LinearLayout(this); 
     layout.setOrientation(1); 

     /** Create a new textview array to display the results */ 
     TextView name[]; 
     TextView website[]; 
     TextView category[]; 

     try { 

      /** Handling XML */ 
      SAXParserFactory spf = SAXParserFactory.newInstance(); 
      SAXParser sp = spf.newSAXParser(); 
      XMLReader xr = sp.getXMLReader(); 

      /** Send URL to parse XML Tags */ 
      URL sourceUrl = new URL(
        "http://www.androidpeople.com/wp-content/uploads/2010/06/example.xml"); 

      /** Create handler to handle XML Tags (extends DefaultHandler) */ 
      MyXMLHandler myXMLHandler = new MyXMLHandler(); 
      xr.setContentHandler(myXMLHandler); 
      xr.parse(new InputSource(sourceUrl.openStream())); 

     } catch (Exception e) { 
      System.out.println("XML Pasing Excpetion = " + e); 
     } 

     /** Get result from MyXMLHandler SitlesList Object */ 
     sitesList = MyXMLHandler.sitesList; 

     /** Assign textview array lenght by arraylist size */ 
     name = new TextView[sitesList.getName().size()]; 
     website = new TextView[sitesList.getName().size()]; 
     category = new TextView[sitesList.getName().size()]; 

     /** Set the result text in textview and add it to layout */ 
     for (int i = 0; i < sitesList.getName().size(); i++) { 
      name[i] = new TextView(this); 
      name[i].setText("Name = "+sitesList.getName().get(i)); 
      website[i] = new TextView(this); 
      website[i].setText("Website = "+sitesList.getWebsite().get(i)); 
      category[i] = new TextView(this); 
      category[i].setText("Website Category = "+sitesList.getCategory().get(i)); 

      layout.addView(name[i]); 
      layout.addView(website[i]); 
      layout.addView(category[i]); 
     } 

     /** Set the layout view to display */ 
     setContentView(layout); 

    } 
} 

XMLParsing Sample Project

這裏是我的輸出窗口 -

Output

0

看看本教程中,XML解析一個很好的指南android.i希望這將解決烏爾查詢

在這裏,我把那tutrial內容

說,我們需要解析這個XML文件 http://api.androidhive.info/pizza/?format=xml

,這裏是一類用於解析這個XML

public class AndroidXMLParsingActivity extends ListActivity { 

// All static variables 
static final String URL = "http://api.androidhive.info/pizza/?format=xml"; 
// XML node keys 
static final String KEY_ITEM = "item"; // parent node 
static final String KEY_ID = "id"; 
static final String KEY_NAME = "name"; 
static final String KEY_COST = "cost"; 
static final String KEY_DESC = "description"; 

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

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

    XMLParser parser = new XMLParser(); 
    String xml = parser.getXmlFromUrl(URL); // getting XML 
    Document doc = parser.getDomElement(xml); // getting DOM element 

    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_COST, "Rs." + parser.getValue(e, KEY_COST)); 
     map.put(KEY_DESC, parser.getValue(e, KEY_DESC)); 

     // adding HashList to ArrayList 
     menuItems.add(map); 
    } 

    // Adding menuItems to ListView 
    ListAdapter adapter = new SimpleAdapter(this, menuItems, 
      R.layout.list_item, 
      new String[] { KEY_NAME, KEY_DESC, KEY_COST }, new int[] { 
        R.id.name, R.id.desciption, R.id.cost }); 

    setListAdapter(adapter); 

    // selecting single ListView item 
    ListView lv = getListView(); 
      // listening to single listitem click 
    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 description = ((TextView) view.findViewById(R.id.desciption)).getText().toString(); 

      // Starting new intent 
      Intent in = new Intent(getApplicationContext(), SingleMenuItemActivity.class); 
      in.putExtra(KEY_NAME, name); 
      in.putExtra(KEY_COST, cost); 
      in.putExtra(KEY_DESC, description); 
      startActivity(in); 

     } 
    }); 
} 

}

和完整的指南,這個可以從這個鏈接與API的工作對我罰款

http://www.androidhive.info/2011/11/android-xml-parsing-tutorial/

+0

你應該在這裏包括文章的重要部分。因爲可能有這種鏈接將不再存在於功能中的情況。 –

+0

好吧,我會看到這個教程thanx :)) – loleana

+0

帕雷什我敢肯定,鏈接工程 – loleana