title: HttpURLConnect - Http PUT with JSON using GSON streaming not sending content
body: <p>I am new with Android development. I am trying to build an Android http client that makes requests to a .NET Web API sending JSON.</p>
  
  <p>I am using:</p>
  
  <ul>
  <li>GSON 2.3 to serialize-deserialize JSON as it supports streaming because JSONObject does not handle well large JSON (it uses too much memory as it tries to load the whole JSON in a String in memory). I tried to follow <a href="https://sites.google.com/site/gson/streaming" rel="nofollow noreferrer">https://sites.google.com/site/gson/streaming</a></li>
  <li>Android API 17 within Android Studio</li>
  <li>HttpURLConnection as the client Http recommended by <a href="http://developer.android.com/reference/java/net/HttpURLConnection.html" rel="nofollow noreferrer">http://developer.android.com/reference/java/net/HttpURLConnection.html</a></li>
  </ul>
  
  <p>I am trying to request an http PUT with JSON and the Web API in server-side gets the http request but it's unable to find any JSON, so I must be missing something that I cannot see.. I have even considered what it says here: <a href="https://stackoverflow.com/questions/21974407/how-to-stream-a-json-object-to-a-httpurlconnection-post-request">How to stream a JSON object to a HttpURLConnection POST request</a> about using the connect() method despite the <em>HttpURLConnection documentation does not mention it</em>.</p>
  
  <p>My code:</p>
  
  <pre><code>public void sendData(final String id, final int count, final MobileData myData)
          throws WebApiException {
      HttpURLConnection urlConnection = null;
      final String urlSend = "http://myIISapi" + "/" + id + "?count=" + count;
      try {
          final URL url = new URL(urlSend);
          urlConnection = (HttpURLConnection) url.openConnection();
          urlConnection.setDoOutput(true);
          urlConnection.setChunkedStreamingMode(0); //gets system default chunk size
  
          urlConnection.setRequestMethod("PUT");
          urlConnection.setRequestProperty("Content-Type", "application/json");
          urlConnection.setRequestProperty("Accept", "application/json");
          urlConnection.connect(); //necessary?
  
  
          final OutputStream outputStream = new BufferedOutputStream(urlConnection.getOutputStream());
          final JsonWriter writer = new JsonWriter(new OutputStreamWriter(outputStream, "UTF-8"));
          writer.setIndent("  ");
          Gson gson = new Gson();
          //String parsed = gson.toJson(amsData, MobileAmsData.class);
          gson.toJson(myData, MobileData.class, writer);
          writer.flush();
          writer.close();
  
          final int statusCode = urlConnection.getResponseCode();
          LOG.info("sendData server responded with status code = " + statusCode);
          if (HttpURLConnection.HTTP_OK != statusCode) {
              throw new WebApiException("Server did not accept data. Status code: " + statusCode);
          }
      }
      catch (Exception e) {
          throw new WebApiException("Unable to send PUT request", e);
      } finally {
          if (urlConnection != null) {
              urlConnection.disconnect();
          }
      }
  }
  </code></pre>
  
  <p>Am I missing some mandatory header? I am a little bit confused about the streaming way of sending JSON so the problem might be there. Finally, is it possible that by streaming data like that there are actually more than one HTTP request and the server should consider that the data is being streamed? Or the streaming happens only at client-side in the way the single http request is built?</p>
  
  <p>Thank you guys!</p>
  
  <p><strong>EDITED:</strong> If I explicitly say to the JsonWriter to </p>
  
  <blockquote>
    <p>writer.beginObject();</p>
  </blockquote>
  
  <p>before the </p>
  
  <blockquote>
    <p>gson.toJson(myData, MobileData.class, writer);</p>
  </blockquote>
  
  <p>then it will throw an <em>IllegalStateException - Nesting problem</em>. Just to clarify, I am trying to use the mixed GSON writing to take advantage of mixed streaming &amp; object model access. So I am trying to grab the MobileAmsData complex object, stream-serialize it to JSON and http PUT it towards a remote server.</p>
  
  <p><strong>EDITED2:</strong> The JSON I am trying to send would look like this if serialized using </p>
  
  <blockquote>
    <p>String parsed = gson.toJson(myData, MobileData.class);</p>
  </blockquote>
  
  <pre><code>{
     "Things1":[
  
     ],
     "Things2":[
  
     ],
     "Things3":[
  
     ],
     "Things4":[
  
     ],
  
     "Things1Count":0,
     "Things2Count":0,
     "Things3Count":0,
     "Things4Count":0,
     "Count":2,
     "BarcodeCount":1
  }
  </code></pre>