0

我在以下行中收到错误 String weatherString = QueryYahooWeather(); 它显示“AndroidYahooWeatherDOMActivity 类型的方法 QueryYahooWeather() 未定义”

import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.apache.http.HttpEntity;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;  
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.xml.sax.SAXException;

import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

public class AndroidYahooWeatherDOMActivity extends Activity {

TextView weather;
TextView txtcity;
TextView txtregion;
TextView txtcountry;
TextView txttemp;
TextView txtpressure;
TextView txthumidity;
TextView txtspeed;
TextView txtsunrise;
TextView txtsunset;
TextView txtcondition;

TextView day1;
TextView date1;
TextView low1;
TextView high1;
TextView text1;
TextView day2;
TextView date2;
TextView low2;
TextView high2;
TextView text2;

ImageView img;
ImageView nxtday1;
ImageView nxtday2;


class MyWeather {
    WeatherImageHandler im = new WeatherImageHandler(getApplicationContext());
    WeatherImageHandler im1 = new WeatherImageHandler(getApplicationContext());
    WeatherImageHandler im2 = new WeatherImageHandler(getApplicationContext());
    String description;
    String city;
    String region;
    String country;

    String windChill;
    String windDirection;
    String windSpeed;

    String sunrise;
    String sunset;

    String conditiontext;
    String conditiondate;

    String temp;
    String pressure;
    String humidity;
    String visibility;
    String code;

    String rising;

    // //<yweather:forecast day="Wed" date="28 Nov 2012" low="65" high="87"
    // text="Partly Cloudy" code="29" />
    String day;
    String date;
    String low;
    String high;
    String text;
    String code0;

    String day1;
    String date1;
    String low1;
    String high1;
    String text1;
    String code1;

    String day2;
    String date2;
    String low2;
    String high2;
    String text2;
    String code2;
    //String[] arrDay = { "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun" };

    public String toString() {

        return "\n- " + description + " -\n\n" + "city: " + city + "\n"
                + "region: " + region + "\n" + "country: " + country
                + "\n\n"

                + "Wind\n" + "chill: " + windChill + "\n" + "direction: "
                + windDirection + "\n" + "speed: " + windSpeed + "\n\n"

                + "Sunrise: " + sunrise + "\n" + "Sunset: " + sunset
                + "\n\n"

                + "Condition: " + conditiontext + "\n" + conditiondate
                + "\n\n" + "temp:" + temp + "\n" + "pressure:" + pressure
                + "\n" + "humidity" + humidity + "\n" + "rising" + rising
                + "\n" + "visibility" + visibility + "\n"+"code  :" +code+"\n\n"

                + "day  :" + day
                + "\n" + "date  :" + date + "\n" + "low  :" + low + "\n" + "high  :"
                + high+ "\n" + "text  :" + text+"\n"+"code"+code0+"\n\n"

                + "day  :" + day1
                + "\n" + "date  :" + date1 + "\n" + "low  :" + low1 + "\n" + "high  :"
                + high1+ "\n" + "text  :" + text1+"code"+code1+"\n\n"

                + "day  :" + day2
                + "\n" + "date  :" + date2 + "\n" + "low  :" + low2 + "\n" + "high  :"
                + high2+ "\n" + "text  :" + text2+"code"+code2;


    }
}

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    //weather = (TextView) findViewById(R.id.weather);
    txtcity = (TextView) findViewById(R.id.city);
    txtregion = (TextView) findViewById(R.id.region);
    txtcountry = (TextView) findViewById(R.id.country);
    txttemp = (TextView) findViewById(R.id.temp);
    txtpressure = (TextView) findViewById(R.id.pressure);
    txthumidity = (TextView) findViewById(R.id.humidity);
    txtspeed = (TextView) findViewById(R.id.speed);
    txtsunrise = (TextView) findViewById(R.id.sunrise);
    txtsunset = (TextView) findViewById(R.id.sunset);
    txtcondition = (TextView) findViewById(R.id.condition);

    day1 = (TextView) findViewById(R.id.day1);
    date1 = (TextView) findViewById(R.id.date1);
    low1 = (TextView) findViewById(R.id.low1);
    high1 = (TextView) findViewById(R.id.high1);
    text1 = (TextView) findViewById(R.id.text1);

    day2 = (TextView) findViewById(R.id.day2);
    date2 = (TextView) findViewById(R.id.date2);
    low2 = (TextView) findViewById(R.id.low2);
    high2 = (TextView) findViewById(R.id.high2);
    text2 = (TextView) findViewById(R.id.text2);


    img = (ImageView) findViewById(R.id.imageView1);
    nxtday1 = (ImageView) findViewById(R.id.nextday1);
    nxtday2 = (ImageView) findViewById(R.id.nextday2);

    String weatherString = QueryYahooWeather();
    Document weatherDoc = convertStringToDocument(weatherString);

    MyWeather weatherResult = parseWeather(weatherDoc);
    txtcity.setText(weatherResult.city);
    txtregion.setText(weatherResult.region);
    txtcountry.setText(weatherResult.country);
    txttemp.setText(weatherResult.temp);
    txtpressure.setText(weatherResult.pressure);
    txthumidity.setText(weatherResult.humidity);
    txtspeed.setText(weatherResult.windSpeed);
    txtsunrise.setText(weatherResult.sunrise);
    txtsunset.setText(weatherResult.sunset);
    txtcondition.setText(weatherResult.date);

    day1.setText(weatherResult.day);
    date1.setText(weatherResult.date);
    low1.setText(weatherResult.low);
    high1.setText(weatherResult.high);
    text1.setText(weatherResult.text);

    day2.setText(weatherResult.day1);
    date2.setText(weatherResult.date1);
    low2.setText(weatherResult.low1);
    high2.setText(weatherResult.high1);
    text2.setText(weatherResult.text1);

    img.setImageDrawable(weatherResult.im.getWeatherImage());
    nxtday1.setImageDrawable(weatherResult.im1.getWeatherImage());
    nxtday2.setImageDrawable(weatherResult.im2.getWeatherImage());
}

    private MyWeather parseWeather(Document srcDoc) {

    MyWeather myWeather = new MyWeather();


    myWeather.description = srcDoc.getElementsByTagName("description")
            .item(0).getTextContent();


    Node locationNode = srcDoc.getElementsByTagName("yweather:location")
            .item(0);
    myWeather.city = locationNode.getAttributes().getNamedItem("city")
            .getNodeValue().toString();
    myWeather.region = locationNode.getAttributes().getNamedItem("region")
            .getNodeValue().toString();
    myWeather.country = locationNode.getAttributes()
            .getNamedItem("country").getNodeValue().toString();


    Node windNode = srcDoc.getElementsByTagName("yweather:wind").item(0);
    myWeather.windChill = windNode.getAttributes().getNamedItem("chill")
            .getNodeValue().toString();
    myWeather.windDirection = windNode.getAttributes()
            .getNamedItem("direction").getNodeValue().toString();
    myWeather.windSpeed = windNode.getAttributes().getNamedItem("speed")
            .getNodeValue().toString();


    Node atmosphereNode = srcDoc
            .getElementsByTagName("yweather:atmosphere").item(0);
    myWeather.humidity = atmosphereNode.getAttributes()
            .getNamedItem("humidity").getNodeValue().toString();
    myWeather.visibility = atmosphereNode.getAttributes()
            .getNamedItem("visibility").getNodeValue().toString();
    myWeather.pressure = atmosphereNode.getAttributes()
            .getNamedItem("pressure").getNodeValue().toString();
    myWeather.rising = atmosphereNode.getAttributes()
            .getNamedItem("rising").getNodeValue().toString();


    Node astronomyNode = srcDoc.getElementsByTagName("yweather:astronomy")
            .item(0);
    myWeather.sunrise = astronomyNode.getAttributes()
            .getNamedItem("sunrise").getNodeValue().toString();
    myWeather.sunset = astronomyNode.getAttributes().getNamedItem("sunset")
            .getNodeValue().toString();


    Node conditionNode = srcDoc.getElementsByTagName("yweather:condition")
            .item(0);
    myWeather.conditiontext = conditionNode.getAttributes()
            .getNamedItem("text").getNodeValue().toString();
    myWeather.conditiondate = conditionNode.getAttributes()
            .getNamedItem("date").getNodeValue().toString();
    myWeather.temp = conditionNode.getAttributes().getNamedItem("temp")
            .getNodeValue().toString();
    myWeather.code = conditionNode.getAttributes().getNamedItem("code")
            .getNodeValue().toString();
    myWeather.im.setWeatherImage(myWeather.code);


    Node forecastNode1 = srcDoc.getElementsByTagName("yweather:forecast")
            .item(0);
    myWeather.day = forecastNode1.getAttributes().getNamedItem("day")
            .getNodeValue().toString();
    myWeather.date = forecastNode1.getAttributes().getNamedItem("date")
            .getNodeValue().toString();
    myWeather.low = forecastNode1.getAttributes().getNamedItem("low")
            .getNodeValue().toString();
    myWeather.high = forecastNode1.getAttributes().getNamedItem("high")
            .getNodeValue().toString();
    myWeather.text = forecastNode1.getAttributes().getNamedItem("text")
            .getNodeValue().toString();
    myWeather.code0 = forecastNode1.getAttributes().getNamedItem("code")
            .getNodeValue().toString();
    myWeather.im1.setWeatherImage(myWeather.code0);


    Node forecastNode2 = srcDoc.getElementsByTagName("yweather:forecast")
            .item(1);
    myWeather.day1 = forecastNode2.getAttributes().getNamedItem("day")
            .getNodeValue().toString();
    myWeather.date1 = forecastNode2.getAttributes().getNamedItem("date")
            .getNodeValue().toString();
    myWeather.low1 = forecastNode2.getAttributes().getNamedItem("low")
            .getNodeValue().toString();
    myWeather.high1 = forecastNode2.getAttributes().getNamedItem("high")
            .getNodeValue().toString();
    myWeather.text1 = forecastNode2.getAttributes().getNamedItem("text")
            .getNodeValue().toString();
    myWeather.code1 = forecastNode1.getAttributes().getNamedItem("code")
            .getNodeValue().toString();
    myWeather.im2.setWeatherImage(myWeather.code1);



    return myWeather;
}

private Document convertStringToDocument(String src) {
    Document dest = null;

    DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
    DocumentBuilder parser;

    try {
        parser = dbFactory.newDocumentBuilder();
        dest = parser.parse(new ByteArrayInputStream(src.getBytes()));
    } catch (ParserConfigurationException e1) {
        e1.printStackTrace();
        Toast.makeText(AndroidYahooWeatherDOMActivity.this, e1.toString(),
                Toast.LENGTH_LONG).show();
    } catch (SAXException e) {
        e.printStackTrace();
        Toast.makeText(AndroidYahooWeatherDOMActivity.this, e.toString(),
                Toast.LENGTH_LONG).show();
    } catch (IOException e) {
        e.printStackTrace();
        Toast.makeText(AndroidYahooWeatherDOMActivity.this, e.toString(),
                Toast.LENGTH_LONG).show();
    }

    return dest;
}
public class weather extends AsyncTask<String, Integer, String>
{


@Override
protected String doInBackground(String... params) 
{
    // TODO Auto-generated method stub

    private String QueryYahooWeather () 
    {


    String qResult = "";
    String queryString = "http://weather.yahooapis.com/forecastrss?w=2295422";

    HttpClient httpClient = new DefaultHttpClient();
    HttpGet httpGet = new HttpGet(queryString);

    try {
        HttpEntity httpEntity = httpClient.execute(httpGet).getEntity();

        if (httpEntity != null) {
            InputStream inputStream = httpEntity.getContent();
            Reader in = new InputStreamReader(inputStream);
            BufferedReader bufferedreader = new BufferedReader(in);
            StringBuilder stringBuilder = new StringBuilder();

            String stringReadLine = null;

            while ((stringReadLine = bufferedreader.readLine()) != null) {
                stringBuilder.append(stringReadLine + "\n");
            }

            qResult = stringBuilder.toString();
        }

    } catch (ClientProtocolException e) {
        e.printStackTrace();
        Toast.makeText(AndroidYahooWeatherDOMActivity.this, e.toString(),
                Toast.LENGTH_LONG).show();
    } catch (IOException e) {
        e.printStackTrace();
        Toast.makeText(AndroidYahooWeatherDOMActivity.this, e.toString(),
                Toast.LENGTH_LONG).show();
    }

    return qResult;
}

}}

它还显示“语法错误,插入“}”以完成 MethodBody”,但每个大括号都完全闭合。

4

1 回答 1

2

看这个:

@Override
protected String doInBackground(String... params) 
{
    // TODO Auto-generated method stub

    private String QueryYahooWeather () 
    {

您试图在另一种方法中声明一种方法。你不能那样做。

这是你两个问题的主要根源。(大括号不正确,因为您需要doInBackground在开始QueryYahooWeather方法之前关闭,而QueryYahooWeather无法找到因为您没有正确声明它。)

接下来,您实际上已经发布了两个不同类的源代码(这并不明显,因为您没有适当地缩进代码,而且还有很多)。调用QueryYahooWeather是 within AndroidYahooWeatherDOMActivity,但方法本身是在weather类中声明的。

您应该移动该方法使用 的实例weather来调用它。我不清楚你应该如何重新设计这段代码,尽管我怀疑你必须这样做。除此之外,您不应该在 UI 线程中同步进行任何网络调用。您可能应该让您的异步任务回调以在完成时填充您的 UI,而不是尝试仅同步获取结果。

考虑到这个设计问题,我强烈建议您开始第二个项目只是为了做到这一点。你现在有大量的代码,其中大部分都不需要为“我如何查询雅虎天气并在 UI 中显示结果”的问题获得正确的设计解决方案。一旦你在一个更简单的项目中制定了正确的设计,你就可以将它应用到你更复杂的项目中。

此外,我建议重命名该方法以queryYahooWeather符合 Java 命名约定。同样,weather该类应该至少 Weather是,但最好是更具描述性的东西。最后,我建议将每个类放在自己的源文件中,以使您的项目更易于导航。

于 2013-02-13T07:04:48.457 回答