0

Google 天気 xml から天気を取得するサンプル コードを試しましたが、コードは IOException を返しますが、動画と同じことをした理由がわかりません: http://www.youtube.com/watch?v=Z1rtldBTzCE ここに私のコード:

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

    }


}

// set 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;
   }
}
4

1 に答える 1

0

url スキーマに 2 つの H: hhttpnot http:があるという事実に問題を抱えている可能性がありますhhttp://www.google.com/ig/api?weather=Lincoln,Nebraska。1 つ削除しhます。

于 2012-07-08T09:29:41.840 に答える