title: From HttpClient to HttpURLconnection
body: <p>I want to upgrade my code for posting a json.
  The first one implements HttpClient and it works fine!
  I tried to use the new implementation using HttpURLconnection and it doesn't work! I can't send any post request.
  What I'm missing?</p>
  
  <pre><code>public class AsyncPostBG extends AsyncTask&lt;String, String, String&gt; {
  
      private ProxyState mData = null;
      private String mName = null;
  
      public AsyncPostBG(ProxyState data, String name)
      {
          mData = data;
          mName = name;
      }
  
      @Override
      protected String doInBackground(String... params){
          HttpParams httpParams = new BasicHttpParams();
          HttpClient httpClient = new DefaultHttpClient(httpParams);
          HttpPost postMethod = new HttpPost(params[0]);
          postMethod.setHeader("Content-Type", "application/json; charset=utf-8");
          postMethod.setHeader("Accept", "*/*");
          postMethod.setHeader("Accept-Encoding", "gzip, deflate");
          Gson gson=new Gson();
          String json = gson.toJson(mData);
          try {
              postMethod.setEntity(new ByteArrayEntity(json.getBytes("UTF8")));
              HttpResponse response = httpClient.execute(postMethod);
              StatusLine statusLine = response.getStatusLine();
          }
          catch (UnsupportedEncodingException e){
  
          }
          catch (Exception e) {
          }
          return json;
      }
  }
  </code></pre>
  
  <p>Here is my HttpURLConnection implementation:</p>
  
  <pre><code>private class AsyncStatePostBG extends AsyncTask&lt;String, Void,  String&gt; {
      private ProxyObj mData = null;
      private String mName = null;
  
      public AsyncStatePostBG(ProxyObj data, String name) {
          mData = data;
          mName = name;
      }
  
      @Override
      protected String doInBackground(String... params) {
          Gson gson = new Gson();
          String json = gson.toJson(mData);
  
          HttpURLConnection connection = null;
          try {
              URL object = new URL(ComURL + "api/state/" + mName);
              connection = (HttpURLConnection) object.openConnection();
  
              connection.setDoOutput(true);
              connection.setDoInput(true);
              connection.setRequestMethod("POST");
              connection.setRequestProperty("Content-Type", "application/json");
              connection.setRequestProperty("Accept", "*/*");
              connection.setRequestProperty("Accept-Encoding", "gzip, deflate");
              connection.connect();
              OutputStreamWriter streamWriter = new OutputStreamWriter(connection.getOutputStream());
              streamWriter.write(json);
              streamWriter.flush();
              streamWriter.close();
  
          } catch (Exception exception) {
  
              return null;
          }
          return json;
  
      }
  }
  </code></pre>