java - AsyncTask 中的循环器

标签 java android dom android-asynctask

我正在学习 HTML 并使用 XML DOM 解析数据。为此,我创建了一个从雅虎的wheater API 读取Wheater 的应用程序。

执行应用程序时,在 logcat 中显示错误:java.lang.RuntimeException: Can't create handler inside thread that has not caller Looper.prepare()。

我不知道这意味着什么,也不知道代码是否正确。

这是雅虎的wheater API的XML文件的链接:

http://weather.yahooapis.com/forecastrss?w=766273&u=c

这是我的代码:

public class WeatherActivity extends Activity {

private static final String WEATHER_URL = "http://weather.yahooapis.com/forecastjson?w=";
private static final String MADRID_CODE = "766273";

private static final String LOCATION_NAME = "location";
private static final String CITY_NAME = "city";
private static final String CONDITION_NAME = "condition";
private static final String TEMPERATURE_NAME = "temperature";
private static final String FORECAST_NAME = "forecast";
private static final String DAY_NAME = "day";
private static final String HIGH_TEMPERATURE_NAME = "high_temperature";
private static final String LOW_TEMPERATURE_NAME = "low_temperature";

private static final String TODAY = "Today";
private static final String TOMORROW = "Tomorrow";

private Button mButton;
private TextView mCity;
private TextView mToday;
private TextView mTomorrow;


private class WeatherInfo {
    String city;
    int temperatureNow;
    int lowTemperature;
    int highTemperature;
    int lowTemperatureTomorrow;
    int highTemperatureTomorrow;
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    mCity = (TextView) findViewById(R.id.city);
    mToday = (TextView) findViewById(R.id.today);
    mTomorrow = (TextView) findViewById(R.id.tomorrow);

    mButton = (Button) findViewById(R.id.button);
    mButton.setOnClickListener(new OnClickListener() {

        public void onClick(View v) {
            launch();
        }
    });
}

private void launch(){
    try {
        new WeatherAsyncTask().execute(MADRID_CODE);
    } catch (IllegalArgumentException e) {
        e.printStackTrace();
        Toast.makeText(WeatherActivity.this, e.getMessage(), Toast.LENGTH_SHORT).show();
    }
}


private class WeatherAsyncTask extends AsyncTask<String, Void, WeatherInfo>{

    @Override
    protected WeatherInfo doInBackground(String... params) {
        String code = params[0];
        if (TextUtils.isEmpty(code))
            throw new IllegalArgumentException("Code cannot be empty");

        URL url = null;
        HttpURLConnection connection = null;

        try {
            url = new URL(WEATHER_URL + code);
            connection = (HttpURLConnection) url.openConnection();
            connection.setRequestMethod("GET");

            InputStream is = connection.getInputStream();
            WeatherInfo info = readWeatherInfo(is);
            return info;

        } catch (IOException e) {
            e.printStackTrace();
            Toast.makeText(WeatherActivity.this, e.getMessage(), Toast.LENGTH_SHORT).show();
        } finally {
            if (connection != null)
                connection.disconnect();
        }
        return null;
    }

    @Override
    protected void onPostExecute(WeatherInfo result) {
        super.onPostExecute(result);
        showResult(result);
    }



    private WeatherInfo readWeatherInfo(InputStream is){
        if (is == null)
            return null;

        WeatherInfo info = new WeatherInfo();

        try {

            DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
            DocumentBuilder builder = factory.newDocumentBuilder();
            Document dom = builder.parse(is);
            Element root = dom.getDocumentElement();
            NodeList items = root.getElementsByTagName("item");

            for (int i=0; i<items.getLength(); i++) {
                Node item = items.item(i);
                NodeList datos = item.getChildNodes();

                for (int j=0; j<datos.getLength(); j++) {
                    Node dato = datos.item(j);
                    String etiqueta = dato.getNodeName();

                    if (etiqueta.equals(LOCATION_NAME)) {
                        String texto = obtenerTexto(dato);
                        if (texto.equals(TEMPERATURE_NAME)) {
                            info.city = texto;
                        }
                    }

                    else if (etiqueta.equals(CONDITION_NAME)) {
                        String texto = obtenerTexto(dato);
                        if (texto.equals(CITY_NAME)) {
                            info.temperatureNow = Integer.parseInt(texto);
                        }
                    }
                    else if (etiqueta.equals(FORECAST_NAME)) {
                        String texto = obtenerTexto(dato);
                        String day = null;
                        int high = -111;
                        int low = -111;

                        if (texto.equals(DAY_NAME)){
                            day = texto;
                        } else if (texto.equals(HIGH_TEMPERATURE_NAME)){
                            high = Integer.parseInt(texto);
                        }  else if (texto.equals(LOW_TEMPERATURE_NAME)){
                            low = Integer.parseInt(texto);
                        }

                        if (day.equals(TODAY)){
                            info.highTemperature = high;
                            info.lowTemperature = low;
                        } else if (day.equals(TOMORROW)){
                            info.highTemperatureTomorrow = high;
                            info.lowTemperatureTomorrow = low;
                        }
                    }
                }
            }
        }
        catch (Exception ex)
        {
            throw new RuntimeException(ex);
        }

        return info;
    }


    private String obtenerTexto(Node dato) {
        StringBuilder texto = new StringBuilder();
        NodeList fragmentos = dato.getChildNodes();

        for (int k=0;k<fragmentos.getLength();k++) {
            texto.append(fragmentos.item(k).getNodeValue());
        }
        return texto.toString();
    }

}

private void showResult(WeatherInfo info){
    mCity.setText("Temperature in " + info.city);
    mToday.setText("Today: " + info.temperatureNow + " F (min: " + info.lowTemperature + " F / max: " + info.highTemperature + " F).");
    mTomorrow.setText("Tomorrow: min: " + info.lowTemperatureTomorrow + " F / max: " + info.highTemperatureTomorrow + " F.");
}


}

最佳答案

您无法在 ASyncTaskdoInBackground 中显示 Toast

尝试将其包装在 runOnUIThread() 中,例如:

runOnUiThread(new Runnable() {
    @Override
    public void run() {
        Toast.makeText(WeatherActivity.this, e.getMessage(), Toast.LENGTH_SHORT).show();
    }
});

关于java - AsyncTask 中的循环器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21402225/

相关文章:

java - 在java中创建一个带有字符串拆分函数的数组

java - 是否可以在 IntelliJ IDEA 中找到所有 (java) "foreach"循环?

Java Path2D.closePath 无法正常工作?

android - Google Analytics v4 Android 未找到广告系列数据

Android,应用程序在锁屏启动后重新启动

reactjs - Material UI 上的“keepMounted”属性选择组件未将菜单项安装到 DOM

java - 如何将字符串拆分为重叠的字符对

android - Firebase 数据库的常用时间

javascript - 如何使用 JavaScript 跨页面加载获取元标记的内容?

javascript - 无法用 JS 理解这个 DOM 目标