Esempio n. 1
0
  private String readResult(HttpURLConnection urlConnection) throws WeiboException {
    InputStream is = null;
    BufferedReader buffer = null;
    GlobalContext globalContext = GlobalContext.getInstance();
    String errorStr = globalContext.getString(R.string.timeout);
    globalContext = null;
    try {
      is = urlConnection.getInputStream();

      String content_encode = urlConnection.getContentEncoding();

      if (null != content_encode && !"".equals(content_encode) && content_encode.equals("gzip")) {
        is = new GZIPInputStream(is);
      }

      buffer = new BufferedReader(new InputStreamReader(is));
      StringBuilder strBuilder = new StringBuilder();
      String line;
      while ((line = buffer.readLine()) != null) {
        strBuilder.append(line);
      }
      AppLogger.d("result=" + strBuilder.toString());
      return strBuilder.toString();
    } catch (IOException e) {
      e.printStackTrace();
      throw new WeiboException(errorStr, e);
    } finally {
      Utility.closeSilently(is);
      Utility.closeSilently(buffer);
      urlConnection.disconnect();
    }
  }
Esempio n. 2
0
  public boolean doUploadFile(
      String urlStr,
      Map<String, String> param,
      String path,
      final FileUploaderHttpHelper.ProgressListener listener)
      throws WeiboException {
    String BOUNDARYSTR = getBoundry();
    String BOUNDARY = "--" + BOUNDARYSTR + "\r\n";
    HttpURLConnection urlConnection = null;
    BufferedOutputStream out = null;
    FileInputStream fis = null;
    GlobalContext globalContext = GlobalContext.getInstance();
    String errorStr = globalContext.getString(R.string.timeout);
    globalContext = null;
    InputStream is = null;
    try {
      URL url = null;

      url = new URL(urlStr);

      Proxy proxy = getProxy();
      if (proxy != null) urlConnection = (HttpURLConnection) url.openConnection(proxy);
      else urlConnection = (HttpURLConnection) url.openConnection();

      urlConnection.setConnectTimeout(UPLOAD_CONNECT_TIMEOUT);
      urlConnection.setReadTimeout(UPLOAD_READ_TIMEOUT);
      urlConnection.setDoInput(true);
      urlConnection.setDoOutput(true);
      urlConnection.setRequestMethod("POST");
      urlConnection.setUseCaches(false);
      urlConnection.setRequestProperty("Connection", "Keep-Alive");
      urlConnection.setRequestProperty("Charset", "UTF-8");
      urlConnection.setRequestProperty(
          "Content-type", "multipart/form-data;boundary=" + BOUNDARYSTR);
      urlConnection.connect();

      out = new BufferedOutputStream(urlConnection.getOutputStream());

      StringBuilder sb = new StringBuilder();

      Map<String, String> paramMap = new HashMap<String, String>();

      for (String key : param.keySet()) {
        if (param.get(key) != null) {
          paramMap.put(key, param.get(key));
        }
      }

      for (String str : paramMap.keySet()) {
        sb.append(BOUNDARY);
        sb.append("Content-Disposition:form-data;name=\"");
        sb.append(str);
        sb.append("\"\r\n\r\n");
        sb.append(param.get(str));
        sb.append("\r\n");
      }

      out.write(sb.toString().getBytes());

      File file = new File(path);
      out.write(BOUNDARY.getBytes());
      StringBuilder filenamesb = new StringBuilder();
      filenamesb.append(
          "Content-Disposition:form-data;Content-Type:application/octet-stream;name=\"pic");
      filenamesb.append("\";filename=\"");
      filenamesb.append(file.getName() + "\"\r\n\r\n");
      out.write(filenamesb.toString().getBytes());

      fis = new FileInputStream(file);

      int bytesRead;
      int bytesAvailable;
      int bufferSize;
      byte[] buffer;
      int maxBufferSize = 1 * 1024;

      bytesAvailable = fis.available();
      bufferSize = Math.min(bytesAvailable, maxBufferSize);
      buffer = new byte[bufferSize];
      bytesRead = fis.read(buffer, 0, bufferSize);
      long transferred = 0;
      final Thread thread = Thread.currentThread();
      while (bytesRead > 0) {

        if (thread.isInterrupted()) {
          file.delete();
          throw new InterruptedIOException();
        }
        out.write(buffer, 0, bufferSize);
        bytesAvailable = fis.available();
        bufferSize = Math.min(bytesAvailable, maxBufferSize);
        bytesRead = fis.read(buffer, 0, bufferSize);
        transferred += bytesRead;
        if (transferred % 50 == 0) out.flush();
        if (listener != null) listener.transferred(transferred);
      }

      out.write("\r\n\r\n".getBytes());
      fis.close();

      out.write(("--" + BOUNDARYSTR + "--\r\n").getBytes());
      out.flush();
      out.close();
      int status = urlConnection.getResponseCode();
      if (listener != null) {
        listener.completed();
      }
      if (status != HttpURLConnection.HTTP_OK) {
        String error = handleError(urlConnection);
        throw new WeiboException(error);
      }

    } catch (IOException e) {
      e.printStackTrace();
      throw new WeiboException(errorStr, e);
    } finally {
      Utility.closeSilently(fis);
      Utility.closeSilently(out);
      if (urlConnection != null) urlConnection.disconnect();
    }

    return true;
  }
Esempio n. 3
0
  public boolean doGetSaveFile(
      String urlStr, String path, FileDownloaderHttpHelper.DownloadListener downloadListener) {

    File file = FileManager.createNewFileInSDCard(path);
    if (file == null) {
      return false;
    }

    FileOutputStream out = null;
    InputStream in = null;
    HttpURLConnection urlConnection = null;
    try {

      URL url = new URL(urlStr);
      AppLogger.d("download request=" + urlStr);
      Proxy proxy = getProxy();
      if (proxy != null) urlConnection = (HttpURLConnection) url.openConnection(proxy);
      else urlConnection = (HttpURLConnection) url.openConnection();

      urlConnection.setRequestMethod("GET");
      urlConnection.setDoOutput(false);
      urlConnection.setConnectTimeout(DOWNLOAD_CONNECT_TIMEOUT);
      urlConnection.setReadTimeout(DOWNLOAD_READ_TIMEOUT);
      urlConnection.setRequestProperty("Connection", "Keep-Alive");
      urlConnection.setRequestProperty("Charset", "UTF-8");
      urlConnection.setRequestProperty("Accept-Encoding", "gzip, deflate");

      urlConnection.connect();

      int status = urlConnection.getResponseCode();

      if (status != HttpURLConnection.HTTP_OK) {
        return false;
      }

      int bytetotal = (int) urlConnection.getContentLength();
      int bytesum = 0;
      int byteread = 0;
      out = new FileOutputStream(file);
      in = urlConnection.getInputStream();

      final Thread thread = Thread.currentThread();
      byte[] buffer = new byte[1444];
      while ((byteread = in.read(buffer)) != -1) {
        if (thread.isInterrupted()) {
          file.delete();
          throw new InterruptedIOException();
        }

        bytesum += byteread;
        out.write(buffer, 0, byteread);
        if (downloadListener != null && bytetotal > 0) {
          downloadListener.pushProgress(bytesum, bytetotal);
        }
      }
      return true;

    } catch (IOException e) {
      e.printStackTrace();
    } finally {
      Utility.closeSilently(in);
      Utility.closeSilently(out);
      if (urlConnection != null) urlConnection.disconnect();
    }

    return false;
  }