Android基于OkHttp实现下载和上传图⽚
本⽂实例为⼤家分享了OkHttp实现下载图⽚和上传图⽚的具体代码,供⼤家参考,具体内容如下
MainActivity.java
public class MainActivity extends AppCompatActivity {
private String Path = "10.url/eth/ajNVdqHZLLAxibwnrOxXSzIxA76ichutwMCcOpA45xjiapneMZsib7eY4wUxF6XDmL2FmZEVYsf86iaw/";  private static final int SUCCESS = 993;
private static final int FALL = 814;
Handler handler = new Handler() {
@Override
public void handleMessage(Message msg) {
switch (msg.what) {
//加载⽹络成功,进⾏UI的更新,处理得到的图⽚资源
case SUCCESS:
//通过message,拿到字节数组
byte[] Picture = (byte[]) msg.obj;
//使⽤BitmapFactory⼯⼚,把字节数组转换为bitmap
Bitmap bitmap = BitmapFactory.decodeByteArray(Picture, 0, Picture.length);
//通过ImageView,设置图⽚
mImageView_okhttp.setImageBitmap(bitmap);
break;
//当加载⽹络失败,执⾏的逻辑代码
case FALL:
Toast.makeText(MainActivity.this, "⽹络异常", Toast.LENGTH_SHORT).show();
break;
default:
break;
}
}
};
private ImageView mImageView_okhttp;
@Override
protected void onCreate(Bundle savedInstanceState) {
setContentView(R.layout.activity_main);
//初始化控件
initView();
}
private void initView() {
mImageView_okhttp = (ImageView) findViewById(R.id.imageView_okhttp);
}
/**
* 根据点击事件获取络上的图⽚资源,使⽤的是OKhttp框架
*
* @param view
*/
public void Picture_okhttp_bt(View view) {
//1. 创建OKhttpClient对象
OkHttpClient okHttpClient = new OkHttpClient();
//2.建⽴Request对象,设置参数,请求⽅式如果是get,就不⽤设置,默认使⽤的就是get
Request request = new Request.Builder()
.url(Path)//设置请求⽹址
.build();//建⽴request对象
//3.创建⼀个Call对象,参数是request对象,发送请求
Call call = wCall(request);
//4.异步请求,请求加⼊调度
@Override//请求失败回调
public void onFailure(Call call, IOException e) {
handler.sendEmptyMessage(FALL);
}
@Override//请求成功回调
public void onResponse(Call call, Response response) throws IOException {
//得到从⽹上获取资源,转换成我们想要的类型
byte[] Picture_bt = response.body().bytes();
//通过handler更新UI
Message message = handler.obtainMessage();
message.obj = Picture_bt;
message.what = SUCCESS;
handler.sendMessage(message);
}
});
}
//当按钮点击时,执⾏使⽤OKhttp上传图⽚到服务器(blog.csdn/tangxl2008008/article/details/51777355)
//注意:有时候上传图⽚失败,是服务器规定还要上传⼀个Key,如果开发中关于⽹络这⼀块出现问题,就多和⾯试官沟通沟通  public void uploading(View view) {
//图⽚上传接⼝地址
String url = "www.718shop/picture.upload.do";
//创建上传⽂件对象
File file = new ExternalStorageDirectory(), "big.jpg");
//创建RequestBody封装参数
RequestBody fileBody = ate(MediaType.parse("application/octet-stream"), file);
//创建MultipartBody,给RequestBody进⾏设置
MultipartBody multipartBody = new MultipartBody.Builder()
.setType(MultipartBody.FORM)
.addFormDataPart("image", "big.jpg", fileBody)
.build();
//创建Request
Request request = new Request.Builder()
.
url(url)
.post(multipartBody)
.build();
//创建okhttp对象
OkHttpClient okHttpClient = new OkHttpClient.Builder()
.connectTimeout(10, TimeUnit.SECONDS)
.readTimeout(10, TimeUnit.SECONDS)
.writeTimeout(10, TimeUnit.SECONDS)
.build();
//上传完图⽚,得到服务器反馈数据
@Override
public void onFailure(Call call, IOException e) {
Log.e("ff", "uploadMultiFile() e=" + e);
}
@Override
public void onResponse(Call call, Response response) throws IOException {
Log.i("ff", "uploadMultiFile() response=" + response.body().string());
}
});
}
}
l
<RelativeLayout
xmlns:android="schemas.android/apk/res/android"
xmlns:tools="schemas.android/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.sn.picture_okhttp.MainActivity">
<Button
android:id="@+id/button"
android:onClick="Picture_okhttp_bt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="下载图⽚"
connect下载android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="59dp"/>
<Button
android:text="上传图⽚"
android:onClick="uploading"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/button2"
android:layout_alignParentTop="true"
android:layout_alignLeft="@+id/button"
android:layout_alignStart="@+id/button"/>
<ImageView
android:id="@+id/imageView_okhttp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"/>
</RelativeLayout>
implementation 'com.squareup.okhttp3:okhttp:3.4.2'
以上就是本⽂的全部内容,希望对⼤家的学习有所帮助,也希望⼤家多多⽀持。

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。