1

Trying to register a user into the database.

This code works:

try {
            URL url = new URL("http://www.jacksteel.co.uk/Comp4/registeruser.php?" + getQuery(userData));
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            conn.setReadTimeout(10000);
            conn.setConnectTimeout(15000);
            conn.setRequestMethod("POST");
            conn.setDoInput(true);
            conn.setDoOutput(true);


            OutputStream os = conn.getOutputStream();
            BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(os, "UTF-8"));
            writer.write(getQuery(userData));
            Log.d("myTag", getQuery(userData));

            writer.flush();
            writer.close();
            os.close();
            Log.d("myTag",conn.getInputStream().toString());


            conn.connect();

        } catch (Exception e) {
            e.printStackTrace();
        }

However this code does not:

 try {
            URL url = new URL("http://www.jacksteel.co.uk/Comp4/registeruser.php");
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            conn.setReadTimeout(10000);
            conn.setConnectTimeout(15000);
            conn.setRequestMethod("POST");
            conn.setDoInput(true);
            conn.setDoOutput(true);


            OutputStream os = conn.getOutputStream();
            BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(os, "UTF-8"));
            writer.write(getQuery(userData));
            Log.d("myTag", getQuery(userData));

            writer.flush();
            writer.close();
            os.close();
            Log.d("myTag",conn.getInputStream().toString());


            conn.connect();

        } catch (Exception e) {
            e.printStackTrace();
        }

The only difference being that I concatenated the result of getQuery() onto the end of the URL, which suggests that the HTTP post is not working, I got the code fore the Post from here I already have the data in a nameValuePair list called 'userData'

So, my question is why does it not work without appending getQuery(userData) to the URL, and can I make it work?

Community
  • 1
  • 1
Jack
  • 391
  • 5
  • 25
  • What's the error you get when it doesn't work? – Daniel Nugent Nov 07 '15 at 20:23
  • @DanielNugent I don't get an error it just doesn't insert anything into the database – Jack Nov 07 '15 at 20:25
  • What is the full response from the server when it doesn't work? – Daniel Nugent Nov 07 '15 at 20:33
  • @DanielNugent just noticed i had the line `Log.d("myTag",conn.getInputStream().toString());` commented out, uncommented it and tried again, same thing happens except in both cases this time i get the following error for that log line: `java.io.FileNotFoundException: http://www.jacksteel.co.uk/comp4/registeruser.php` – Jack Nov 07 '15 at 20:41
  • Strange. Try calling `conn.connect()` before logging `conn.getInputStream()`. Also try moving the logging to the place where you successfully got a result when it was working. – Daniel Nugent Nov 07 '15 at 20:45
  • I suspect the first experiment is performing a GET request. Strip out all the outputstream part to test it. Also, are you sure that the registeruser.php does accept POST requests? – Little Santi Nov 07 '15 at 21:09
  • Got it working without appending `getQuery(userData)` but it still throws the FileNotFoundException at the logging, even after moving `conn.connect` above it – Jack Nov 07 '15 at 21:32

0 Answers0