Esempio n. 1
0
  public static String doRequestPost(String url, Bundle params) {
    ArrayList<BasicNameValuePair> paramsList = new ArrayList<BasicNameValuePair>();
    BasicNameValuePair paramPair;
    if (params != null) {
      for (String key : params.keySet()) {
        paramPair = new BasicNameValuePair(key, params.getString(key));
        paramsList.add(paramPair);
      }
    }

    HttpParams httpParameters = new BasicHttpParams();
    HttpConnectionParams.setConnectionTimeout(httpParameters, CONNECTION_TIME_OUT);
    HttpConnectionParams.setSoTimeout(httpParameters, SOCKET_TIME_OUT);

    try {
      HttpPost httpRequest = new HttpPost(url);
      httpRequest.setEntity(new UrlEncodedFormEntity(paramsList, HTTP.UTF_8));
      HttpResponse httpResponse = new DefaultHttpClient(httpParameters).execute(httpRequest);

      if (httpResponse.getStatusLine().getStatusCode() == SUCCESS_STATUS) {
        String strResult = EntityUtils.toString(httpResponse.getEntity());
        return strResult;
      } else {
        LogUtils.logD("Http response error when do post operation!");
        return null;
      }
    } catch (Exception e) {
      LogUtils.logE(e.getMessage());
      return null;
    }
  }
Esempio n. 2
0
  public static String doRequestGet(String url, Bundle params) {
    String newUrl = url;
    if (params != null && !params.isEmpty()) {
      StringBuilder sb = new StringBuilder();
      sb.append(url).append('?');

      boolean first = true;
      for (String key : params.keySet()) {
        if (!first) {
          sb.append('&');
        } else {
          first = false;
        }
        sb.append(URLEncoder.encode(key))
            .append('=')
            .append(URLEncoder.encode(params.getString(key)));
      }
      newUrl = sb.toString();
    }

    HttpParams httpParameters = new BasicHttpParams();
    HttpConnectionParams.setConnectionTimeout(httpParameters, CONNECTION_TIME_OUT);
    HttpConnectionParams.setSoTimeout(httpParameters, SOCKET_TIME_OUT);

    try {
      HttpGet httpRequest = new HttpGet(newUrl);
      HttpResponse httpResponse = new DefaultHttpClient(httpParameters).execute(httpRequest);
      if (httpResponse.getStatusLine().getStatusCode() == SUCCESS_STATUS) {
        return EntityUtils.toString(httpResponse.getEntity());
      } else {
        LogUtils.logD("Http response error when do get operation!");
      }
    } catch (Exception e) {
      LogUtils.logE(e.getMessage());
    }
    return null;
  }
Esempio n. 3
0
  public static boolean getFileFromServer(String path, Bundle params, String filePath) {
    byte[] bf = new byte[FILE_BUFFER_SIZE];
    int current = 0;
    InputStream is = null;
    HttpURLConnection connect = null;
    BufferedInputStream bis = null;
    OutputStream fos = null;

    try {
      fos = new FileOutputStream(filePath);

      String completePath = path;
      if (params != null && !params.isEmpty()) {
        StringBuilder sb = new StringBuilder();
        sb.append(path).append('?');

        boolean first = true;
        for (String key : params.keySet()) {
          if (!first) {
            sb.append('&');
          } else {
            first = false;
          }
          sb.append(URLEncoder.encode(key))
              .append('=')
              .append(URLEncoder.encode(params.getString(key)));
        }
        completePath = sb.toString();
      }

      URL url = new URL(completePath);
      connect = (HttpURLConnection) url.openConnection();
      connect.setDoInput(true);
      connect.setConnectTimeout(CONNECTION_TIME_OUT);
      connect.setReadTimeout(SOCKET_TIME_OUT);
      int code = connect.getResponseCode();

      if (code == HttpURLConnection.HTTP_OK) {
        is = connect.getInputStream();
        bis = new BufferedInputStream(is);
        while ((current = bis.read(bf)) != FILE_END) {
          fos.write(bf, 0, current);
        }
      }
      return true;
    } catch (MalformedURLException e) {
      LogUtils.logE(e.getMessage());
      return false;
    } catch (IOException e) {
      LogUtils.logE(e.getMessage());
      return false;
    } catch (Exception e) {
      LogUtils.logE(e.getMessage());
      return false;
    } finally {
      try {
        if (bis != null) {
          bis.close();
        }
        if (is != null) {
          is.close();
        }
        if (fos != null) {
          fos.close();
        }
        if (connect != null) {
          connect.disconnect();
        }
      } catch (IOException e) {
        LogUtils.logE(e.getMessage());
      }
    }
  }