80,351
社区成员
发帖
与我相关
我的任务
分享
WebHelper类不用我解释了 就是向服务器来解析数据的
参数什么的 重点在下面 红色部分 FOR语句我注释了
在里面有个OPT方法 有问题不能对应我点击的游戏
// 获取返回json报文
if (hr.getStatusLine().getStatusCode() == 200) {
jsonforString = EntityUtils.toString(hr.getEntity());
Log.d(TAG, jsonforString);
JSONObject jsonObject = new JSONObject(jsonforString.toString());
JSONArray jna = jsonObject.getJSONArray("data");
for (int i = 0; i < jna.length(); i++) {
GameInfo gamesCategory = new GameInfo();
JSONObject jb = (JSONObject) jna.opt(i);
gamesCategory.setName(jb.getString("name"));
gamesCategory.setTypeName(jb.getString("typename"));
gamesCategory.setLogo_url(jb.getString("logo_url"));
gamesCategory.setGameid(jb.getString("gameid"));
gamesCategory.setSourceScore(jb.getString("score"));
gamesCategory.setSize(jb.getString("size"));
JSONArray jna2 = jb.getJSONArray("download");
// for(int j = 0; j < jna2.length(); j++){
// int index= indexs+1;
JSONObject jb2 = (JSONObject) jna2.opt(0);//这个方法opt有问题
gamesCategory.setDownLoadFileUrl(jb2.getString("filename"));//下载的参数
// }
Log.d(TAG, "gamesCategory.toString()" + gamesCategory.toString());
gamesCategoryList.add(gamesCategory);
}
//下面是相应的游戏下载地址url
fileUrl = info.getDownLoadFileUrl();//得到下载地址 这里我做了个封装
holder.btns .setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
// DownedFileLength = 0;
holder.ratingBarScore.setVisibility(View.GONE);
holder.pro.setVisibility(View.VISIBLE);
holder.textView.setVisibility(View.VISIBLE);
Thread thread = new Thread(){
public void run(){
Log.v("ccf", "onClick");
try {
downLoadFile(fileUrl);//我把地址传进来了
openFile(new File("/sdcard/update/updata.apk"));
Toast.makeText(context, "开始下载"+fileUrl, 0).show();
// holder.btns.setBackgroundResource(R.drawable.way);
}catch (Exception e){
e.printStackTrace();
}
protected File downLoadFile(String httpUrl) {
// TODO Auto-generated method stub
final String fileName = "updata.apk";
File tmpFile = new File("//sdcard//update");
if (!tmpFile.exists()) {
tmpFile.mkdir();
}
final File file = new File("//sdcard//update//" + fileName);
try {
URL url = new URL(httpUrl);
try {
HttpURLConnection conn = (HttpURLConnection) url
.openConnection();
InputStream is = conn.getInputStream();
// FileLength = conn.getContentLength();//根据响应得到大小
Log.v("wjp","FileLength"+FileLength );
// if(FileLength <=0)throw new RuntimeException("无法获取文件的大小");//获取文件大小
// Log.v("wjp","FileLengsss"+FileLength );
// if(is == null)throw new RuntimeException("stream is null");//null
FileOutputStream fos = new FileOutputStream(file);
byte[] buf = new byte[1024];
//
conn.connect();
double count = 0;
if (conn.getResponseCode() >= 400) {
Toast.makeText(context, "出错", Toast.LENGTH_SHORT)
.show();
} else {
while (count <= 100) {
if (is != null) {
int numRead = is.read(buf);
if (numRead <=0) {
break;
} else {
fos.write(buf, 0, numRead);
// count +=numRead;
// sendMsg(1);//消息主要是更新进度条下载同步进行
}
} else {
break;
}
}
}
conn.disconnect();
fos.close();
is.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return file;
}