一 .先导依赖`
implementation 'com.squareup.okhttp3:okhttp:3.11.0'
二.配置权限
上代码
MainActivity
package com.example.picture_dome;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;
import com.example.picture_dome.presenter.Presenter;
import com.example.picture_dome.presenter.SecondPresenter;
public class MainActivity extends AppCompatActivity implements View.OnClickListener, Presenter.onLoadData{
private Button down;
private Button chuan;
private ImageView img;
private Presenter mPresenter;
private SecondPresenter mSecondPresenter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
mPresenter = new Presenter(this);
mSecondPresenter = new SecondPresenter();
}
private void initView() {
down = (Button) findViewById(R.id.down);
chuan = (Button) findViewById(R.id.chuan);
img = (ImageView) findViewById(R.id.img);
down.setOnClickListener(this);
chuan.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.down:
mPresenter.loaddata();
break;
case R.id.chuan:
mSecondPresenter.loaddata();
break;
}
}
@Override
public void success(final byte[] bytes) {
runOnUiThread(new Runnable() {
@Override
public void run() {
Bitmap bitmap = BitmapFactory.decodeByteArray(bytes, 0, bytes.length);
img.setImageBitmap(bitmap);
}
});
}
@Override
public void error() {
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(MainActivity.this, "没有网络", Toast.LENGTH_SHORT).show();
}
});
}
}
布局
下载的实现 接口MVP P层
package com.example.picture_dome.presenter;
import java.io.IOException;
import java.util.concurrent.TimeUnit;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
/**
* date:2018/11/13
* author:赵豪轩(xuan)
* function:
*/
public class Presenter {
String path="https://10.url.cn/eth/ajNVdqHZLLAxibwnrOxXSzIxA76ichutwMCcOpA45xjiapneMZsib7eY4wUxF6XDmL2FmZEVYsf86iaw/";
public void loaddata() {
OkHttpClient okHttpClient = new OkHttpClient.Builder()
.writeTimeout(10, TimeUnit.SECONDS)
.readTimeout(10, TimeUnit.SECONDS)
.connectTimeout(10, TimeUnit.SECONDS)
.build();
Request request = new Request.Builder()
.url(path)
.build();
okHttpClient.newCall(request).enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
mOnLoadData.error();
}
@Override
public void onResponse(Call call, Response response) throws IOException {
byte[] bytes = response.body().bytes();
mOnLoadData.success(bytes);
}
});
}
public interface onLoadData{
void success(byte[] bytes);
void error();
}
private onLoadData mOnLoadData;
public Presenter(onLoadData onLoadData) {
mOnLoadData = onLoadData;
}
}
上传的实现
package com.example.picture_dome.presenter;
import android.os.Environment;
import android.util.Log;
import java.io.File;
import java.io.IOException;
import java.util.concurrent.TimeUnit;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;
/**
* date:2018/11/13
* author:赵豪轩(xuan)
* function:
*/
public class SecondPresenter {
String path="https://www.718shop.com/sell/sell.m.picture.upload.do";
public void loaddata() {
//上传的文件的对象
File file = new File(Environment.getExternalStorageDirectory()+"/Download", "cccc.jpg");
//创建RequestBody封装参数
RequestBody requestBody = RequestBody.create(MediaType.parse("application/octet-stream"), file);
//创建MultipartBody,给RequestBody进行设置
MultipartBody multipartBody = new MultipartBody.Builder()
.setType(MultipartBody.FORM)
.addFormDataPart("image", "cccc.jpg", requestBody)
.build();
OkHttpClient okHttpClient = new OkHttpClient.Builder()
.writeTimeout(10, TimeUnit.SECONDS)
.readTimeout(10, TimeUnit.SECONDS)
.connectTimeout(10, TimeUnit.SECONDS)
.build();
final Request request = new Request.Builder()
.url(path)
.post(multipartBody)
.build();
okHttpClient.newCall(request).enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
Log.e("zhx","错误" + e);
}
@Override
public void onResponse(Call call, Response response) throws IOException {
String string = response.body().string();
Log.e("zhx"," " + string );
}
});
}
}