title: HttpURLConnection - Response Code: 400 (Bad Request) Android Studio -> xserve
body: <p>So I'm trying to connect to our database via Xserve, AT the moment I'm trying to access the token for the user. I'm using the correct username and password along with the context type and grant type; I know this because I've tried the same POST method via googles postmaster extension. For whatever reason when I try the same thing on Android, at least what I think is the same, it gives me a 400 response code and doesn't return anything.</p>
  
  <p>Here's the code used to connect: </p>
  
  <pre><code>private HttpURLConnection urlConnection;
  
  @Override
      protected Boolean doInBackground(Void... params) {
          Boolean blnResult = false;
          StringBuilder result = new StringBuilder();
          JSONObject passing = new JSONObject();
  
          try {
              URL url = new URL("http://xserve.uopnet.plymouth.ac.uk/modules/INTPROJ/PRCS251M/token");
  
              // set up connection
              urlConnection = (HttpURLConnection) url.openConnection();
              urlConnection.setDoInput(true);
              urlConnection.setDoOutput(true);
              urlConnection.addRequestProperty("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8" );
              urlConnection.setRequestMethod("POST");
              urlConnection.connect();
  
              // set up parameters to pass
              passing.put("username", mEmail);
              passing.put("password", mPassword);
              passing.put("grant_type", "password");
  
              // add parameters to connection
              OutputStreamWriter wr= new OutputStreamWriter(urlConnection.getOutputStream());
              wr.write(passing.toString());
  
  
              // If request was good
              if (urlConnection.getResponseCode() == 200) {
                  blnResult = true;
                  BufferedReader reader = new BufferedReader(
                          new InputStreamReader(urlConnection.getInputStream()));
                  String line;
  
                  while ((line = reader.readLine()) != null) {
                      result.append(line);
                  }
                  reader.close();
              }
  
              //JSONObject json = new JSONObject(builder.toString());
              Log.v("Response Code", String.format("%d", urlConnection.getResponseCode()));
              Log.v("Returned String", result.toString());
  
          }catch( Exception e) {
              e.printStackTrace();
          }
          finally {
              urlConnection.disconnect();
          }
  
          return blnResult;
      }
  </code></pre>
  
  <p>I haven't stored the result into the JSONObject yet as I'll use that later, but I expected some kind of output via the "Log.v".</p>
  
  <p>Is there anything that stands out?</p>