我可以在onPostexecute中使用setImageResource吗?

时间:2018-01-29 22:05:32

标签: 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();
        }

    }
}

1 个答案:

答案 0 :(得分:1)

您只是意外地使用==来比较字符串:)

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