コード例 #1
0
  private String getStringData2(List<BasicNameValuePair> pairs) throws IOException {
    String content = null;
    HttpURLConnection conn = (HttpURLConnection) mUrl.openConnection();
    if (!Utility.isWifi(MyApplication.getInstance())) {
      mConnectionTimeout = CONNECTION_TIMEOUT_GPRS;
      mSocketTimeout = SOCKET_TIMEOUT_GPRS;
    }
    conn.setConnectTimeout(mConnectionTimeout);
    conn.setReadTimeout(mSocketTimeout);
    conn.setDoOutput(true);

    if (pairs != null) {
      conn.setRequestMethod("POST");
      for (int i = 0; i < pairs.size(); ++i) {
        content += pairs.get(i).getName() + "=" + pairs.get(i).getValue();
        if (i != pairs.size() - 1) // Not the last
        content += "&";
      }
    } else conn.setRequestMethod("GET");

    Iterator<Entry<String, String>> iter = mExtraHeaders.entrySet().iterator();
    while (iter.hasNext()) {
      Map.Entry<String, String> entry = iter.next();
      String key = entry.getKey();
      String value = entry.getValue();
      conn.addRequestProperty(key, value);
    }

    conn.connect();
    if (content != null) {
      DataOutputStream out = new DataOutputStream(conn.getOutputStream());
      out.writeBytes(content);
      out.flush();
      out.close();
    }

    mReponseHeaders = conn.getHeaderFields();
    String line;
    StringBuilder sb = new StringBuilder();
    BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
    while ((line = reader.readLine()) != null) {
      sb.append(line + "\n");
    }

    return sb.toString();
  }
コード例 #2
0
  public String getStringData(List<BasicNameValuePair> pairs, String charset) {
    if (mUrl == null) {
      return null;
    }
    if (Utility.isNetworkAvailable() == false) {
      return null;
    }

    if (!Utility.isWifi(MyApplication.getInstance())) {
      mConnectionTimeout = CONNECTION_TIMEOUT_GPRS;
      mSocketTimeout = SOCKET_TIMEOUT_GPRS;
    }

    HttpParams httpParameters = new BasicHttpParams();
    HttpConnectionParams.setConnectionTimeout(httpParameters, mConnectionTimeout);
    HttpConnectionParams.setSoTimeout(httpParameters, mSocketTimeout);

    HttpClient client = new DefaultHttpClient(httpParameters);
    HttpResponse response = null;
    HttpEntity entity = null;
    String recvData = null;

    try {
      // POST
      if (pairs != null) {
        HttpPost post = new HttpPost(mUrl.toString());
        post.setEntity(new UrlEncodedFormEntity(pairs, HTTP.UTF_8));
        Iterator<Entry<String, String>> iter = mExtraHeaders.entrySet().iterator();
        while (iter.hasNext()) {
          Map.Entry<String, String> entry = iter.next();
          String key = entry.getKey();
          String value = entry.getValue();
          post.setHeader(key, value);
        }
        response = client.execute(post);
      }
      // GET
      else {
        HttpGet get = new HttpGet(mUrl.toString());
        Iterator<Entry<String, String>> iter = mExtraHeaders.entrySet().iterator();
        while (iter.hasNext()) {
          Map.Entry<String, String> entry = iter.next();
          String key = entry.getKey();
          String value = entry.getValue();
          get.setHeader(key, value);
        }
        response = client.execute(get);
      }

      if (response == null) {
        Log.e("Error", "Response Null");
        client.getConnectionManager().shutdown();
        return null;
      }
      if (response.getStatusLine().getStatusCode() != 200) {
        Log.e("Error", "Status code = %d" + response.getStatusLine().getStatusCode());
        client.getConnectionManager().shutdown();
        return null;
      }

      mResponse = response;
      entity = response.getEntity();
      if (entity == null) {
        Log.e("Error", "Status code = %d" + response.getStatusLine().getStatusCode());
        client.getConnectionManager().shutdown();
        return null;
      }

      InputStream is = entity.getContent();
      if (entity.getContentEncoding() != null
          && entity.getContentEncoding().getValue().toLowerCase(Locale.ENGLISH).contains("gzip")) {
        is = new GZIPInputStream(is);
      }
      String line;
      StringBuilder sb = new StringBuilder();
      BufferedReader reader;
      if (charset != null && !charset.equals(""))
        reader = new BufferedReader(new InputStreamReader(is, charset));
      else reader = new BufferedReader(new InputStreamReader(is));
      while ((line = reader.readLine()) != null) {
        sb.append(line + "\n");
      }

      recvData = sb.toString();
      if (recvData == null) {
        Log.e("Error", "Receive Null");
        client.getConnectionManager().shutdown();
        return null;
      }
      client.getConnectionManager().shutdown();
    } catch (UnsupportedEncodingException e1) {
      Log.e("Exception", "Set name pair failed!");
      //			e1.printStackTrace();
      client.getConnectionManager().shutdown();
      return null;
    } catch (ClientProtocolException e) {
      Log.e("Exception", "Exec post protocol failed!");
      //			e.printStackTrace();
      client.getConnectionManager().shutdown();
      return null;
    } catch (IOException e) {
      Log.e("Exception", "Exec post IO failed!");
      //			e.printStackTrace();
      client.getConnectionManager().shutdown();
      return null;
    } catch (ParseException e) {
      Log.e("Error", "Entity parse failed");
      //			e.printStackTrace();
      client.getConnectionManager().shutdown();
      return null;
    }

    return recvData;

    //		String recvData;
    //		try
    //		{
    //            recvData = getStringData2(pairs);
    //        }
    //		catch (IOException e)
    //		{
    //            e.printStackTrace();
    //            return null;
    //        }
    //		return recvData;
  }