The post variable is never being set and for now, all I'm trying to do is set that variable but it's always null
All my credentials are correct and I am accessing the the php file but just unable to post any data at all. Could this be a problem on my server?
This is my java code:
try {
URL url = new URL("http://ggh.com/ghh.php");
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestProperty("Content-Type", "application/json");
conn.setRequestProperty("Accept", "application/json");
conn.setRequestMethod("POST");
conn.setReadTimeout(15 * 1000);
conn.setConnectTimeout(15 * 1000);
conn.setDoOutput(true);
conn.setDoInput(true);
OutputStream os = conn.getOutputStream();
BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(os, "UTF-8"));
Uri.Builder builder = new Uri.Builder()
.appendQueryParameter("password", password)
.appendQueryParameter("username", username);
String query = builder.build().getEncodedQuery();
bufferedWriter.write(query);
bufferedWriter.flush();
bufferedWriter.close();
os.close();
conn.connect();
InputStream inputStream = conn.getInputStream();
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream, "iso-8859-1"));
String line = "";
while ((line = bufferedReader.readLine()) != null) {
response += line;
}
try {
objmain = new JSONObject(response);
jsonArray = objmain.getJSONArray("apps");
for (int i = 0; i < jsonArray.length(); i++) {
obj = jsonArray.getJSONObject(i);
pass = (obj.getString("password"));
pass_hash = (obj.getString("password_hash"));
first_name = (obj.getString("first_name"));
last_name = (obj.getString("last_name"));
email_personal = (obj.getString("email_personal"));
phone_personal = (obj.getString("phone_personal"));
linkedin = (obj.getString("linkedin"));
company_name = (obj.getString("company_name"));
job_title = (obj.getString("job_title"));
phone_professional = (obj.getString("phone_professional"));
email_professional = (obj.getString("email_professional"));
address = (obj.getString("address"));
database = username + "_db";
}
} catch (JSONException e) {
e.printStackTrace();
}
bufferedReader.close();
inputStream.close();
conn.disconnect();
} catch (IOException e) {
e.printStackTrace();
}
My php code:
<?php
$servername = "sad.com";
$username = "ad";
$password = "adad";
// Create connection
$conn = new mysqli($servername, $username, $password);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
mysqli_select_db($conn,"connkt");
function hashPassword($password, $salt) {
$hash_password = hash("SHA512", base64_encode(str_rot13(hash("SHA512", str_rot13($salt . $password)))));
return $hash_password;
}
$user = $_POST['username'];
$pass = $_POST['password'];
$hashed_password = hashPassword('someday', $pass);
$response["apps"] = array();
$result = mysqli_query($conn, "Select * FROM `users` WHERE username='$user' LIMIT 1");
while($row=mysqli_fetch_array($result))
{
$apps = array();
$apps["password"] = $row["password"];
$apps["first_name"] = $row["first_name"];
$apps["last_name"] = $row["last_name"];
$apps["email_personal"] = $row["email_personal"];
$apps["facebook"] = $row["facebook"];
$apps["phone_personal"] = $row["phone_personal"];
$apps["linkedin"] = $row["linkedin"];
$apps["company_name"] = $row["company"];
$apps["job_title"] = $row["title"];
$apps["phone_professional"] = $row["phone_professional"];
$apps["email_professional"] = $row["email_professional"];
$apps["address"] = $row["address"];
$apps["password_hash"] = $hashed_password;
array_push($response["apps"], $apps);
}
print(json_encode($user));
I would suggest using Volley to send data to the server :
http://www.androidhive.info/2014/05/android-working-with-volley-library-1/
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.