title: connection to a web service on https:// in android
body: <p>working on an event manager application , connecting to an http:// web service to retrieve JSON response storing events in local sqlite DB and so forth everything was going alright , until we moved the web service to https://, i started receiving ssl certificate error every time i tried to connect to the service .
  using DefaultHttpClient ,<br>
  i looked up solutions on stack overflow and i found some going as far as to write custom classes to solve the issue .
  i can't believe that there's no straight forward solution to connect to a web service on https:// from android without having to write custom </p>
  
  <p>i'd appreciate if someone could help me with this issue </p>
  
  <p>here's the code </p>
  
  <pre><code>package com.xxxxx.xxxxxxxxx.utility;
  
  import java.io.BufferedReader;
  import java.io.IOException;
  import java.io.InputStream;
  import java.io.InputStreamReader;
  import java.io.UnsupportedEncodingException;
  import java.util.List;  
  import org.apache.http.HttpEntity;
  import org.apache.http.HttpResponse;
  import org.apache.http.HttpStatus;
  import org.apache.http.HttpVersion;
  import org.apache.http.NameValuePair;
  import org.apache.http.StatusLine;
  import org.apache.http.client.ClientProtocolException;
  import org.apache.http.client.ResponseHandler;
  import org.apache.http.client.entity.UrlEncodedFormEntity;
  import org.apache.http.client.methods.HttpGet;
  import org.apache.http.client.methods.HttpPost;
  import org.apache.http.client.utils.URLEncodedUtils;
  import org.apache.http.conn.ClientConnectionManager;
  import org.apache.http.conn.scheme.Scheme;
  import org.apache.http.impl.client.BasicResponseHandler;
  import org.apache.http.impl.client.DefaultHttpClient;
  import org.apache.http.impl.conn.SingleClientConnManager;
  import org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager;
  import org.apache.http.params.BasicHttpParams;
  import org.apache.http.params.HttpConnectionParams;
  import org.apache.http.params.HttpParams;
  import org.apache.http.params.HttpProtocolParams;
  import org.apache.http.protocol.HTTP;
  import org.json.JSONException;
  import org.json.JSONObject;
  
  import android.util.Log;
  
  public class JSONParser {
      // input stream to buffer data
      static InputStream is = null;
      //json object 
  
      static JSONObject jObj = new JSONObject();
      // json string
      static String json = "";
      private String socketResult = null;
  
      // constructor
      public JSONParser() {
  
      }
  
      // function get json from url
  
      public JSONObject makeHttpRequest(String url, List&lt;NameValuePair&gt; params) {
  
          // Making HTTP request
          try {
  
              final HttpParams httpParameters = new BasicHttpParams();
              // Set the timeout in milliseconds until a connection is established.
                HttpConnectionParams.setConnectionTimeout(httpParameters, 240000);
  
              // Set the default socket timeout (SO_TIMEOUT)
              // in milliseconds which is the timeout for waiting for data.
             HttpConnectionParams.setSoTimeout(httpParameters, 180000);
              // request method is POST
              // defaultHttpClient
  
             DefaultHttpClient httpClient = new DefaultHttpClient(httpParameters);
              // HTTP POST method
              HttpPost httpPost = new HttpPost(url);
              // Hands the entity to the request. // thr paramters to pass to the request in the form of name value pairs  
              httpPost.setEntity(new UrlEncodedFormEntity(params));
              // handels the response form the http request 
              HttpResponse httpResponse = httpClient.execute(httpPost);
  
  StatusLine status = httpResponse.getStatusLine();
  if (status.getStatusCode() == HttpStatus.SC_OK) {
  
  
      // An entity that can be sent or received with an HTTP message. Entities can be found in some    requests and in responses, where they are optional.
      HttpEntity httpEntity = httpResponse.getEntity();
        // store the content / data of the entity in an input stream 
      is = httpEntity.getContent();
  }else{
      // Do something else, if wanted.
  }
  
             // An entity that can be sent or received with an HTTP message. Entities can be found in some requests and in responses, where they are optional.
           // HttpEntity httpEntity = httpResponse.getEntity();
             // store the content / data of the entity in an input stream 
           //is = httpEntity.getContent();
  
  
  
          } catch (UnsupportedEncodingException e) {
              Log.d("UnsupportedEncodingException", "HTTP Error", e);
  
          } catch (ClientProtocolException e) {
              Log.d("ClientProtocolException", "HTTP Error", e);
  
          } catch (IOException e) {
              Log.d("IOException", "Connection Error", e);
  
          }
  
          try {
  
              // read the data in the input stream entity to buffer 
              BufferedReader reader = new BufferedReader(new InputStreamReader(is, "iso-8859-1"), 8);
              StringBuilder sb = new StringBuilder();
              String line = null;
              // construct a string builder object the buffer data 
              while ((line = reader.readLine()) != null) {
                  sb.append(line + "\n");
              }
              // close the input stream 
              is.close();
  
  
              //  create string from the json data 
              json = sb.toString();
          } catch (Exception e) {
              Log.d("Buffer Error", "Error converting result " + e.toString());
          }
  
          // try parse the string to a JSON object
          try {
              //----------------------------------------------------------------------
              //Log.d(" creating json object " , json);
  
  
              jObj = new JSONObject(json);
          } catch (JSONException e) {
              Log.d("JSON Parser", "Error parsing data " + e.toString());
              //----------------------------------------------------------------------
               System.out.println("JSON Parserexception:" + e);
          }
  
          // return JSON String
          return jObj;
  
      }
  }
  </code></pre>