title: HttpURLConnection working in Java but not android
body: <p>We've been trying to send a POST request to a node.js server in an android app. Because the old apache dependencies are deprecated (and I can't seem to access them - I've tried) we've been using the HttpURLConnection classes. We've coded a class in java that works as just a standalone class (Request.java) but when incorperated in the android program, it throws an error every time and when trying to get the message of the error, it just returns null.</p>
  
  <p>Request.java</p>
  
  <pre><code>package andrewmmattb.beacongame;
  
  /**
   * Created by matt on 05/03/2016.
   */
  
  import java.net.HttpURLConnection;
  import java.io.BufferedReader;
  import java.io.DataOutputStream;
  import java.io.InputStreamReader;
  import java.net.URL;
  
  public class Request {
  
      public static void main(String[] args) throws Exception {
          Request http = new Request();
  
          System.out.println("POST");
          http.sendPost("{\"username\": \"matt\",\"newPoints\":5}");
      }
  
      public static void sendPost(String json) throws Exception {
  
          String url = "http://ec2-54-187-69-193.us-west-2.compute.amazonaws.com/points";
          URL obj = new URL(url);
          HttpURLConnection con = (HttpURLConnection) obj.openConnection();
  
          //add reuqest header
          con.setRequestMethod("POST");
          con.setRequestProperty("User-Agent", "");
          con.setRequestProperty("Accept-Language", "en-US,en;q=0.5");
          con.setRequestProperty("Content-Type", "application/json");
  
          String urlParameters = "";
  
          // Send post request
          con.setDoOutput(true);
          DataOutputStream wr = new      DataOutputStream(con.getOutputStream());
          wr.writeBytes(json);
          wr.flush();
          wr.close();
  
          int responseCode = con.getResponseCode();
          System.out.println("\nSending 'POST' request to URL : " + url);
          System.out.println("Post parameters : " + urlParameters);
          System.out.println("Response Code : " + responseCode);
  
          BufferedReader in = new BufferedReader(
                  new InputStreamReader(con.getInputStream()));
          String inputLine;
          StringBuffer response = new StringBuffer();
  
          while ((inputLine = in.readLine()) != null) {
              response.append(inputLine);
          }
          in.close();
  
          //print result
          System.out.println(response.toString());
      }
  }
  </code></pre>
  
  <p>GameActivity.java</p>
  
  <pre><code>package andrewmmattb.beacongame;
  
  import android.app.Activity;
  import android.app.DownloadManager;
  import android.content.Intent;
  import android.os.Bundle;
  import android.os.Debug;
  import android.os.Handler;
  import android.util.Base64;
  import android.util.JsonWriter;
  import android.util.Log;
  import android.view.Menu;
  import android.view.MenuItem;
  import android.widget.TextView;
  import android.widget.Toast;
  
  import org.json.*;
  
  import java.io.BufferedOutputStream;
  import java.io.BufferedReader;
  import java.io.IOException;
  import java.io.InputStream;
  import java.io.InputStreamReader;
  import java.io.OutputStream;
  import java.io.OutputStreamWriter;
  import java.net.HttpURLConnection;
  import java.net.MalformedURLException;
  import java.net.URL;
  import java.util.ArrayList;
  import java.util.Collection;
  import java.util.HashMap;
  import java.util.Map;
  import java.util.Set;
  
  public class GameActivity extends Activity {
  
  String username;
  String serverPath = "THE PATH TO THE SERVER";
  
  int score = 0;
  int prevScore = 0;
  
  TextView usernameTextView;
  TextView scoreTextView;
  
  @Override
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_game);
  
      usernameTextView = (TextView)findViewById(R.id.textViewGameUsername);
      scoreTextView = (TextView)findViewById(R.id.textViewGameScore);
  
      Intent intent = getIntent();
      username = intent.getStringExtra("username");
      usernameTextView.setText(username);
  
      try {
          makeSeverPost();
      }
      catch (IOException e) {
          Toast.makeText(GameActivity.this,"There was an IO error, called after function call (line 56)",Toast.LENGTH_LONG).show();
          e.printStackTrace();
      }
  }
  
  void makeSeverPost() throws IOException {
      // creates a map object with username and the additional points to the previous sent score
      Map&lt;String,Object&gt; values = new HashMap&lt;String,Object&gt;();
      values.put("username",username);
      values.put("newPoints",score-prevScore);
      // sets the previous score to equal the current score
      prevScore = score;
  
      // writes the map into a string in JSON format
      String jsonString = new JSONObject(values).toString();
  
      try {
          Request.sendPost(jsonString);
      } catch (Exception e) {
          e.printStackTrace();
          Log.e("problem",""+e.getMessage());
      }
      }
  }
  </code></pre>
  
  <p>There are many redundant dependancies due to all the other attempts to do this we've made.</p>