欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

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

程序员文章站 2024-03-06 11:32:49
一、首先下载jar包   如果使用android studio只需要加入依赖compile 'com.squareup.okhttp3:okhttp:...

一、首先下载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();


 }

}

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