title: POST JSON Object using Volley on Android to get Authentication Token
body: <p>I need to send a POST request to a server using the following JSON object format:</p>
  
  <pre><code>{"auth": {"tenantName": "&lt;tenant&gt;", "passwordCredentials": {"username": "&lt;user&gt;", "password": "&lt;password&gt;"}}}
  </code></pre>
  
  <p>to get an authentication token as response for subsequent requests:</p>
  
  <pre><code>{
  access: {
  token: {
  issued_at: "2014-11-03T01:45:53.819103"
  expires: "2014-11-03T02:45:53Z"
  id: "686889ba4244432696aaac1d022f4973" 
  </code></pre>
  
  <p>So far I'm just testing the connection with volley to see if I can get the proper response in a Toast:</p>
  
  <pre><code>public void loginRequest(){
      String[] credentials = getSharedPrefs();
      final String user = credentials[0];
      final String pass = credentials[1];
      final String url = credentials[2];
  
      JSONObject login = new JSONObject();
      JSONObject result = new JSONObject();
      try {
          JSONObject auth = login.getJSONObject("auth");
          JSONObject tenantName = auth.getJSONObject("tenantName");
          JSONObject passwordCredentials = auth.getJSONObject("passwordCredentials");
          JSONObject username = passwordCredentials.getJSONObject("username");
          JSONObject password = passwordCredentials.getJSONObject("password");
          login.put("tenantName","");
          login.put("username",user);
          login.put("password",pass);
      } catch (JSONException e) {
          e.printStackTrace();
      }
  
  
      JsonObjectRequest getRequest = new JsonObjectRequest(Request.Method.POST, url, login,
              new Response.Listener&lt;JSONObject&gt;()
              {
                  @Override
                  public void onResponse(JSONObject response) {
                      // display response
                      // TO DO
                      Toast toast = Toast.makeText(Login.this,response.toString(),Toast.LENGTH_LONG);
                      toast.setGravity(Gravity.TOP|Gravity.CENTER_HORIZONTAL,0,120);
                      toast.show();
  
                  }
              },
              new Response.ErrorListener()
              {
                  @Override
                  public void onErrorResponse(VolleyError error) {
                      Toast.makeText(Login.this, error.getMessage(), Toast.LENGTH_LONG).show();
                  }
              }
      ){
  
          protected Map&lt;String, String&gt; getParams() {
              Map&lt;String, String&gt; params = new HashMap&lt;String, String&gt;();
              params.put("tenantName", "");
              params.put("username", user);
              params.put("password", pass);
              return params;
          }
  
          public Map&lt;String, String&gt; getHeaders() throws AuthFailureError {
              Map&lt;String, String&gt; params = new HashMap&lt;String, String&gt;();
              params.put("Accept", "application/json");
              params.put("Content-Type", "application/json");
              return params;
          }
  
      };
  
          RequestQueue queue = Volley.newRequestQueue(this);
          queue.add(getRequest);
      }
  }
  </code></pre>
  
  <p>However I'm getting the following error:</p>
  
  <pre><code>E/Volley﹕ [893] NetworkDispatcher.run: Unhandled exception java.lang.RuntimeException: Bad URL
  java.lang.RuntimeException: Bad URL
          at com.android.volley.toolbox.BasicNetwork.performRequest(BasicNetwork.java:147)
          at com.android.volley.NetworkDispatcher.run(NetworkDispatcher.java:110)
   Caused by: java.net.MalformedURLException: Protocol not found:
          at java.net.URL.&lt;init&gt;(URL.java:176)
          at java.net.URL.&lt;init&gt;(URL.java:125)
          at com.android.volley.toolbox.HurlStack.performRequest(HurlStack.java:101)
          at com.android.volley.toolbox.BasicNetwork.performRequest(BasicNetwork.java:96)
              at com.android.volley.NetworkDispatcher.run(NetworkDispatcher.java:110)
  </code></pre>
  
  <p>Any idea what I'm missing? </p>
  
  <p>EDIT: I did some more digging and realised the error above was caused by a wrong getter on the string "url". I fixed that but now I get:</p>
  
  <pre><code>E/Volley﹕ [787] BasicNetwork.performRequest: Unexpected response code 400 for http://10.10.10.10:5000/v2.0/tokens 
  </code></pre>
  
  <p>It seems the code is not sending JSON properly. For instance if I try to send anything other than aplication/json to the site using the Advanced Rest Client I also get <strong><em>400 Bad Request</em></strong> </p>