网络请求
public class Uitls extends AsyncTask<String,Integer,String> {
private String path;
private int end;
private long start;
private Handler handler;
private Context context;
private InputStream inputStream;
private RandomAccessFile rw;
public Uitls(String path, Handler handler, Context context) {
this.path = path;
this.handler = handler;
this.context = context;
}
@Override
protected String doInBackground(String... strings) {
try {
URL url = new URL(strings[0]);
URLConnection urlConnection = url.openConnection();
urlConnection.connect();
end = urlConnection.getContentLength();
handler.post(new Runnable() {
@Override
public void run() {
Share.progressBar.setVisibility(View.VISIBLE);
Share.progressBar.setMax(end);
}
});
} catch (Exception e) {
e.printStackTrace();
}
File file = new File(path);
if (file.exists()){
start = file.length();
}else {
start = 0;
}
try {
URL url = new URL(strings[0]);
URLConnection connection = url.openConnection();
connection.setRequestProperty("Range","bytes="+start+"-"+end);
connection.connect();
inputStream = connection.getInputStream();
rw = new RandomAccessFile(path, "rw");
int len = 0;
byte[] bys = new byte[1024];
while ((len = inputStream.read(bys)) != -1){
rw.write(bys,0,len);
start+= len;
publishProgress((int) start);
}
Log.i("---", "doInBackground: "+Share.lists.get(Share.index).getTitle());
SharedPreferences sharedPreferences = context.getSharedPreferences(Share.lists.get(Share.index).getTitle(), Context.MODE_PRIVATE);
SharedPreferences.Editor edit = sharedPreferences.edit();
edit.putString("picName",Share.lists.get(Share.index).getTitle());
edit.commit();
} catch (Exception e) {
e.printStackTrace();
}finally {
if (inputStream!=null){
try {
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}else if (rw!=null){
try {
rw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return null;
}
@Override
protected void onProgressUpdate(Integer... values) {
super.onProgressUpdate(values);
Share.progressBar.setProgress(values[0]);
}
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
Share.progressBar.setVisibility(View.GONE);
}
}
适配器
public class MyAdapter extends BaseAdapter {
private List<JavaBean.DataBean> lists;
private Context context;
private LayoutInflater layoutInflater;
public MyAdapter(List<JavaBean.DataBean> lists, Context context) {
this.lists = lists;
this.context = context;
layoutInflater = LayoutInflater.from(context);
}
@Override
public int getCount() {
return lists.size();
}
@Override
public Object getItem(int position) {
return lists.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
if(convertView == null){
holder = new ViewHolder();
convertView = layoutInflater.inflate(R.layout.item_layout,null);
holder.textView_title = convertView.findViewById(R.id.textView_title);
holder.imageView_Pic = convertView.findViewById(R.id.imageView_Pic);
convertView.setTag(holder);
}else {
holder = (ViewHolder) convertView.getTag();
}
Glide.with(context).load(lists.get(position).getPic()).into(holder.imageView_Pic);
holder.textView_title.setText(lists.get(position).getTitle());
return convertView;
}
class ViewHolder{
private TextView textView_title;
private ImageView imageView_Pic;
}
}
java代码
public class MainActivity extends AppCompatActivity {
private ListView listView;
private List<JavaBean.DataBean> lists = new ArrayList<>();
private MyAdapter adapter;
private AlertDialog.Builder builder;
private ProgressBar pb;
private String path = "/storage/emulated/0/";
private Handler handler = new Handler();
private String picName;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
requestPermissions(
new String[]{
Manifest.permission.READ_EXTERNAL_STORAGE,
Manifest.permission.WRITE_EXTERNAL_STORAGE
}, 100
);
}
private void initView() {
listView = (ListView) findViewById(R.id.listView);
pb = (ProgressBar) findViewById(R.id.pb);
Share.progressBar = pb;
OkGo.<String>get("http://www.qubaobei.com/ios/cf/dish_list.php?stage_id=1&limit=10&page=1").execute(new StringCallback() {
@Override
public void onSuccess(Response<String> response) {
String json = response.body();
JavaBean javaBean = JSON.parseObject(json, JavaBean.class);
lists.addAll(javaBean.getData());
Share.lists = lists;
adapter = new MyAdapter(lists, MainActivity.this);
listView.setAdapter(adapter);
}
});
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, final int position, long id) {
Share.index = position;
SharedPreferences sharedPreferences = getSharedPreferences(lists.get(position).getTitle(),MODE_PRIVATE);
picName = sharedPreferences.getString("picName", "no");
builder = new AlertDialog.Builder(MainActivity.this);
builder.setIcon(R.mipmap.ic_launcher);
builder.setTitle("");
builder.setMessage("是否下载");
builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
if (picName.equals("no")){
new Uitls(path+"img"+Share.index+".jpg",handler,MainActivity.this).execute(lists.get(position).getPic());
Toast.makeText(MainActivity.this, "下载成功", Toast.LENGTH_SHORT).show();
}else {
Toast.makeText(MainActivity.this, "图片已下载!", Toast.LENGTH_SHORT).show();
}
}
});
builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
});
AlertDialog alertDialog = builder.create();
alertDialog.show();
}
});
}
}