Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/ant/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android xml解析_Android - Fatal编程技术网

Android xml解析

Android xml解析,android,Android,我刚刚尝试了从google weather xml获取天气信息的sampl代码,但代码给出了IOException,但我不知道为什么我会像视频一样:http://www.youtube.com/watch?v=Z1rtldBTzCE 这是我的密码: public class MyProjectActivity extends Activity implements android.view.View.OnClickListene { /** Called when the activi

我刚刚尝试了从google weather xml获取天气信息的sampl代码,但代码给出了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);
            }

    }


}
//设置获取类 包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;
   }
}

您的url模式有两个Hs:
hhttp
而不是
http
hhttp://www.google.com/ig/api?weather=Lincoln,内布拉斯加州
。删除一个
h

@user1404380您能添加异常的堆栈跟踪吗?我怎么做??sry,但我是android新手^ ^@user1404380你是说添加堆栈跟踪?