title: Sending Complex JSON Object
body: <p>I want to communicate with a web server and exchange JSON information.</p>
  
  <p>my webservice URL looking like following format: <code>http://46.157.263.140/EngineTestingWCF/DPMobileBookingService.svc/SearchOnlyCus</code></p>
  
  <p>Here is my JSON Request format.</p>
  
  <pre><code>{
      "f": {
          "Adults": 1,
          "CabinClass": 0,
          "ChildAge": [
              7
          ],
          "Children": 1,
          "CustomerId": 0,
          "CustomerType": 0,
          "CustomerUserId": 81,
          "DepartureDate": "/Date(1358965800000+0530)/",
          "DepartureDateGap": 0,
          "Infants": 1,
          "IsPackageUpsell": false,
          "JourneyType": 2,
          "PreferredCurrency": "INR",
          "ReturnDate": "/Date(1359138600000+0530)/",
          "ReturnDateGap": 0,
          "SearchOption": 1
      },
      "fsc": "0"
  }
  </code></pre>
  
  <p>I tried with the following code to send a request:</p>
  
  <pre><code>public class Fdetails {
      private String Adults = "1";
      private String CabinClass = "0";
      private String[] ChildAge = { "7" };
      private String Children = "1";
      private String CustomerId = "0";
      private String CustomerType = "0";
      private String CustomerUserId = "0";
      private Date DepartureDate = new Date();
      private String DepartureDateGap = "0";
      private String Infants = "1";
      private String IsPackageUpsell = "false";
      private String JourneyType = "1";
      private String PreferredCurrency = "MYR";
      private String ReturnDate = "";
      private String ReturnDateGap = "0";
      private String SearchOption = "1";
  }
  
  public class Fpack {
      private Fdetails f = new Fdetails();
      private String fsc = "0";
  }
  </code></pre>
  
  <p>Then using Gson I create the <strong>JSON object</strong> like:</p>
  
  <pre><code>public static String getJSONString(String url) {
  String jsonResponse = null;
  String jsonReq = null;
  Fpack fReq = new Fpack();
  
  try {                                                
      Gson gson = new Gson();
      jsonReq = gson.toJson(fReq);                        
      JSONObject json = new JSONObject(jsonReq);
      JSONObject jsonObjRecv = HttpClient.SendHttpPost(url, json);
      jsonResponse = jsonObjRecv.toString();
  } 
  catch (JSONException e) {
                             e.printStackTrace();
                  }               
  return jsonResponse;
      }
  </code></pre>
  
  <p>and my <code>HttpClient.SendHttpPost</code> method is</p>
  
  <pre><code>public static JSONObject SendHttpPost(String URL, JSONObject json) {
  
          try {
              DefaultHttpClient httpclient = new DefaultHttpClient();
              HttpPost httpPostRequest = new HttpPost(URL);
  
              StringEntity se;
              se = new StringEntity(json.toString());
              httpPostRequest.setEntity(se);
              se.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE, "application/json"));          
              HttpResponse response = (HttpResponse) httpclient.execute(httpPostRequest);
              HttpEntity entity = response.getEntity();
              if (entity != null) {
                  // Read the content stream
                  InputStream instream = entity.getContent();
                  Header contentEncoding = response.getFirstHeader("Content-Encoding");
                  if (contentEncoding != null &amp;&amp; contentEncoding.getValue().equalsIgnoreCase("gzip")) {
                      instream = new GZIPInputStream(instream);
                  }
  
                  // convert content stream to a String
                  String resultString= convertStreamToString(instream);
                  instream.close();
                  resultString = resultString.substring(1,resultString.length()-1); // remove wrapping "[" and "]"
  
                  // Transform the String into a JSONObject
                  JSONObject jsonObjRecv = new JSONObject(resultString);
              return jsonObjRecv;
              } 
      catch (Exception e)
          {
              e.printStackTrace();
          }
          return null;
      }
  </code></pre>
  
  <p>Now I get the following exception:</p>
  
  <pre><code>org.json.JSONException: Value !DOCTYPE of type java.lang.String cannot be converted to JSONObject
  at org.json.JSON.typeMismatch(JSON.java:111)
  at org.json.JSONObject.&lt;init&gt;(JSONObject.java:158)
  at org.json.JSONObject.&lt;init&gt;(JSONObject.java:171)
  </code></pre>
  
  <p>and the printout of JSON string right before I make the request is as follows:</p>
  
  <pre><code>{
      "f": {
          "PreferredCurrency": "MYR",
          "ReturnDate": "",            
          "ChildAge": [
              7
          ],
          "DepartureDate": "Mar 2, 2013 1:17:06 PM",
          "CustomerUserId": 0,
          "CustomerType": 0,
          "CustomerId": 0,
          "Children": 1,
          "DepartureDateGap": 0,
          "Infants": 1,
          "IsPackageUpsell": false,
          "JourneyType": 1,
          "CabinClass": 0,
          "Adults": 1,
          "ReturnDateGap": 0,
          "SearchOption": 1
  
      },
      "fsc": "0"
  }
  </code></pre>
  
  <p>How do I solve this exception? Thanks in advance!</p>