<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS" />
<uses-permission android:name="android.permission.WRITE_INTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_INTERNAL_STORAGE" />
public class Uris {
public static final String uri = "http://mapp.qzone.qq.com/cgi-bin/mapp/mapp_subcatelist_qq?yyb_cateid=-10&categoryName=%E8%85%BE%E8%AE%AF%E8%BD%AF%E4%BB%B6&pageNo=1&pageSize=20&type=app&platform=touch&network_type=unknown&resolution=412x732";
}
引用xutils3.0的包
public class MyApplicalion extends Application {
//初始化xUtils
@Override
public void onCreate() {
super.onCreate();
x.Ext.init(this);
}
}
public class MyAdapter extends BaseAdapter {
private List<AppBean> mList;
private Context mContext;
public MyAdapter(List<AppBean> list, Context context) {
mList = list;
mContext = context;
}
@Override
public int getCount() {
return mList.size();
}
@Override
public Object getItem(int position) {
return mList.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null){
convertView = View.inflate(mContext, R.layout.item,null);
holder = new ViewHolder();
holder.mTextView = (TextView) convertView.findViewById(R.id.title_text);
convertView.setTag(holder);
}else
holder = (ViewHolder) convertView.getTag();
holder.mTextView.setText(mList.get(position).getName());
return convertView;
}
class ViewHolder{
TextView mTextView;
}
}
public class MainActivity extends AppCompatActivity {
private XListView lv;
private List<AppBean> list = new ArrayList<AppBean>();
private String path = Environment.getExternalStorageDirectory()+ File.separator;
private ProgressDialog progressdialog;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main);
initView();
initData();
}
private void initView() {
lv = (XListView) findViewById(R.id.lv);
}
private void initData() {
RequestParams params = new RequestParams(Uris.uri);
x.http().get(params, new Callback.CommonCallback<String>() {
@Override
public void onSuccess(String result) {
String s = result.substring(0,result.length()-1);
Gson gson = new Gson();
Bean bean = gson.fromJson(s, Bean.class);
list = bean.getApp();
lv.setAdapter(new MyAdapter(list, MainActivity.this));
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, final int position, long id) {
new AlertDialog.Builder(MainActivity.this)
.setTitle("网络选择")
.setIcon(R.mipmap.ic_launcher)
.setSingleChoiceItems(new String[]{"wifi", "手机流量"}, 0, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
switch (which){
case 0:
new AlertDialog.Builder(MainActivity.this)
.setTitle("版本更新")
.setMessage("现在检查到新版本,是否更新?")
.setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
})
.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
progressdialog = new ProgressDialog(MainActivity.this);
RequestParams params = new RequestParams(list.get(position).getUrl());
params.setSaveFilePath(path+list.get(position).getName()+".apk");
x.http().post(params, new ProgressCallback<File>() {
@Override
public void onSuccess(File result) {
Toast.makeText(MainActivity.this,"下载成功",Toast.LENGTH_SHORT).show();
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(result),"application/vnd.android.package-archive");
startActivity(intent);
}
@Override
public void onError(Throwable ex, boolean isOnCallback) {
Toast.makeText(MainActivity.this,"下载失败"+ex.getMessage(),Toast.LENGTH_SHORT).show();
Log.i("rjz",ex.getMessage());
if (ex instanceof HttpException) { //网络错误
HttpException httpEx = (HttpException) ex;
int responseCode = httpEx.getCode();
String responseMsg = httpEx.getMessage();
String errorResult = httpEx.getResult();
//...
} else { //其他错误
//...
}
}
@Override
public void onCancelled(CancelledException cex) {
}
@Override
public void onFinished() {
}
@Override
public void onWaiting() {
}
@Override
public void onStarted() {
}
@Override
public void onLoading(long total, long current, boolean isDownloading) {
progressdialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
progressdialog.setMessage("下载中");
progressdialog.show();
progressdialog.setMax((int) total);
progressdialog.setProgress((int) current);
}
});
}
})
.show();
break;
case 1:
Intent intent = new Intent(Settings.ACTION_WIFI_SETTINGS);
startActivity(intent);
break;
}
}
}).show();
}
});
}
@Override
public void onError(Throwable ex, boolean isOnCallback) {
}
@Override
public void onCancelled(CancelledException cex) {
}
@Override
public void onFinished() {
}
});
}
}