封装Utils类
public class MyOkUtils {
private Handler handler = new Handler();
private OkHttpClient client;
public MyOkUtils() {
Interceptor interceptor = new Interceptor() {
@Override
public Response intercept(Chain chain) throws IOException {
Request request = chain.request()
.newBuilder()
.build();
return chain.proceed(request);
}
};
client = new OkHttpClient.Builder()
.connectTimeout(20, TimeUnit.SECONDS)
.readTimeout(20, TimeUnit.SECONDS)
.addInterceptor(interceptor)
.build();
}
private static MyOkUtils myOkUtils = null;
public static MyOkUtils getInstance() {
if (myOkUtils == null) {
synchronized (Object.class) {
if (myOkUtils == null) {
myOkUtils = new MyOkUtils();
}
}
}
return myOkUtils;
}
public void doget(String url, final Oklistener oklistener) {
Request request = new Request.Builder()
.url(url)
.get()
.build();
client.newCall(request).enqueue(new Callback() {
@Override
public void onFailure(Call call, final IOException e) {
handler.post(new Runnable() {
@Override
public void run() {
oklistener.onError(e.getMessage());
}
});
}
@Override
public void onResponse(Call call, Response response) throws IOException {
final String string = response.body().string();
handler.post(new Runnable() {
@Override
public void run() {
oklistener.onOk(string);
}
});
}
});
}
public void dopost(String url, HashMap<String, String> map, final Oklistener oklistener) {
FormBody.Builder builder = new FormBody.Builder();
for (Map.Entry<String, String> entry : map.entrySet()) {
builder.add(entry.getKey(), entry.getValue());
}
Request request = new Request.Builder()
.url(url)
.post(builder.build())
.build();
client.newCall(request).enqueue(new Callback() {
@Override
public void onFailure(Call call, final IOException e) {
handler.post(new Runnable() {
@Override
public void run() {
oklistener.onError(e.getMessage());
}
});
}
@Override
public void onResponse(Call call, Response response) throws IOException {
final String string = response.body().string();
handler.post(new Runnable() {
@Override
public void run() {
oklistener.onOk(string);
}
});
}
});
}
public void download(String url, final String path, final ProgressListener progressListener) {
Request build = new Request.Builder()
.url(url)
.get()
.build();
client.newCall(build).enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
}
@Override
public void onResponse(Call call, Response response) throws IOException {
long length = response.body().contentLength();
int count = (int) (length / 3);
}
});
Request request = new Request.Builder()
.url(url)
.get()
.build();
client.newCall(request).enqueue(new Callback() {
@Override
public void onFailure(Call call, final IOException e) {
handler.post(new Runnable() {
@Override
public void run() {
progressListener.onError(e.getMessage());
}
});
}
@Override
public void onResponse(Call call, Response response) throws IOException {
long length = response.body().contentLength();
InputStream is = response.body().byteStream();
FileOutputStream fos = new FileOutputStream(new File(path));
int len = 0;
int count = 0;
byte b[] = new byte[1024];
while ((len = is.read(b)) != -1) {
count += len;
fos.write(b, 0, len);
final int progress = (int) (count * 100 / length);
handler.post(new Runnable() {
@Override
public void run() {
progressListener.onProgress(progress);
}
});
}
handler.post(new Runnable() {
@Override
public void run() {
progressListener.onFinish();
}
});
}
});
}
public void Upload(String url, String path, String serviceName, String type, final Oklistener oklistener) {
RequestBody requestBody = RequestBody.create(MediaType.parse(type), new File(path));
MultipartBody multipartBody = new MultipartBody.Builder()
.setType(MultipartBody.FORM)
.addFormDataPart("file", serviceName, requestBody)
.build();
Request request = new Request.Builder()
.url(url)
.post(multipartBody)
.build();
client.newCall(request).enqueue(new Callback() {
@Override
public void onFailure(Call call, final IOException e) {
handler.post(new Runnable() {
@Override
public void run() {
oklistener.onError(e.getMessage());
}
});
}
@Override
public void onResponse(Call call, Response response) throws IOException {
handler.post(new Runnable() {
@Override
public void run() {
oklistener.onOk("上传成功");
}
});
}
});
}
private long max;
private long number;
private int count;
private String str_url;
public void fenload(String url, final String path, final ProgressListener progressListener){
str_url = url;
Request request = new Request.Builder()
.url(url)
.get()
.build();
client.newCall(request).enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
}
@Override
public void onResponse(Call call, Response response) throws IOException {
max = response.body().contentLength();
Log.i("tag", "onResponse: ++++++总长:"+max);
number = max/3;
Request request1 = new Request.Builder()
.get()
.url(str_url)
.header("Range", "bytes=0-" + number)
.build();
client.newCall(request1).enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
}
@Override
public void onResponse(Call call, Response response) throws IOException {
InputStream is = response.body().byteStream();
RandomAccessFile randomAccessFile = new RandomAccessFile(path, "rw");
randomAccessFile.seek(0);
byte b[] = new byte[1024];
int len = 0;
while ((len = is.read(b))!=-1){
randomAccessFile.write(b,0,len);
count+=len;
final int progress = (int) (count*100/max);
handler.post(new Runnable() {
@Override
public void run() {
progressListener.onProgress(progress);
}
});
}
}
});
Request request2 = new Request.Builder()
.get()
.url(str_url)
.header("Range", "bytes="+number+"-"+number*2)
.build();
client.newCall(request2).enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
}
@Override
public void onResponse(Call call, Response response) throws IOException {
InputStream is = response.body().byteStream();
RandomAccessFile randomAccessFile = new RandomAccessFile(path, "rw");
randomAccessFile.seek(number);
byte b[] = new byte[1024];
int len = 0;
while ((len = is.read(b))!=-1){
randomAccessFile.write(b,0,len);
count+=len;
final int progress = (int) (count*100/max);
handler.post(new Runnable() {
@Override
public void run() {
progressListener.onProgress(progress);
}
});
}
}
});
Request request3 = new Request.Builder()
.get()
.url(str_url)
.header("Range", "bytes="+number*2+"-" + max)
.build();
client.newCall(request3).enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
}
@Override
public void onResponse(Call call, Response response) throws IOException {
InputStream is = response.body().byteStream();
RandomAccessFile randomAccessFile = new RandomAccessFile(path, "rw");
randomAccessFile.seek(number*2);
byte b[] = new byte[1024];
int len = 0;
while ((len = is.read(b))!=-1){
randomAccessFile.write(b,0,len);
count+=len;
final int progress = (int) (count*100/max);
handler.post(new Runnable() {
@Override
public void run() {
progressListener.onProgress(progress);
}
});
}
}
});
}
});
}
}
java 代码
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private Button bt_get;
private Button bt_post;
private Button bt_download;
private SeekBar seekBar;
private Button bt_upload;
private MyModel myModel = new MyModelimpl();
private Button bt_fenload;
private SeekBar seekBar2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
requestPermissions(new String[]{Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE}, 100);
initView();
}
private void initView() {
bt_get = (Button) findViewById(R.id.bt_get);
bt_post = (Button) findViewById(R.id.bt_post);
bt_download = (Button) findViewById(R.id.bt_download);
seekBar = (SeekBar) findViewById(R.id.seekBar);
bt_upload = (Button) findViewById(R.id.bt_upload);
bt_get.setOnClickListener(this);
bt_post.setOnClickListener(this);
bt_download.setOnClickListener(this);
bt_upload.setOnClickListener(this);
bt_fenload = (Button) findViewById(R.id.bt_fenload);
bt_fenload.setOnClickListener(this);
seekBar2 = (SeekBar) findViewById(R.id.seekBar2);
seekBar2.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.bt_get:
myModel.doget("http://www.qubaobei.com/ios/cf/dish_list.php?stage_id=1&limit=20&page=1", new Oklistener() {
@Override
public void onOk(String json) {
Toast.makeText(MainActivity.this, "" + json, Toast.LENGTH_SHORT).show();
}
@Override
public void onError(String message) {
Toast.makeText(MainActivity.this, "" + message, Toast.LENGTH_SHORT).show();
}
});
break;
case R.id.bt_post:
HashMap map = new HashMap();
map.put("limit", "20");
map.put("page", "1");
myModel.dopost("http://www.qubaobei.com/ios/cf/dish_list.php?stage_id=1&", map, new Oklistener() {
@Override
public void onOk(String json) {
Toast.makeText(MainActivity.this, "" + json, Toast.LENGTH_SHORT).show();
}
@Override
public void onError(String message) {
Toast.makeText(MainActivity.this, "" + message, Toast.LENGTH_SHORT).show();
}
});
break;
case R.id.bt_download:
myModel.DownLoad("http://p1.music.126.net/yC_df5u0myXVp-bM99K3Lw==/5870292580832850.jpg", "/storage/emulated/0/xxx.jpg", new ProgressListener() {
@Override
public void onProgress(int progress) {
seekBar.setProgress(progress);
}
@Override
public void onError(String message) {
Toast.makeText(MainActivity.this, "下载失败", Toast.LENGTH_SHORT).show();
}
@Override
public void onFinish() {
Toast.makeText(MainActivity.this, "下载完成", Toast.LENGTH_SHORT).show();
}
});
break;
case R.id.bt_upload:
myModel.UpLoad("http://169.254.114.118/hfs/", "/storage/emulated/0/xxx.jpg", "xxx.jpg", "media/jpg", new Oklistener() {
@Override
public void onOk(String json) {
Toast.makeText(MainActivity.this, "上传成功", Toast.LENGTH_SHORT).show();
}
@Override
public void onError(String message) {
Toast.makeText(MainActivity.this, "上传失败" + message, Toast.LENGTH_SHORT).show();
}
});
break;
case R.id.bt_fenload:
myModel.FenLoad("http://uvideo.spriteapp.cn/video/2019/1102/5dbcf34d0269d_wpd.mp4", "/storage/emulated/0/houzi.mp4", new ProgressListener() {
@Override
public void onProgress(int progress) {
seekBar2.setProgress(progress);
}
@Override
public void onError(String message) {
Toast.makeText(MainActivity.this, "下载失败"+message, Toast.LENGTH_SHORT).show();
}
@Override
public void onFinish() {
Toast.makeText(MainActivity.this, "下载完成", Toast.LENGTH_SHORT).show();
}
});
break;
}
}
}