Android使用okHttp(get方式)下載圖片_Android

來源:互聯網
上載者:User

一、首先下載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 = "yun_qi_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(); }}

以上就是本文的全部內容,希望對大家的學習有所協助,也希望大家多多支援雲棲社區。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.