断点续传和封装线程
1.线程封装 Post请求和下载
post请求
1.model
public interface HttpModel {
void post(String str, String params, Handler handler);
void post(String str, String params, Handler handler);
void download(String str,String path,Handler handler);
void break_point(String str,String path,Handler handler);
}
2.model实现类
public class HttpModelImpl implements HttpModel {
@Override
public void post(String str, String params, Handler handler) {
new PostThread(str,params,handler).start();
}
@Override
public void download(String str, String path, Handler handler) {
new DownloadThread(str,path,handler).start();
}
@Override
public void break_point(String str, String path, Handler handler) {
new BreakPointThread(str,path,handler).start();
}
}
3.post线程
public class PostThread extends Thread {
private String str;
private String parms;
private Handler handler;
public PostThread(String str, String parms, Handler handler) {
this.str = str;
this.parms = parms;
this.handler = handler;
}
@Override
public void run() {
super.run();
StringBuffer stringBuffer = new StringBuffer();
try {
URL url = new URL(str);
HttpURLConnection connection=null;
connection = (HttpURLConnection) url.openConnection();
connection.setConnectTimeout(60*1000);
connection.setReadTimeout(60*1000);
connection.setRequestMethod("POST");
connection.setDoInput(true);
connection.setDoOutput(true);
connection.getOutputStream().write(parms.getBytes());
connection.connect();
byte[] bytes = new byte[1024];
int len=0;
InputStream inputStream = connection.getInputStream();
if (connection.getResponseCode()==200){
while((len=inputStream.read(bytes))!=-1){
stringBuffer.append(new String(bytes,0,len));
}
Message obtain = Message.obtain();
obtain.what=101;
obtain.obj = stringBuffer.toString();
handler.sendMessage(obtain);
}else{
Message obtain = Message.obtain();
obtain.what=102;
obtain.obj="失败";
handler.sendMessage(obtain);
}
} catch (IOException e) {
Message obtain = Message.obtain();
obtain.what=102;
obtain.obj=e.getMessage();
handler.sendMessage(obtain);
}
}
}
4.activity代码
package com.example.homewoek0902;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import android.Manifest;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.Toast;
import com.example.homewoek0902.model.HttpModel;
import com.example.homewoek0902.model.HttpModelImpl;
public class MainActivity extends AppCompatActivity {
private Button get;
private Button post;
private Button download;
private Button break_point;
private ProgressBar progressbar;
private HttpModel model;
private Handler handler = new Handler(){
@Override
public void handleMessage(@NonNull Message msg) {
super.handleMessage(msg);
if (msg.what==101){
Toast.makeText(MainActivity.this, ""+msg.obj.toString(), Toast.LENGTH_SHORT).show();
}else if (msg.what==102){
Toast.makeText(MainActivity.this, "失败了"+msg.obj.toString(), Toast.LENGTH_SHORT).show();
}else if (msg.what==103){
progressbar.setProgress((Integer) msg.obj);
}
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
get = (Button) findViewById(R.id.get);
post = (Button) findViewById(R.id.post);
download = (Button) findViewById(R.id.download);
break_point = (Button) findViewById(R.id.break_point);
progressbar = (ProgressBar) findViewById(R.id.progressbar);
if (Build.VERSION.SDK_INT>=Build.VERSION_CODES.M){
requestPermissions(new String[]{Manifest.permission.READ_EXTERNAL_STORAGE,Manifest.permission.WRITE_EXTERNAL_STORAGE},100);
}
model = new HttpModelImpl();
get.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
}
});
post.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
model.post("http://api.yunzhancn.cn/api/app.interface.php?siteid=78703&","itemid=2&act=ad_app",handler);
}
});
download.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
model.download("http://uvideo.spriteapp.cn/video/2019/0512/56488d0a-7465-11e9-b91b-1866daeb0df1_wpd.mp4","/sdcard/aaaa.mp4",handler);
}
});
break_point.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
model.break_point("http://uvideo.spriteapp.cn/video/2019/0512/56488d0a-7465-11e9-b91b-1866daeb0df1_wpd.mp4","/sdcard/aaaa.mp4",handler);
}
});
}
}
5.Download线程
package com.example.homewoek0902.thread;
import android.os.Handler;
import android.os.Message;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
public class DownloadThread extends Thread {
private String str;
private String path;
private Handler handler;
public DownloadThread(String str, String path, Handler handler) {
this.str = str;
this.path = path;
this.handler = handler;
}
@Override
public void run() {
super.run();
try {
URL url = new URL(str);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
connection.connect();
int max = connection.getContentLength();
if (connection.getResponseCode()==200){
InputStream inputStream = connection.getInputStream();
FileOutputStream fileOutputStream = new FileOutputStream(path);
byte[] bytes = new byte[1024];
int len=0;
int count=0;
while((len=inputStream.read(bytes))!=-1){
fileOutputStream.write(bytes,0,len);
count+=len;
Message obtain = Message.obtain();
obtain.what=103;
obtain.obj=count*100/max;
handler.sendMessage(obtain);
}
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
6.断点续传
package com.example.homewoek0902.thread;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.RandomAccessFile;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
public class BreakPointThread extends Thread{
private String str;
private String path;
private Handler handler;
public BreakPointThread(String str, String path, Handler handler) {
this.str = str;
this.path = path;
this.handler = handler;
}
int max;
int start;
@Override
public void run() {
super.run();
try {
URL url = new URL(str);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
connection.connect();
max = connection.getContentLength();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
File file = new File(path);
if (file.exists()){
start= (int) file.length();
}
Log.d("zjy", "start "+start+"end"+max);
try {
URL url = new URL(str);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
connection.setRequestProperty("Range","bytes="+start+"-"+max);
connection.connect();
if (connection.getResponseCode()==206){
InputStream inputStream = connection.getInputStream();
RandomAccessFile randomAccessFile = new RandomAccessFile(path, "rw");
randomAccessFile.seek(start);
byte[] bytes = new byte[1024];
int len=0;
int count=start;
while((len=inputStream.read(bytes))!=-1){
randomAccessFile.write(bytes,0,len);
count+=len;
Message obtain = Message.obtain();
obtain.what=103;
obtain.obj=count*100/max;
handler.sendMessage(obtain);
}
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}