我一直在做 新波士顿的天气教程
在 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>