Example #1
0
  private String doGet(String url, Map<String, String> param) throws WeiboException {

    HttpGet httpGet = new HttpGet();
    URIBuilder uriBuilder;
    try {
      uriBuilder = new URIBuilder(url);

      Set<String> keys = param.keySet();

      for (String key : keys) {
        String value = param.get(key);
        if (!TextUtils.isEmpty(value)) uriBuilder.addParameter(key, param.get(key));
      }

      httpGet.setURI(uriBuilder.build());

      AppLogger.d(uriBuilder.build().toString());

    } catch (URISyntaxException e) {
      AppLogger.d(e.getMessage());
    }

    CookieStore cookieStore = new BasicCookieStore();

    HttpContext localContext = new BasicHttpContext();
    localContext.setAttribute(ClientContext.COOKIE_STORE, cookieStore);

    HttpResponse response = getHttpResponse(httpGet, localContext);

    if (response != null) {
      return dealWithResponse(response);
    } else {
      return "";
    }
  }
Example #2
0
  public static File createNewFileInSDCard(String absolutePath) {
    if (!isExternalStorageMounted()) {
      AppLogger.e("sdcard unavailiable");
      return null;
    }

    File file = new File(absolutePath);
    if (file.exists()) {
      return file;
    } else {
      File dir = file.getParentFile();
      if (!dir.exists()) {
        dir.mkdirs();
      }

      try {
        if (file.createNewFile()) {
          return file;
        }
      } catch (IOException e) {
        AppLogger.d(e.getMessage());
        return null;
      }
    }
    return null;
  }
Example #3
0
  public String doGet(String urlStr, Map<String, String> param) throws WeiboException {
    GlobalContext globalContext = GlobalContext.getInstance();
    String errorStr = globalContext.getString(R.string.timeout);
    globalContext = null;
    InputStream is = null;
    try {

      StringBuilder urlBuilder = new StringBuilder(urlStr);
      urlBuilder.append("?").append(Utility.encodeUrl(param));
      URL url = new URL(urlBuilder.toString());
      AppLogger.d("get request" + url);
      Proxy proxy = getProxy();
      HttpURLConnection urlConnection;
      if (proxy != null) urlConnection = (HttpURLConnection) url.openConnection(proxy);
      else urlConnection = (HttpURLConnection) url.openConnection();

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

      urlConnection.connect();

      return handleResponse(urlConnection);
    } catch (IOException e) {
      e.printStackTrace();
      throw new WeiboException(errorStr, e);
    }
  }
Example #4
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();
    }
  }
Example #5
0
  private String doPost(String url, Map<String, String> param) throws WeiboException {
    AppLogger.d(url);
    HttpPost httpPost = new HttpPost();

    List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();

    Set<String> keys = param.keySet();
    for (String key : keys) {
      String value = param.get(key);
      if (!TextUtils.isEmpty(value)) nameValuePairs.add(new BasicNameValuePair(key, value));
    }

    UrlEncodedFormEntity entity = null;
    try {
      entity = new UrlEncodedFormEntity(nameValuePairs, "UTF-8");
    } catch (UnsupportedEncodingException ignored) {

    }
    try {
      httpPost.setURI(new URI(url));
    } catch (URISyntaxException e) {
      AppLogger.e(e.getMessage());
    }
    httpPost.setEntity(entity);

    HttpResponse response = getHttpResponse(httpPost, null);

    if (response != null) {

      return dealWithResponse(response);
    } else {
      return "";
    }
  }
Example #6
0
  /** don't need error message to show */
  private String doGetSaveFile(
      String url, String path, FileDownloaderHttpHelper.DownloadListener downloadListener) {

    URIBuilder uriBuilder;
    HttpGet httpGet = new HttpGet();
    try {
      uriBuilder = new URIBuilder(url);

      httpGet.setURI(uriBuilder.build());

      AppLogger.d(uriBuilder.build().toString());

    } catch (URISyntaxException e) {
      AppLogger.d(e.getMessage());
    }

    HttpResponse response = null;
    try {

      response = httpClient.execute(httpGet);

    } catch (ConnectTimeoutException ignored) {

      AppLogger.e(ignored.getMessage());

    } catch (ClientProtocolException ignored) {
      AppLogger.e(ignored.getMessage());

    } catch (IOException ignored) {
      AppLogger.e(ignored.getMessage());
    }

    if (response != null) {

      return FileDownloaderHttpHelper.saveFile(response, path, downloadListener);

    } else {
      return "";
    }
  }
Example #7
0
  private String readResult(HttpResponse response) {
    HttpEntity entity = response.getEntity();
    String result = "";

    try {
      result = EntityUtils.toString(entity);

    } catch (IOException e) {

      AppLogger.e(e.getMessage());
      ActivityUtils.showTips(R.string.timeout);
    }

    AppLogger.d(result);

    return result;
  }
Example #8
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;
  }