2012-07-08 57 views
0

我剛剛嘗試了獲取天氣fom谷歌天氣xml的代碼,但代碼給出了IOException但我不知道原因,我只是做了相同的視頻:http://www.youtube.com/watch ?v = Z1rtldBTzCE 這裏是我的代碼:天氣度xml解析

public class MyProjectActivity extends Activity implements android.view.View.OnClickListene { 
    /** Called when the activity is first created. */ 
    TextView tv; 
    EditText edt1,edt2; 
    Button btn; 
    String site="hhttp://www.google.com/ig/api?weather=Lincoln,Nebraska"; 


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

    tv=(TextView) findViewById(R.id.TV); 
    edt1=(EditText) findViewById(R.id.et1); 
    edt2=(EditText) findViewById(R.id.et2); 
    btn=(Button) findViewById(R.id.b1); 

    btn. 

setOnClickListener(this); 


    } 

public void onClick(View V){ 

     String c=edt1.getText().toString(); 
     String s=edt2.getText().toString(); 
     StringBuilder url=new StringBuilder(); 
     url.append(c+","+s); 
     String fullUrl=url.toString(); 
     try{ 
      URL website=new URL(fullUrl); 
      SAXParserFactory spf=SAXParserFactory.newInstance(); 
      SAXParser sp=spf.newSAXParser(); 
      XMLReader xr=sp.getXMLReader(); 
      handler work=new handler(); 
      xr.setContentHandler(work); 
      xr.parse(new InputSource (website.openStream())); 
      String information=work.getInfo(); 
      tv.setText(information); 
     }catch(Exception e) 
     { 
      tv.setText("error"); 
     } 



    } 



package com.myProject.pro; 

import org.xml.sax.Attributes; 
import org.xml.sax.SAXException; 
import org.xml.sax.helpers.DefaultHandler; 

public class handler extends DefaultHandler { 

    geterSetter info=new geterSetter(); 

    public String getInfo(){ 
     return info.dataToString(); 
    } 
    @Override 
    public void startElement(String uri, String localName, String qName, 
      Attributes attributes) throws SAXException { 
     // TODO Auto-generated method stub 
    if(localName.equals("city")){ 
     String city=attributes.getValue("data"); 
     info.setCity(city); 
     }else if(localName.equals("temp_c")){ 
      String t=attributes.getValue("data"); 
      int temp=Integer.parseInt(t); 
      info.setTemp(temp); 
      } 

    } 


} 

//設置GET 類包com.myProject.pro;

public class geterSetter { 
    int temp; 
    String city; 

    public void setCity(String c){ 
     city=c; 
    } 
    public void setTemp(int t){ 
     temp=t; 
    } 
    public String dataToString(){ 
     return city+temp; 
    } 
} 

回答

0

也許你有麻煩的事實您的網址架構中有2個HS:hhttphttphhttp://www.google.com/ig/api?weather=Lincoln,Nebraska。刪除一個h

+0

不,它不是問題:( – user1404380 2012-07-08 09:33:45

+0

@ user1404380你可以添加異常的堆棧跟蹤嗎? – 2012-07-08 09:35:46

+0

我該怎麼做?sry但我是新的android^_ ^! – user1404380 2012-07-08 09:52:14