Php was unable to receive data sent by Android using HttpURLConnection
I am trying to send some data from Android to server (database) using OutputStreamWriter, but I cannot see the result in the database and Logcat is ok. I tried the solutions posted on stackoverflow but no luck. Here are my codes: -
private class sentFeedback extends AsyncTask<String,Void,String>{
@Override
protected String doInBackground(String... params) {
if(isNetworkAvailable()){
try{
URL feedback = new URL("http:.../setFeedback.php");
HttpURLConnection conn = (HttpURLConnection) feedback.openConnection();
conn.setDoOutput(true);
conn.setRequestMethod("POST");
OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());
String data = getData(params);
wr.write(data);
wr.flush();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}else{
return "No internet connection";
}
return null;
}
public String getData(String... params) throws UnsupportedEncodingException {
String data = "id" + "=" + URLEncoder.encode(params[0], "UTF-8");
data += "&" + "rating" + "=" + URLEncoder.encode(params[1], "UTF-8");
data += "&" + "message" + "=" + URLEncoder.encode(params[2], "UTF-8");
return data;
}
}
php code: -
$id = $_POST['id'];
$rating = (double)$_POST['rating'];
$message = $_POST['message'];
$feedbackQuery = "INSERT INTO feedback(message, id, rating) VALUES ('$message', '$id', '$rating')";
$feedbackQueryResult = $mysqli->query($feedbackQuery);
$mysqli->close();
I am currently using the 000webhost free account. Is this the reason why I can send data back?
Thanks in advance!
+3
source to share
1 answer