コード例 #1
0
  public void getDataSource2(String strPath) {
    File f = new File(myActivity.getFilesDir().getPath() + "/" + "alipay.apk");
    if (f.exists()) {
      f.delete();
    }
    DefaultHttpClient m_httpclient;
    HttpGet m_httppost;
    m_httpclient = new DefaultHttpClient();
    m_httppost = new HttpGet(strPath);
    HttpParams httpParams = m_httpclient.getParams();
    // 设置网络超时参数
    HttpConnectionParams.setConnectionTimeout(httpParams, 20 * 1000);
    HttpConnectionParams.setSoTimeout(httpParams, 45 * 1000);
    try {
      HttpResponse response = m_httpclient.execute(m_httppost);
      if (response.getStatusLine().getStatusCode() != 200) {
        errorFlag = true;
      } else {
        HttpEntity entity = response.getEntity();
        fileLength = entity.getContentLength();
        log("download file size " + fileLength);

        setProcessStep((int) fileLength);

        InputStream content = entity.getContent();

        saveStream(content);
      }

    } catch (ClientProtocolException e) {
      // network Exception to be log
      SystemExceptionHandler.getInstance().saveConnInfoToFile(e, Constants.MONITORPOINT_CONNECTERR);
      e.printStackTrace();
      errorFlag = true;
      errorStringId = R.string.FailDownloadFile;

    } catch (IOException e) {
      // network Exception to be log
      SystemExceptionHandler.getInstance().saveConnInfoToFile(e, Constants.MONITORPOINT_CONNECTERR);
      e.printStackTrace();
      errorFlag = true;
    } catch (Exception e) {
      // network Exception to be log
      SystemExceptionHandler.getInstance().saveConnInfoToFile(e, Constants.MONITORPOINT_CONNECTERR);
      e.printStackTrace();
      errorFlag = true;
    }
  }
コード例 #2
0
  private void saveStream(InputStream is) {
    FileOutputStream fos;
    try {
      // fos = new FileOutputStream(myTempFile);
      fos = myActivity.openFileOutput("alipay.apk", Context.MODE_WORLD_READABLE);
      fileLocation = myActivity.getFilesDir().getPath() + "/" + "alipay.apk";
      // SaveUrl(myTempFile.getPath());

      SaveUrl(fileLocation);

      byte buf[] = new byte[5120];
      int total = 0;
      do {
        int numread = is.read(buf);
        if (numread <= 0) {
          break;
        }

        fos.write(buf, 0, numread);
        total += numread;
        setProcess(total);

      } while (!mCanceled);

      is.close();
    } catch (FileNotFoundException e) {
      // network Exception to be log
      SystemExceptionHandler.getInstance().saveConnInfoToFile(e, Constants.MONITORPOINT_CONNECTERR);
      e.printStackTrace();
      errorFlag = true;

    } catch (Exception ex) {
      // network Exception to be log
      SystemExceptionHandler.getInstance()
          .saveConnInfoToFile(ex, Constants.MONITORPOINT_CONNECTERR);
      errorFlag = true;
    } finally {
      try {
        is.close();
      } catch (IOException e) {
        // network Exception to be log
        SystemExceptionHandler.getInstance()
            .saveConnInfoToFile(e, Constants.MONITORPOINT_CONNECTERR);
      }
    }
  }