当前位置:主页 > 移动开发 > Android代码 >

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

时间:2020-12-27 16:27:20 | 栏目:Android代码 | 点击:

一、首先下载Jar包

https://github.com/square/okhttp 

如果使用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();


 }

}

您可能感兴趣的文章:

相关文章