Пример #1
0
 private String doPostForString(String url, List<BasicNameValuePair> params) {
   InputStream is = null;
   InputStreamReader in = null;
   try {
     DefaultHttpClient httpClient = new DefaultHttpClient(); // 使用DefaultHttpClient创建HttpClient实例
     HttpPost post = new HttpPost(url); // 构建HttpPost
     UrlEncodedFormEntity entity = new UrlEncodedFormEntity(params, HTTP.UTF_8); // 使用编码构建Post实体
     post.setEntity(entity); // 执行Post方法
     if (paramsProperty != null) {
       for (BasicNameValuePair basic : paramsProperty) {
         post.setHeader(basic.getName(), basic.getValue());
       }
     }
     HttpResponse response = httpClient.execute(post); // 获取返回实体
     is = response.getEntity().getContent();
     in = new InputStreamReader(is);
     ByteArrayOutputStream bos = new ByteArrayOutputStream();
     byte[] buffer = new byte[1024];
     int lenth = 0;
     while ((lenth = is.read(buffer)) != -1) {
       bos.write(buffer, 0, lenth);
       bos.flush();
     }
     bos.close();
     String json = bos.toString();
     if (isLog) {
       LogFileHelper.getInstance().i(TAG, post.getURI().toString());
       LogFileHelper.getInstance().i(TAG, json);
     }
     return json;
   } catch (Exception e) {
     // TODO Auto-generated catch block
     LogFileHelper.getInstance().e(getClass().getSimpleName(), e.getMessage());
   }
   // 使用finally块来关闭输出流、输入流
   finally {
     try {
       if (is != null) {
         is.close();
       }
       if (in != null) {
         in.close();
       }
     } catch (IOException e) {
       LogFileHelper.getInstance().e(getClass().getSimpleName(), e.getMessage());
     }
   }
   return null;
 }
Пример #2
0
  /**
   * Title: doGetForJson Description: 发送请求的 URL,直接返回JSON数据解析。返回集合列表 return T throws
   *
   * @param isString
   */
  private String doGetForString(String url, List<BasicNameValuePair> params) {
    InputStream is = null;
    InputStreamReader in = null;
    StringBuffer geturl = new StringBuffer();
    geturl.append(url);

    if (params != null) {
      geturl.append("?");
      for (BasicNameValuePair item : params) {
        geturl.append(item.getName() + "=" + item.getValue() + "&");
      }
      geturl.deleteCharAt(geturl.length() - 1);
    }
    try {
      URL realUrl = new URL(new String(geturl.toString().getBytes(), "ISO-8859-1"));
      // 打开和URL之间的连接
      HttpURLConnection conn = (HttpURLConnection) realUrl.openConnection();
      // 设置通用的请求属性

      conn.setReadTimeout(TIMEOUT_IN_MILLIONS);
      conn.setConnectTimeout(TIMEOUT_IN_MILLIONS);
      conn.setRequestMethod("GET");
      conn.setRequestProperty("accept", "*/*");
      conn.setRequestProperty("connection", "Keep-Alive");
      if (paramsProperty != null) {
        for (BasicNameValuePair basic : paramsProperty) {
          conn.setRequestProperty(basic.getName(), basic.getValue());
        }
      }

      // 定义BufferedReader输入流来读取URL的响应
      is = conn.getInputStream();
      in = new InputStreamReader(is);
      ByteArrayOutputStream bos = new ByteArrayOutputStream();
      byte[] buffer = new byte[1024];
      int lenth = 0;
      while ((lenth = is.read(buffer)) != -1) {
        bos.write(buffer, 0, lenth);
        bos.flush();
      }
      bos.close();
      String json = bos.toString();
      if (isLog) {
        LogFileHelper.getInstance().i(TAG, realUrl.toString());
        LogFileHelper.getInstance().i(TAG, json);
      }
      return json;
    } catch (Exception e) {

      LogFileHelper.getInstance().e(getClass().getSimpleName(), e.getMessage());
    }

    // 使用finally块来关闭输出流、输入流
    finally {
      try {
        if (in != null) {
          in.close();
        }
        if (is != null) {
          is.close();
        }
      } catch (IOException e) {
        LogFileHelper.getInstance().e(getClass().getSimpleName(), e.getMessage());
      }
    }
    return null;
  }