0

I can't understand how to switch to POST method in my HttpsURLConnection. On the debugger the request method is GET also after the setRequestMethod method. Can you tell me where is my mistake?

try {
                URL url=new URL("https://smartmates.herokuapp.com");
                HttpsURLConnection connection= (HttpsURLConnection) url.openConnection();
                connection.setRequestMethod("POST");
                connection.setDoInput(true);
                connection.setDoOutput(true);
                //I'll add some params here
                connection.disconnect();

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

Thank you very much.

ficus
  • 3
  • 2

1 Answers1

1

This is a piece of code that I use to POST the String mensaje and receiving rta.ToString(). I think that DoSetInput(true) is a mistake, because you want to send a POST (output) and, eventually, get a response.

 `
    String urlParametros = "<?xml version=\"1.0\"?>";
    urlParametros = urlParametros + mensaje;
    byte[] postDatos = urlParametros.getBytes(StandardCharsets.UTF_8);
    try {
        URL miurl = new URL(url);
        con = (HttpURLConnection) miurl.openConnection();
        con.setDoOutput(true);
        con.setRequestMethod("POST");
        //******…………..
        con.setRequestProperty("User-Agent", "Java client");
        con.setRequestProperty("Content-Type", "text/xml");
        try (DataOutputStream datos = new DataOutputStream(con.getOutputStream())) {
            datos.write(postDatos);
        }
        StringBuilder rta;
        try (BufferedReader entrada = new BufferedReader(
                new InputStreamReader(con.getInputStream()))) {
            String linea;
            rta = new StringBuilder();
            while ((linea = entrada.readLine()) != null) {
                rta.append(linea);
                rta.append(System.lineSeparator());
            }
        }
        return rta.toString();
    } finally {
        con.disconnect();
    }´

Hope it helps

Daniel

  • thank you very much, can you also tell me if Request property such as User-Agent and content-type are compulsory? i guess that this code will send an xml body, right? how can i send a JsonObject? – ficus Jan 28 '19 at 20:52
  • Not sure. I think there are ïnstructions" for the server. In my case, I send XML in the POST.Try connect.addRequestProperty("Accept", "application/json"); – Daniel Contarino Jan 29 '19 at 00:10
  • I tried your solution but unfurtunately it didn't worked, so i tried to run the same code in eclipse and i can't understand why but it works. May it be that there is a bug in android studio? Because in eclipse everything works as it should... Thanks for your help – ficus Jan 29 '19 at 18:26
  • I don't understand: the piece of code I gave you worked in Eclipse but not in Android Studio? It only needs the obvious Imports, using a separate thread from the UI, and Internet permission in the Manifest. – Daniel Contarino Jan 29 '19 at 18:41
  • in eclipse works the part about posting datas but the problem is that running that code in android studio the requestMethod remains GET during all the runtime – ficus Jan 29 '19 at 20:06
  • Weird. I put a breakpont after 'setRequestMethod', and check con. The 'requestMethod' is "POST". How do you check the method? Do you get some error? – Daniel Contarino Jan 29 '19 at 22:39
  • Me too, I put some breakpoints and I check all the time the connection fronte the debugger. I know it's weird, maybe there are some problems with my mobile android version or the android studio version. There isn't any error, sometimes It crashes, sometimes It go on but of course It doesn't work – ficus Jan 30 '19 at 06:25
  • I had a nice Galaxy S6 that was no capable of sending more tan 13 KB in a POST (a jpg) and hangs. But, the same app in any other cellphone, worked perfectly. Same Android versión, etc. – Daniel Contarino Jan 30 '19 at 12:37