其他分享
首页 > 其他分享> > 安卓利用OKhttp3访问服务器图片资源

安卓利用OKhttp3访问服务器图片资源

作者:互联网

public class HomeFragment extends BaseFragment {
   private static final String TAG=HomeFragment.class.getSimpleName();
   private TextView textView;
   private ImageView imageView;
   private ListView listView;

   private Handler handler = new Handler(){
       @Override
       public void handleMessage(@NonNull Message msg) {
           Bitmap bitmap = (Bitmap) msg.obj;
           imageView.setImageBitmap(bitmap);
       }
   };

    @Override
    public View initView() {
        imageView=new ImageView(mContext);
        Log.e(TAG,"主页视图被加载了");

        OkHttpClient okHttpClient = new OkHttpClient();
        Request request = new Request.Builder().url(Constants.IMAGES).build();

        okHttpClient.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                Log.e("error","请求失败");
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                InputStream is = response.body().byteStream();
                Bitmap bitmap = BitmapFactory.decodeStream(is);
                Message message = new Message();
                message.obj = bitmap;
                handler.sendMessage(message);
            }
        });

        return imageView;
    }

url为:http://localhost:8080/images/background_img.jpg

标签:imageView,安卓,private,bitmap,OKhttp3,new,Override,服务器,public
来源: https://blog.csdn.net/YSZAYMT/article/details/117365098