当前位置: 移动技术网 > IT编程>移动开发>Android > Android使用okHttp(get方式)下载图片

Android使用okHttp(get方式)下载图片

2019年07月24日  | 移动技术网IT编程  | 我要评论

张太勋,神话放送121013,dnf来自地狱的生物

一、首先下载jar包

 

如果使用android studio只需要加入依赖compile 'com.squareup.okhttp3:okhttp:3.2.0'  

二、下载一张图片并显示
使用的是hanlder的方式 

package com.liunan.okhttpdemo2;

import android.graphics.bitmap;
import android.graphics.bitmapfactory;
import android.os.handler;
import android.os.message;
import android.support.v7.app.appcompatactivity;
import android.os.bundle;
import android.view.view;
import android.widget.imageview;
import android.widget.toast;

import java.io.ioexception;
import java.io.inputstream;

import okhttp3.okhttpclient;
import okhttp3.request;
import okhttp3.response;
import okhttp3.responsebody;

public class mainactivity extends appcompatactivity {


 private static final int error = 1;
 private static final int success = 2 ;
 private string url = "http://192.168.1.102:8080/img/a.jpg";
 private imageview miv;

 @override
 protected void oncreate(bundle savedinstancestate) {
 super.oncreate(savedinstancestate);
 setcontentview(r.layout.activity_main);
 initview();

 }

 private handler handler = new handler(){
 @override
 public void handlemessage(message msg) {
  switch (msg.what){
  case success:
   miv.setimagebitmap((bitmap) msg.obj);
   break;

  case error:

   toast.maketext(mainactivity.this, "请求超时", toast.length_short).show();

   break;
  }
 }
 };

 /**
 * 初始化 组件
 */
 private void initview() {

 miv = (imageview) findviewbyid(r.id.main_iv);
 }


 /**
 * 点击获取图片
 */
 public void getpic(view v){


 new thread(){
  @override
  public void run() {
  //获取okhttp对象get请求,

  try {
   okhttpclient client = new okhttpclient();

   //获取请求对象
   request request = new request.builder().url(url).build();

   //获取响应体

   responsebody body = client.newcall(request).execute().body();

   //获取流
   inputstream in = body.bytestream();
   //转化为bitmap
   bitmap bitmap = bitmapfactory.decodestream(in);

   //使用hanlder发送消息
   message msg = message.obtain();

   msg.what = success;
   msg.obj = bitmap;

   handler.sendmessage(msg);



  } catch (ioexception e) {
   e.printstacktrace();
   //失败
   message msg = message.obtain();
   msg.what = error;

   handler.sendmessage(msg);
  }


  }
 }.start();
 }


}

也可以把网络请求写为一个工具类, 

package com.liunan.okhttpdemo2;

import java.io.ioexception;
import java.io.inputstream;

import okhttp3.okhttpclient;
import okhttp3.request;
import okhttp3.response;

/**
 * created by 刘楠 on 2016-03-27.
 */
public class okhttputils {

 okhttpclient client = new okhttpclient();

 /**
 * 获取流
 * @param url 请求地址
 * @return 输入流
 */
 public inputstream getinpustream(string url) throws ioexception {
 //设置 请求
 request request = new request.builder()
  .url(url).build();


 //获取行响应

 inputstream in = client.newcall(request).execute().body().bytestream();

 return in;


 }

 /**
 * 返回字符串
 * @param url
 * @return 返回字符串
 * @throws ioexception
 */
 public string getstring(string url) throws ioexception {
 //设置 请求
 request request = new request.builder()
  .url(url).build();


 //获取行响应

 response response = client.newcall(request).execute();

 return response.body().string();


 }

}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持移动技术网。

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网