公司目前有个项目类似 google play 基本功能都做好了 能取道数据也都解析出来了 但就是下载的功能和游戏名字不对应,我通俗讲吧 比如我点击的是愤怒小鸟的下载 但确下载的别的游戏,看代码 我给你们解释一下
- Java code
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");[color=#FF0000]// for(int j = 0; j < jna2.length(); j++){// int index= indexs+1; JSONObject jb2 = (JSONObject) jna2.opt(0);//这个方法opt有问题 gamesCategory.setDownLoadFileUrl(jb2.getString("filename"));//下载的参数 // }[/color] Log.d(TAG, "gamesCategory.toString()" + gamesCategory.toString()); gamesCategoryList.add(gamesCategory); }
这里就是我点击的时候的事件 意思就是得到下载地址 看代码
- Java code
//下面是相应的游戏下载地址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(); }
downLoadFile方法这里:其实不用多解释 基本没什么用处
- Java code
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;}