2012-06-25 75 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