示例#1
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;
  }
示例#2
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 "";
    }
  }
  public String getInfo() {
    Map<String, String> map = new HashMap<String, String>();
    map.put("language", "zh-CN");
    map.put("sensor", "false");
    map.put("latlng", getLatlng());

    String url = URLHelper.GOOGLELOCATION;

    String jsonData = null;
    try {
      jsonData = HttpUtility.getInstance().executeNormalTask(HttpMethod.Get, url, map);
    } catch (WeiboException e) {
      AppLogger.e(e.getMessage());
    }

    try {
      JSONObject jsonObject = new JSONObject(jsonData);
      JSONArray results = jsonObject.optJSONArray("results");
      JSONObject jsonObject1 = results.getJSONObject(0);
      String formatAddress = jsonObject1.optString("formatted_address");
      int index = formatAddress.indexOf(" ");
      if (index > 0) {
        String location = formatAddress.substring(0, index);
        return location;
      } else {
        return formatAddress;
      }
    } catch (JSONException e) {
      AppLogger.e(e.getMessage());
    }

    return "";
  }
示例#4
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 "";
    }
  }
示例#5
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);
    }
  }
示例#6
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();
    }
  }
示例#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;
  }
示例#8
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 "";
    }
  }
示例#9
0
  private HttpResponse getHttpResponse(HttpRequestBase httpRequest, HttpContext localContext)
      throws WeiboException {
    HttpResponse response = null;
    try {
      if (localContext != null) {
        response = httpClient.execute(httpRequest, localContext);
      } else {
        response = httpClient.execute(httpRequest);
      }
    } catch (ConnectTimeoutException e) {

      AppLogger.e(e.getMessage());
      throw new WeiboException(GlobalContext.getInstance().getString(R.string.timeout), e);

    } catch (ClientProtocolException e) {
      AppLogger.e(e.getMessage());
      throw new WeiboException(GlobalContext.getInstance().getString(R.string.timeout), e);

    } catch (IOException e) {
      AppLogger.e(e.getMessage());
      throw new WeiboException(GlobalContext.getInstance().getString(R.string.timeout), e);
    }
    return response;
  }
    @Override
    protected UserBean doInBackground(Void... params) {

      FriendshipsDao dao = new FriendshipsDao(GlobalContext.getInstance().getSpecialToken());
      if (!TextUtils.isEmpty(bean.getId())) {
        dao.setUid(bean.getId());
      } else {
        dao.setScreen_name(bean.getScreen_name());
      }
      try {
        return dao.followIt();
      } catch (WeiboException e) {
        AppLogger.e(e.getError());
        this.e = e;
        cancel(true);
        return null;
      }
    }
示例#11
0
  private String handleError(HttpURLConnection urlConnection) throws WeiboException {

    String result = readError(urlConnection);
    String err = null;
    int errCode = 0;
    try {
      AppLogger.e("error=" + result);
      JSONObject json = new JSONObject(result);
      err = json.optString("error_description", "");
      if (TextUtils.isEmpty(err)) err = json.getString("error");
      errCode = json.getInt("error_code");
      WeiboException exception = new WeiboException();
      exception.setError_code(errCode);
      exception.setOriError(err);
      throw exception;

    } catch (JSONException e) {
      e.printStackTrace();
    }

    return result;
  }
示例#12
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;
  }