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?