一、首先下载jar包 https://GitHub.com/square/okHttp 如果使用Android studio只需要加入依赖compile 'co
一、首先下载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;
public class OkHttpUtils {
OkHttpClient client = new OkHttpClient();
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;
}
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();
}
}
您可能感兴趣的文章:详解Android中使用OkHttp发送HTTP的post请求的方法Android中Retrofit+OkHttp进行HTTP网络编程的使用指南详解Android使用OKHttp3实现下载(断点续传、显示进度)Android的OkHttp包中的HTTP拦截器Interceptor用法示例Android中Okhttp3实现上传多张图片同时传递参数Android OkHttp Post上传文件并且携带参数实例详解Android中的Retrofit+OkHttp+RxJava缓存架构使用Android中okhttp3使用详解android中实现OkHttp下载文件并带进度条Android Okhttp断点续传面试深入解析
--结束END--
本文标题: Android使用okHttp(get方式)下载图片
本文链接: https://lsjlt.com/news/24317.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
2024-01-21
2023-10-28
2023-10-28
2023-10-27
2023-10-27
2023-10-27
2023-10-27
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0