1

The New Bostonで Weather チュートリアルを行っています。

logcat に次のエラーが表示されます: トレース ファイルを開くときにエラーが発生しました: そのようなファイルまたはディレクトリはありません(2)。エミュレーターはプログラムを実行できますが、上部の編集テキスト ボックスに「stad」と入力すると、URL から何も返されません。私が間違っていることを教えてください。これが私の 3 つのクラスと 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を試すと、Web ブラウザで

ページは次のとおりです。

エラーが発生しました!

次のエラー メッセージが生成されました。

[Thu Dec 6 23:50:06 2012] [エラー] 無効なパラメーター '場所': モントリオールは有効な値ではありません。

そして、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>

等...

それが役立つことを願っています、

于 2012-12-06T23:54:10.597 に答える
0

ソリューション編集:

これをマニフェストに追加すると、10 進数を 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 に答える