0

I am new to android and I am using O-Authentication 1.0 in my code. Get request is working but post request is not working in my code. When I check in postman I get response but not getting request in the code. Here is my code : protected String doInBackground(String... params) {

        try {
            URL url = new URL(params[0]);
            HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
            httpURLConnection.setDoInput(true);
            httpURLConnection.setDoOutput(true);
            httpURLConnection.setRequestProperty("Content-Type", "application/json");
            httpURLConnection.setRequestMethod("POST");


            // Send the post body
            if (this.postData != null) {
                OutputStreamWriter writer = new OutputStreamWriter(httpURLConnection.getOutputStream());
                writer.write(postData.toString());
                writer.flush();
            }

            int statusCode = httpURLConnection.getResponseCode();

            if (statusCode == 200) {

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

            } else {
                // Status code is not 200
                // Do something to handle the error
            }

        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

Thanks in advance.

Risk
  • 23
  • 1
  • 7

1 Answers1

0

You could use these for rest api requests.

http://square.github.io/retrofit/

http://square.github.io/okhttp/

It reduce your code length and lines and efficiently makes your stuff load faster and saves bandwidth. It supports both synchronous blocking calls and async calls with callbacks.

JosephM
  • 2,935
  • 4
  • 18
  • 28
  • It is helpful but I am not getting how to use O-Authentication 1.0. How can I use it in my code? Can you please share an example to explain this ? – Risk Oct 31 '17 at 09:38
  • you can do it by adding Interceptor to request. go through this link https://stackoverflow.com/questions/18478258/android-retrofit-parameterized-headers – JosephM Nov 10 '17 at 05:42