Post request for registering user data on server by HttpUrlConnection
<p>I am currently working on a project in which I need to send username,password and email of a user for registering the user on my server. I used POST command for that and as I have to send 3 values for register a user,I used ContentValues for that purpose. Here is my code:</p>

<pre><code>@Override
    protected String doInBackground(String... params) {
        try {
            url = new URL(params[0]);
            httpURLConnection = (HttpURLConnection) url.openConnection();
            httpURLConnection.setDoInput(true);
            httpURLConnection.setDoOutput(true);
            httpURLConnection.setRequestMethod("POST");

            ContentValues values = new ContentValues();
            values.put("email", "abc@xyz.com");
            values.put("password", "123");
            values.put("name","ABC");

            outputStream = httpURLConnection.getOutputStream();

            bufferedWriter = new BufferedWriter(new OutputStreamWriter(outputStream));
            bufferedWriter.write(getQuery(values));
            bufferedWriter.flush();

            statusCode = httpURLConnection.getResponseCode();

            Log.i("Result",String.valueOf(statusCode));

            inputStream = new BufferedInputStream(httpURLConnection.getInputStream());

            InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
            int data = inputStreamReader.read();
            while (data != -1) {
                char current = (char) data;
                result += current;
                data = inputStreamReader.read();
            }

            JSONObject jsonObject = new JSONObject(result);

            Log.i("Result",String.valueOf(jsonObject));

            if (statusCode == 200) {
                inputStream = new BufferedInputStream(httpURLConnection.getInputStream());

                Log.i("Result","Correct Data Returned");

                JSONObject jsonObject = new JSONObject(response);

                return true;

            } else {
                Log.i("Result","Data not returned");
                return false;
            }

        } catch (Exception e) {

            e.printStackTrace();
        } finally {
            try {
                if (inputStream != null) {
                    inputStream.close();
                }
                if (outputStream != null) {
                    outputStream.close();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return null;
    }
</code></pre>

<p>Here is my getQuery method:</p>

<pre><code>private String getQuery(ContentValues values) throws UnsupportedEncodingException
    {
        StringBuilder result = new StringBuilder();
        boolean first = true;

        for (Map.Entry&lt;String, Object&gt; entry : values.valueSet())
        {
            if (first)
                first = false;
            else
                result.append("&amp;");

            result.append(URLEncoder.encode(entry.getKey(), "UTF-8"));
            result.append("=");
            result.append(URLEncoder.encode(String.valueOf(entry.getValue()), "UTF-8"));
        }

        Log.i("Result",result.toString());

        return result.toString();
    }
</code></pre>

<p>But I am getting following response by it:</p>

<pre><code>Result: name=ABC&amp;email=abc%40xyz.com&amp;password=123
Result: 422
System.err: java.io.FileNotFoundException: http://docpanel.myworkdetails.com/api/auth/register
</code></pre>

<p>Where 422 is the status code returned by responseCode that means :</p>

<pre><code>"errors": {
     "email": [
       "The email field is required."
     ],
     "password": [
       "The password field is required."
     ],
     "name": [
       "The password field is required."
     ]
   },
   "status_code": 422
</code></pre>

<p>I am not getting how to pass arguements by POST method so as to make my signup page working. And I have the correct URL.</p>

<p>Is this server side fault or I am making mistakes in implementing POST?</p>

<p>Please Help!
Thanks in advance.</p>