0

I am currently attempting to create a log in app with the new API that does not allow the HTTPClient and HTTPPost methods due to their recent depreciation and was wondering why my current method is not working.

        String userparam = user.username;
        String passwordparam = user.password;
        URL obj = null;
        HttpURLConnection con = null;

        try {
            Log.d("JJK", "CONNECTION ATTEMPT");
            obj = new URL(SERVER_ADDRESS+"register.php");
            con = (HttpURLConnection) obj.openConnection();


            String userpass = //the username and password I use to log into my 000webhost server/phpadmin/etc
            con.setRequestProperty("Authorization", userpass);
            con.setDoOutput(true);
            con.setRequestProperty("Content-Type", "application/json");
            con.setRequestProperty("charset", "utf-8");
            con.setRequestProperty("Accept", "application/json");
            con.setConnectTimeout(CONNECTION_TIMEOUT);
            con.setRequestMethod("POST");
            con.connect();

            String jsonParam  = URLEncoder.encode("username", "UTF-8")
                    + "=" + URLEncoder.encode(userparam, "UTF-8");
            jsonParam += "&" + URLEncoder.encode("password", "UTF-8")
                    + "=" + URLEncoder.encode(passwordparam, "UTF-8");

            //POST STUFF
            DataOutputStream outputstream = new DataOutputStream(con.getOutputStream());
            outputstream.writeBytes(jsonParam);
            outputstream.flush();
            outputstream.close();

This is my POST connecting stuff I am working on in Android Studio at the moment. The server_address refers to my IP address on 000webhost

$con=mysqli_connect(hostname, username, password, dbname);

if (mysqli_connect_errno($con)) {
   echo "Failed to connect to MySQL: " . mysqli_connect_error();
exit();
}


//gets the username and password that has been send to this php file
$username = $_POST['username'];
$password = $_POST['password'];

//stores the values into the database with a sql query
$statement = mysqli_prepare($con, "INSERT INTO 'User' (username, password) VALUES (?,?)");
mysqli_stmt_bind_param($statement, "ss", $username, $password);
mysqli_execute($statement);
mysqli_stmt_close($statement);
mysqli_close($con);

Here I have my php file which is stored on my http://IP_NAME/register.php but for some reason I cannot seem to parse the json object in my php file there is simply no response and thus I am now stuck and was wondering if anyone knew more about this way of doing this?

Jin Kim
  • 17
  • 2
  • your posting json in the body?. If so instead of encoding use `writeUTF(String)` if you need utf -8 encoding. http://developer.android.com/reference/java/io/DataOutputStream.html#writeUTF(java.lang.String). – Raghunandan Jul 16 '15 at 03:19
  • I think I am just passing the string to php. I am getting a connection and whatnot with a POST Response Code :: 200. The URL is my ip address given to me by the website and then I add the register.php to the url string? – Jin Kim Jul 16 '15 at 04:13
  • Take a look at this answer, it has a fully working and tested solution for exactly what you're trying to do in your Java code: http://stackoverflow.com/questions/31253519/add-a-progressdialog-to-json-parser-class-and-return-method-for-mainactivity/31256592#31256592 – Daniel Nugent Jul 16 '15 at 04:18
  • The JSONParser used uses HTTPClient which are the depreciated values I was mentioning so it would not work in API 22 though – Jin Kim Jul 16 '15 at 04:31

0 Answers0