I have an app, which is quite simple. In preparation for a dictionary it is supposed to take words and enter it in a database. The entering part is done by a php script, which works fine. The link that is produced works as well, I tried it manually. May I ask, what I have overseen in this code, as I haven't found anything considering the topic on Google:
private class SendData extends AsyncTask<Void, Void, Void>{
@Override
protected Void doInBackground(Void... voids) {
HttpURLConnection urlConnection = null;
try {
URL url = new URL(uri.toString());
EditText text = (EditText) findViewById(R.id.oshikwanyamaEx);
text.setText(url.toString());
urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setRequestMethod("POST");
urlConnection.connect();
}catch (MalformedURLException m){
m.printStackTrace();
}catch (IOException e){
e.printStackTrace();
}finally {
if (urlConnection != null){
urlConnection.disconnect();
}
}
return null;
}
@Override
protected void onPostExecute(Void aVoid) {
Toast.makeText(MainActivity.this, "Data was sent", Toast.LENGTH_LONG).show();
super.onPostExecute(aVoid);
}
}
Thank you in advance This is the PHP code:
<?php
//Creating a connection
$con = mysqli_connect("breidinga.lima-db.de:3306","USER373834","*********","db_373834_1");
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
//INSERT INTO `ENGOKY` (`_id`, `eng`, `oky`, `type`, `engex`, `okyex`, `okypl`, `engpl`) VALUES (NULL, '', '', '', NULL, NULL, NULL, NULL)
$sql = "INSERT INTO `ENGOKY` (`_id`, `eng`, `oky`, `type`, `engex`, `okyex`, `okypl`, `engpl`) VALUES (NULL,'".$_GET["eng"]."', '";
//$sql = $sql.$_GET["oky"]."', '".$_GET["type"]."', '".$_GET["engex"]."', '".$_GET["okyex"]."', '".$_GET["okyex"]."', '".$_GET["ekypl"]."', '".$_GET["engpl"]."')";
$sql = $sql.$_GET["oky"]."', '".$_GET["type"]."', NULL, NULL, NULL, NULL)";
$sql = str_replace("''","NULL", $sql);
mysqli_query($con ,$sql);
mysqli_free_result($result);
mysqli_close($con);
?>
A little improvement to your code and some more declaration
URL url = new URL("your URL here");
JSONObject postDataParams = new JSONObject();
postDataParams.put("word", "onomatopoeia"); //Change #1
Log.e("params",postDataParams.toString());
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setReadTimeout(15000); //Change #2
conn.setConnectTimeout(15000); //Change #2
conn.setRequestMethod("POST");
conn.setDoInput(true);
conn.setDoOutput(true);
OutputStream os = conn.getOutputStream();
BufferedWriter writer = new BufferedWriter(
new OutputStreamWriter(os, "UTF-8"));
writer.write(getPostDataString(postDataParams));
writer.flush();
writer.close();
os.close();
1 - You need to pass in the parameters into your PHP file as I can't see any evidence of anything being passed in. Assuming the variable is similar to the follow: -
$word= $_POST['word'];
2 - Always useful to set the connection timeout and read timeout as this will terminate the request after some time. In this case, it's 15 seconds.
3 - As a guidance, just make sure there is a return response from the PHP file as you want to make sure if your insertion was successful or not.
4 - Add the following permission, may make a difference
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
5 - Read the response code from the connection and this will determine if it even bothered doing anything.
int response = conn.getResponseCode();
Log.d(DEBUG_TAG, "Response code is: " + response);
if ((response >= 200) && (response < 300)) {
Log.d(DEBUG_TAG, "The response is: " + conn.getResponseMessage());
}
6. User receiving response code 301 (Redirection) and add this line to stop getting redirected
conn.setInstanceFollowRedirects(false);