天气XML解析器无法正常工作

时间:2012-10-30 14:05:05

标签: android xml parsing

我一直在The New Boston

上做天气教程

在logcat中我看到以下错误:错误打开跟踪文件:没有这样的文件或目录(2)。 模拟器可以运行该程序,但是当我在上部edittext框中键入“stad”时,它不会从URL返回任何内容。你能告诉我我做错了吗?这是我的三个类和XML文件。

package com.example.weatherxml;

import java.net.URL;

import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.xml.sax.InputSource;
import org.xml.sax.XMLReader;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;



public class MainActivity extends Activity implements OnClickListener {

static final String baseURL =     "http://api.yr.no/weatherapi/seaapproachforecast/1.0/?location=";
TextView tv;
EditText city;


@Override
public void onCreate(Bundle icicle) {
    super.onCreate(icicle);
    setContentView(R.layout.activity_main);
   Button b = (Button)findViewById(R.id.activate);
   tv = (TextView)findViewById(R.id.textView1);
   city = (EditText)findViewById(R.id.city);
  //state = (EditText)findViewById(R.id.state);
  b.setOnClickListener(this);

  }


public void onClick(View v) {
    // TODO Auto-generated method stub
    String c = city.getText().toString();
    //String s = state.getText().toString();

    StringBuilder URL = new StringBuilder(baseURL);
    URL.append(c);
    String fullUrl = URL.toString();

    try{
        URL website = new URL(fullUrl);
        SAXParserFactory spf = SAXParserFactory.newInstance();
        SAXParser sp = spf.newSAXParser();
        XMLReader xr = sp.getXMLReader();
        HandlingXMLStuff doingWork = new HandlingXMLStuff();
        xr.setContentHandler(doingWork);
        xr.parse(new InputSource(website.openStream()));
        String information = doingWork.getInformation();
        tv.setText(information);
    }catch (Exception e){
    tv.setText("error");
    }

}
}

package com.example.weatherxml;

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

 import android.app.Activity;
 import android.os.Bundle;
 import android.view.View;
 import android.view.View.OnClickListener;
 import android.widget.Button;
 import android.widget.EditText;
 import android.widget.TextView;

 public class HandlingXMLStuff extends DefaultHandler {

   private XMLDataCollected info = new XMLDataCollected();
   public String getInformation(){
   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("location")){
    String city = attributes.getValue("name");
    info.setCity(city);
}else if (localName.equals("windSpeed")){
String t = attributes.getValue("mps");
int temp = Integer.parseInt(t);
info.setTemp(temp);

}

 }
 }


 package com.example.weatherxml;

 public class XMLDataCollected {

int temp = 0; 
String city = null;

public void setCity(String c){
    city = c;
}
public void setTemp(int t){
    temp = t;
}
public String dataToString(){
    return "In " + city + " the Current windspeed is " + temp + "mps ";
}
}


<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >

<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:layout_centerVertical="true"
    android:text="@string/hello_world"
    tools:context=".MainActivity" />

<EditText
    android:id="@+id/city"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="fill_horizontal"
    android:ems="10"
    android:inputType="text"
    android:text="" />

<EditText
    android:id="@+id/state"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_below="@+id/city"
    android:layout_gravity="fill_horizontal"
    android:ems="10"
    android:inputType="text"
    android:text="" />

<Button
    android:id="@+id/activate"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_above="@+id/textView1"
    android:layout_alignParentLeft="true"
    android:layout_marginBottom="34dp"
    android:text="Generate" 
    tools:context=".MainActivity" />


 </RelativeLayout>

2 个答案:

答案 0 :(得分:0)

解决方案编辑:

将此添加到您的清单中,除了您不能将小数解析为int之外,它将起作用。

<uses-permission android:name="android.permission.INTERNET"/>

/////////////////////////////////////////////// //////////////////////////////////

1)在您尝试通话之前,您是否记录了fullUrl以查看其是否正确?

     Log.e("URL:",fullUrl);

2)我没有看到你的“workWork”代码。 (解析出来)

3)个人观点:除非你这样做学习xml,否则我会观看他的json视频并转到worldweatheronline.com并在那里注册api密钥。这将是更少的代码和更快。此外,你将成为一个更好的程序员,因为你必须通过修改他的代码使解析器成为你自己。是的,它的工作,但最终它的价值。

答案 1 :(得分:0)

在我的网络浏览器中,当我尝试 http://api.yr.no/weatherapi/seaapproachforecast/1.0/?location=montreal

该页面如下:

  
    

发生了错误!

  
     

生成了以下错误消息:

     

[Thu 12月6日23:50:06] [错误]参数'location'无效:   蒙特利尔不是一个有效的价值。

API说Link here

  

http://api.met.no/weatherapi/locationforecastlts/1.1/?lat=60.10;lon=9.58

<weatherdata xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="http://api.met.no/weatherapi/locationforecastlts/1.1/schema" created="2012-12-06T23:52:35Z">
<meta>
<model name="YR" termin="2012-12-06T12:00:00Z" runended="2012-12-06T18:16:13Z" nextrun="2012-12-07T06:00:00Z" from="2012-12-07T00:00:00Z" to="2012-12-09T06:00:00Z"/>
<model name="EPS" termin="2012-12-06T12:00:00Z" runended="2012-12-06T21:54:51Z" nextrun="2012-12-07T09:45:00Z" from="2012-12-09T12:00:00Z" to="2012-12-16T06:00:00Z"/>
</meta>
<product class="pointData">
<time datatype="forecast" from="2012-12-07T00:00:00Z" to="2012-12-07T00:00:00Z">
<location altitude="485" latitude="60.1000" longitude="9.5800">
<temperature id="TTT" unit="celcius" value="-11.8"/>
<windDirection id="dd" deg="59.3" name="NE"/>
<windSpeed id="ff" mps="1.8" beaufort="1" name="Flau vind"/>
<humidity value="86.3" unit="percent"/>
<pressure id="pr" unit="hPa" value="1009.5"/>
<cloudiness id="NN" percent="28.8"/>
<fog id="FOG" percent="0.0"/>
<lowClouds id="LOW" percent="0.0"/>
<mediumClouds id="MEDIUM" percent="0.4"/>
<highClouds id="HIGH" percent="27.8"/>
<temperatureProbability unit="probabilitycode" value="0"/>
<windProbability unit="probabilitycode" value="0"/>
</location>
</time>
<time datatype="forecast" from="2012-12-06T23:00:00Z" to="2012-12-07T00:00:00Z">
<location altitude="485" latitude="60.1000" longitude="9.5800">
<precipitation unit="mm" value="0.0" minvalue="0.0" maxvalue="0.0"/>
<symbol id="LIGHTCLOUD" number="2"/>
</location>
</time>

等...

希望它有所帮助,