0

I am trying to send a POST request to my server. I have successfully send a POST request using Postman app so I assume I have messed it up somewhere in my Android app. The code that i used is described here sample code

Boolean makePostServiceCall(String postUrl, String json){
    Log.d(TAG, "makePostServiceCall: starts");

    try{

        URL url = new URL(postUrl);
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        conn.setDoOutput(true);
        conn.setRequestProperty("Content-Type", "application/json");
        conn.setRequestProperty("Accept", "application/json");
        conn.setRequestMethod("POST");
        conn.connect();

        //Write
        OutputStream os = conn.getOutputStream();
        BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(os, "UTF-8"));
        writer.write(json);
        writer.close();
        os.close();



    } catch (MalformedURLException e) {
        e.printStackTrace();
        return false;
    } catch (ProtocolException e) {
        Log.e(TAG, "ProtocolException: " + e.getMessage());
        return false;
    } catch (IOException e) {
        Log.e(TAG, "IOException: " + e.getMessage());
        return false;
    }

    return true;
}

The parameter json looks like this D/PostOrder: doInBackground: json string is: {"tableID":"2","price":"22","payment":"cash","quantity":"1","products":[{"ID":"1","quantity":"1"}]}

Any help will be greately appreciated

Giannis
  • 121
  • 2
  • 4
  • 13
  • What error are you getting? – Software2 Jul 10 '17 at 18:25
  • The problem is that there is no row added to the table that I am trying to insert data. There is no error the app runs but no data is stored. I have properly made a Get request but Post request is not working – Giannis Jul 10 '17 at 18:27
  • 1
    So then the server is sending you a 200 response? – Software2 Jul 10 '17 at 18:30
  • 1
    Do you check status code? Do you get any response from the server? If you get a response from the server then try to read it. It may tell you the problem. – Birendra Singh Jul 10 '17 at 18:33
  • Actually the issue is that HttpURLConnection is deprecated now and if send request to server using HttpURLConnection the request will not be recieved my server so better use OkhttpRequest – yash786 Jul 10 '17 at 18:36
  • @yash786 `HttpClient` is deprecated not `HttpURLConnection.` – Birendra Singh Jul 10 '17 at 18:53
  • Thanks a lot for your time guys especially @VirendraSingh. I had ommited reading from the server. Problem solved – Giannis Jul 10 '17 at 19:08

0 Answers0