不建议使用HTTPUtils。我该怎么办?

时间:2019-03-16 17:21:34

标签: java android

我需要从OpenWeatherMap网站下载图标,构建URL,下载图像,将其保存到本地存储中,还要检查它们是否已经存在。 HTTPUtils用红色下划线显示,当我查找它时,不再使用。教授提供了位图代码供使用。

    @Override
    protected String doInBackground(String... args) {

        try {
            URL url = new URL(TEMPS);
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            InputStream inputStream = conn.getInputStream();

            XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
            factory.setNamespaceAware(false);
            XmlPullParser xpp = factory.newPullParser();
            //this is what talks to the xml on the website
            xpp.setInput(inputStream, "UTF-8");

            while (xpp.getEventType() != XmlPullParser.END_DOCUMENT) {

                if (xpp.getEventType() == XmlPullParser.START_TAG) {
                    if (xpp.getName().equals("temperature")) {
                        curr = xpp.getAttributeValue(null, "value");
                        //tell android to call onProgressUpdate with 25 as parameter
                        publishProgress(25);

                        min = xpp.getAttributeValue(null, "min");
                        publishProgress(50);

                        max = xpp.getAttributeValue(null, "max");
                        publishProgress(75);

                    } else if (xpp.getName().equals("weather")) {
                        icon = xpp.getAttributeValue(null, "icon");
                    }
                }
                xpp.next();
            }

            //Start of JSON reading of UV factor:
            //create the network connection:
            URL UVurl = new URL(UV);
            HttpURLConnection UVConnection = (HttpURLConnection) UVurl.openConnection();
            inputStream = UVConnection.getInputStream();

            //create a JSON object from the response
            BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"), 8);
            StringBuilder sb = new StringBuilder();

            String line = null;
            while ((line = reader.readLine()) != null)
            {
                sb.append(line + "\n");
            }
            String result = sb.toString();

            //now a JSON table:
            JSONObject jObject = new JSONObject(result);
            double aDouble = jObject.getDouble("value");
            Log.i("UV is:", ""+ aDouble);
            uv = aDouble;
//*****This is where I need help
            Bitmap image = null;
            URL imageUrl = new URL(IMAGE);
            HttpURLConnection connection = (HttpURLConnection) imageUrl.openConnection();
            connection.connect();
            int responseCode = connection.getResponseCode();
                if (responseCode == 200) {
                    image = BitmapFactory.decodeStream(connection.getInputStream());
                }

            image = HTTPUtils.getImage(IMAGE);
            FileOutputStream outputStream = openFileOutput(icon + ".png", Context.MODE_PRIVATE);
            image.compress(Bitmap.CompressFormat.PNG, 80, outputStream);
            outputStream.flush();
            outputStream.close();

            public boolean fileExistance(String weatherIcons){
                File file = getBaseContext().getFileStreamPath(weatherIcons);
                return file.exists();
                Log.i("File name:", ""+ file);
            }
            FileInputStream fis = null;
                try {
                    fis = openFileInput("C:/Users/kathy/AndroidStudioProjects/AndroidLabs/app/src/main/res/drawable");
                } catch (FileNotFoundException e) {
                    Log.e("Download this file", e.getMessage());
                }
            Bitmap bm = BitmapFactory.decodeStream(fis);
            publishProgress(100);

                Thread.sleep(2000); //pause for 2000 milliseconds to watch the progress bar grow

        } catch (Exception ex) {
        }
        return null;
    }

1 个答案:

答案 0 :(得分:1)

虽然我不太了解HTTPUtils的来源,但我认为仍然可以依靠JDK和Android SDK的标准类。

try {
    // Get an open Stream to the image bytes
    final InputStream stream = new URL(IMAGE).openStream();

    // Wrap the Stream in a buffered one for optimization purposes
    // and decode it to a Bitmap
    try (final InputStream bufferedInputStream = new BufferedInputStream(stream)) {
        final Bitmap image = BitmapFactory.decodeStream(bufferedInputStream);
        // Process the image
    }
} catch (final IOException e) {
    // Handle the Exception
}

您可能想提取一个辅助方法,该方法仅返回实例化的Bitmap变量。