Skip to content Skip to sidebar Skip to footer

Post Request For Registering User Data On Server By Httpurlconnection

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

Solution 1:

Why don't you just concatenate the url with the parameters?

The request would then look like this: "http://example.com/test?param1=a&param2=b&param3=c"

Solution 2:

You could use HttpPost instead of HttpURLConnection

finalHttpParamshttpParams=newBasicHttpParams();
        HttpConnectionParams.setConnectionTimeout(httpParams, Constants.CONNECTION_TIMEOUT);
        HttpClienthttpClient=newDefaultHttpClient(httpParams);
        HttpPosthttpPost=newHttpPost("yourBackendUrl");
        List<NameValuePair> nameValuePairs = newArrayList<NameValuePair>();
        nameValuePairs.add(newBasicNameValuePair("key","value"));
        httpPost.setEntity(newUrlEncodedFormEntity(nameValuePairs));
        HttpResponseresponse= httpClient.execute(httpPost);
        InputStreaminStream= response.getEntity().getContent();
        builder = newStringBuilder();
        BufferedReaderreader=newBufferedReader(newInputStreamReader(inStream));
        String line;
        while ((line = reader.readLine()) != null) {
            builder.append(line);
        }
        JSONObjectjson=newJSONObject(builder.toString());

Solution 3:

Solved

I was making mistakes that I didn't add httpUrlConnection.connect() at right place and also httpURLConnection.setRequestProperty("Accept","/") at right place made it working fine. So here is the correct code by which POST request using HttpUrlConnection is used to make signUp page possible:

protectedStringdoInBackground(String... params) {
        try {
            url = newURL(params[0]);
            httpURLConnection = (HttpURLConnection) url.openConnection();
            httpURLConnection.setDoInput(true);
            httpURLConnection.setDoOutput(true);
            httpURLConnection.setRequestMethod("POST");
            httpURLConnection.setRequestProperty("Accept","*/*");

            ContentValues values = newContentValues();
            values.put("email", "abc@tjk.com");
            values.put("password", "hjh");
            values.put("name","hui");

            httpURLConnection.connect();

            outputStream = httpURLConnection.getOutputStream();

            bufferedWriter = newBufferedWriter(newOutputStreamWriter(outputStream));
            bufferedWriter.write(getQuery(values));
            bufferedWriter.flush();

            statusCode = httpURLConnection.getResponseCode();

            inputStream = httpURLConnection.getInputStream();

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

                JSONObject jsonObject = newJSONObject(result);

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

            } else {
                Log.i("Result","false");
                returnfalse;
            }

        } catch (Exception e) {

            e.printStackTrace();
        } finally {
            try {
                if (inputStream != null) {
                    inputStream.close();
                }
                if (outputStream != null) {
                    outputStream.close();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        returnnull;
    }

Post a Comment for "Post Request For Registering User Data On Server By Httpurlconnection"