如何在滑行时使用httpUrlConnection

时间:2016-10-07 01:15:54

标签: android android-glide

我想使用各种方法,如setRequestMethod,setRequestProperty

以下是我需要使用滑动库而不是异步任务来实现它的代码

提前致谢。

public class ImageLoadingAsyncTask extends AsyncTask<Integer, Void, Void> {
        Bitmap bmp;

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            pb_imageProgressBar.setVisibility(VISIBLE);
        }

        @Override
        protected Void doInBackground(Integer... integers) {

            String url = PrefManager.getInstanceUrl()
                    + "clients/"
                    + integers[0]
                    + "/images?maxHeight=120&maxWidth=120";
            Log.d("ashu",url);
            try {
                HttpURLConnection httpURLConnection = (HttpURLConnection) (new URL(url))
                        .openConnection();
                httpURLConnection.setRequestMethod("GET");
                httpURLConnection.setRequestProperty(MifosInterceptor.HEADER_TENANT,
                        "default");
                httpURLConnection.setRequestProperty(MifosInterceptor.HEADER_AUTH,
                        PrefManager.getToken());
                httpURLConnection.setRequestProperty("Accept", "application/octet-stream");
                httpURLConnection.setDoInput(true);
                httpURLConnection.connect();
                InputStream inputStream = httpURLConnection.getInputStream();
                bmp = BitmapFactory.decodeStream(inputStream);
                httpURLConnection.disconnect();
            } catch (MalformedURLException e) {
            } catch (IOException ioe) {
            }
            return null;
        }


        @Override
        protected void onPostExecute(Void aVoid) {
            if (bmp != null) {
                iv_clientImage.setImageBitmap(bmp);
            } else {
                iv_clientImage.setImageDrawable(
                        ContextCompat.getDrawable(getActivity(), R.drawable.ic_launcher));
                pb_imageProgressBar.setVisibility(GONE);
            }

        }
    }

1 个答案:

答案 0 :(得分:0)

如果您使用Glide,则不需要HttpUrlConnection,只需直接致电Glide。

String url = PrefManager.getInstanceUrl()
                    + "clients/"
                    + integers[0]
                    + "/images?maxHeight=120&maxWidth=120";

GlideUrl glideUrl = new GlideUrl(url, new LazyHeaders.Builder()
                .addHeader((MifosInterceptor.HEADER_TENANT, "default")
                .addHeader(MifosInterceptor.HEADER_AUTH, PrefManager.getToken())
                .addHeader("Accept", "application/octet-stream")
                .build());
Glide.with(getActivity()).load(glideUrl).into(iv_clientImage);