Example #1
0
 private void sendGetRequest(String urlStr, HashMap<String, String> params) {
   HttpURLConnection urlConnection = null;
   JSONObject json = null;
   URL url;
   try {
     urlStr += "?";
     for (String key : params.keySet()) {
       urlStr += key + "=";
       urlStr += params.get(key);
       urlStr += "&";
     }
     urlStr = urlStr.substring(0, urlStr.length() - 1);
     url = new URL(urlStr);
     InputStream response;
     urlConnection = (HttpURLConnection) url.openConnection();
     response = urlConnection.getInputStream();
     json = convertStreamToJSON(response);
   } catch (Exception e) {
     e.printStackTrace();
   }
   up.handleResponse(responseCode, json);
 }
Example #2
0
  private void sendPostRequest(String urlStr, String urlParams, boolean isJSON) {
    HttpURLConnection urlConnection = null;
    JSONObject json = null;
    URL url;
    try {
      url = new URL(urlStr);

      urlConnection = (HttpURLConnection) url.openConnection();
      urlConnection.setRequestMethod("POST");

      Log.d("kimreik", "isJson " + isJSON);
      if (isJSON) {
        urlConnection.setRequestProperty("Content-Type", "application/json"); // ;
        // charset=utf-8
        urlConnection.setRequestProperty("Accept", "application/json");
      } else {
        // urlConnection.setRequestProperty("Content-Type",
        // "charset=utf-8");
      }
      urlConnection.setDoOutput(true);
      DataOutputStream wr = new DataOutputStream(urlConnection.getOutputStream());
      if (isJSON) {
        wr.write(urlParams.getBytes("UTF-8"));
      } else {
        wr.writeBytes(urlParams);
      }
      wr.flush();
      wr.close();

      InputStream response;
      response = urlConnection.getInputStream();
      json = convertStreamToJSON(response);
    } catch (Exception e) {
      e.printStackTrace();
    }
    up.handleResponse(responseCode, json);
  }