java - 我可以在 onPostexecute 中使用 setImageResource 吗?

标签 java android android-asynctask imageview

我是新手/正在学习 Java 和 Android,并尝试制作一个简单的天气应用程序来告诉我当前位置的天气。我已经掌握了基础知识,并且正在使用 OpenWeatherMap API - 一切都很好。

我正在尝试执行以下操作: - 当API返回给我天气信息时,例如“碎云”或“大雨”,我想将ImageView设置为一张图片,例如碎云或大雨。

我似乎无法让 ImageView 获取图像(位于我的 Drawable 文件夹中) - 我尝试过使用 setImageResource、setImageDrawable 等。有什么指点吗?我错过了什么?

这是我的代码(重要的部分):

public class DownloadHtml extends AsyncTask<String, Void, String> {

    @Override
    protected String doInBackground(String... urls) {
        String result = "";
        URL url;
        HttpURLConnection urlConnection;

        try {

            url = new URL(urls[0]);
            urlConnection = (HttpURLConnection) url.openConnection();
            InputStream in = urlConnection.getInputStream();
            InputStreamReader reader = new InputStreamReader(in);
            int data = reader.read();

            while (data != -1) {

                char current = (char) data;
                result += current;
                data = reader.read();
            }

            return result;

        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        return null;
    }

    @Override
    protected void onPostExecute(String result) {
        super.onPostExecute(result);

        try {
            JSONObject jsonObject = new JSONObject(result);
            String weatherInfo = jsonObject.getString("weather");
            String tempInfo = jsonObject.getJSONObject("main").getString("temp");
            String windInfo = jsonObject.getJSONObject("wind").getString("deg");

            JSONArray weatherArr = new JSONArray(weatherInfo);
            for (int i = 0; i < weatherArr.length(); i++) {

                TextView weatherTextView = findViewById(R.id.weatherTextView);
                weatherTextView.setText("");
                JSONObject jsonPart = weatherArr.getJSONObject(i);
                String description = jsonPart.getString("description");
                weatherTextView.setText(description);
                iv = findViewById(R.id.imageView2);


                if (description == "clear sky") iv.setImageResource(R.drawable.clear_skies);

                else if (description == "few clouds")
                    iv.setImageResource(R.drawable.few_clouds);

                else if (description == "scattered clouds")
                    iv.setImageResource(R.drawable.scattered_clouds);

                else if (description == "broken clouds")
                    iv.setImageResource(R.drawable.broken_clouds);

                else if (description == "shower rain")
                    iv.setImageResource(R.drawable.shower_rain);

                else if (description == "rain") iv.setImageResource(R.drawable.heavy_rain);

                else if (description == "thunderstorm")
                    iv.setImageResource(R.drawable.thunderstorm);

                else if (description == "snow") iv.setImageResource(R.drawable.snow);

                else if (description == "mist") iv.setImageResource(R.drawable.mist);

                Log.i("description", jsonPart.getString("description"));

                if (description != null) {
                    locationManager.removeUpdates(locationListener);
                }
            }

            for (int f = 0; f < tempInfo.length(); f++) {
                TextView weatherTextView3 = findViewById(R.id.weatherTextView3);
                weatherTextView3.setText("");
                String tempString = tempInfo + " degrees celcius.\n";
                weatherTextView3.setText(tempString);
                Log.i("temp: ", tempInfo);


                if (tempString != null) {
                    locationManager.removeUpdates(locationListener);
                    f = 500;
                }
            }

            for (int j = 0; j < windInfo.length(); j++) {
                TextView weatherTextView2 = findViewById(R.id.weatherTextView2);
                weatherTextView2.setText("");
                // assign text to wind direction
                double  windDegrees = Double.valueOf(windInfo);
                if ((windDegrees > 0) && (windDegrees < 90)) {
                    String windString = "A North East Wind.\n";
                    weatherTextView2.setText(windString);
                    Log.i("Wind: ", windInfo);

                }
                if ((windDegrees >= 90) && (windDegrees < 180)) {
                    String windString = "A South East Wind.\n";
                    weatherTextView2.append(windString);
                    Log.i("Wind: ", windInfo);

                }
                if ((windDegrees >= 180) && (windDegrees < 270)) {
                    String windString = "A South West Wind.\n";
                    weatherTextView2.append(windString);
                    Log.i("Wind: ", windInfo);

                }
                if ((windDegrees >= 270) && (windDegrees < 360)) {
                    String windString = "A North West Wind.\n";
                    weatherTextView2.append(windString);
                    Log.i("Wind: ", windInfo);

                }
                if (windInfo != null) {
                    locationManager.removeUpdates(locationListener);
                    j = 500;
                }
            }

        } catch (JSONException e) {
            e.printStackTrace();
        }

    }
}

最佳答案

您只是不小心使用了 == 来比较字符串:)

使用description.equals("clear sky")代替description == "clear sky"

关于java - 我可以在 onPostexecute 中使用 setImageResource 吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48510764/

相关文章:

java - 两个不同大小的排序数组的中位数

java - 野蝇: is possible to define websocket endpoint inside an EJB module of an EAR?

java - Eclipse 预测弹出子菜单的方向

android - 更改android中进度条的颜色

Android:向 ArrayAdapter 添加数据时可见的 ListView 图像闪烁

java - Spring MVC 中服务器向客户端发送消息

java - 正确使用附加类文件中的方法

java - 从 Android,无法连接到我的 Java ServerSocket

java - 创建多个AsyncTask

java - Android Filter类performFiltering方法