1

我一直在做 新波士顿的天气教程

在 logcat 中,我看到以下错误:打开跟踪文件时出错:没有这样的文件或目录(2)。模拟器可以运行程序,但是当我在上方的编辑文本框中键入“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>
4

2 回答 2

0

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

该页面如下:

发生错误!

生成了以下错误消息:

[2012 年 12 月 6 日星期四 23:50:06] [错误] 参数“位置”无效:蒙特利尔不是有效值。

API在这里说链接

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>

ETC...

希望对你有帮助

于 2012-12-06T23:54:10.597 回答
0

解决方案编辑:

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

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

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

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

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

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

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

于 2012-10-30T14:49:57.933 回答