本文实例为大家分享了OkHttp实现下载图片和上传图片的具体代码,供大家参考,具体内容如下
MainActivity.java
public class MainActivity extends AppCompatActivity { private String Path = "https://10.url.cn/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) { super.onCreate(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 = okHttpClient.newCall(request); //4.异步请求,请求加入调度 call.enqueue(new Callback() { @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上传图片到服务器(http://blog.csdn.net/tangxl2008008/article/details/51777355) //注意:有时候上传图片失败,是服务器规定还要上传一个Key,如果开发中关于网络这一块出现问题,就多和面试官沟通沟通 public void uploading(View view) { //图片上传接口地址 String url = "https://www.718shop.com/sell/sell.m.picture.upload.do"; //创建上传文件对象 File file = new File(Environment.getExternalStorageDirectory(), "big.jpg"); //创建RequestBody封装参数 RequestBody fileBody = RequestBody.create(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(); //上传完图片,得到服务器反馈数据 okHttpClient.newCall(request).enqueue(new Callback() { @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()); } }); } }
activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/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="下载图片" 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>
build.gradle //依赖
implementation 'com.squareup.okhttp3:okhttp:3.4.2'
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持找一找教程网。