I am trying to insert inputs to database but whenever I try, it adds empty rows to it. I have created an html form that works perfectly. Here is my code I would appreciate any help.
MyFragment.java
public class MyFragment extends Fragment{
EditText senderEt, headerEt, textEt;
Button btn;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
final View rootView = inflater.inflate(R.layout.my_fragment, container, false);
senderEt = (EditText)rootView.findViewById(R.id.sender);
headerEt = (EditText)rootView.findViewById(R.id.header);
textEt = (EditText)rootView.findViewById(R.id.text);
btn = (Button) rootView.findViewById(R.id.btnSend);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
addToDB(getView());
}
});
return rootView;
}
public void addToDB(View view){
String sender= senderEt.getText().toString();
String header= headerEt.getText().toString();
String text= textEt.getText().toString();
BackgroundTask backgroundTask = new BackgroundTask(getActivity());
backgroundTask.execute(sender, header, text);
}
}
BackgroundTask.java
public class BackgroundTask extends AsyncTask<String, Void, String> {
Context context;
BackgroundTask(Context context){
this.context=context;
}
@Override
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected String doInBackground(String... params) {
String add_url= "http://139.179.196.153:8080/addDB.php";
String sender = params[0];
String header = params[1];
String text = params[2];
try {
URL url = new URL(add_url);
HttpURLConnection httpURLConnection = (HttpURLConnection)url.openConnection();
httpURLConnection.setRequestMethod("POST");
httpURLConnection.setDoOutput(true);
OutputStream outputStream = httpURLConnection.getOutputStream();
BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(outputStream, "UTF-8"));
String data = URLEncoder.encode("sender", "UTF-8") + " = "+URLEncoder.encode(sender, "UTF-8")+"&"+
URLEncoder.encode("header", "UTF-8") + " = "+URLEncoder.encode(header, "UTF-8")+"&"+
URLEncoder.encode("text", "UTF-8") + " = "+URLEncoder.encode(text, "UTF-8");
bufferedWriter.write(data);
bufferedWriter.flush();
bufferedWriter.close();
outputStream.close();
InputStream inpInputStream = httpURLConnection.getInputStream();
inpInputStream.close();
return "Add to DB Success";
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onProgressUpdate(Void... values) {
super.onProgressUpdate(values);
}
@Override
protected void onPostExecute(String res) {
Toast.makeText(context, res, Toast.LENGTH_SHORT).show();
}
}
After I debugged, I got "data" variable in "BackgroundTask" as
sender = fggff&header = kkkjjj&text = qwwqwq
do they have to be in quotes? or are the blanks problem?
php code
<?php
$db_name = "test";
$db_user = "root";
$db_password = "";
$db_server_name = "localhost";
$con = new mysqli($db_server_name, $db_user, $db_password, $db_name);
if($con->connect_error){
echo "Connection error".mysqli_connect_error();
}
else{
echo "<h3>Database connection success</h3>";
}
$sender = $_POST["sender"];
$header = $_POST["header"];
$text = $_POST["text"];
$sql_query = "insert into things values('$sender','$header','$text')";
if(mysqli_query($con, $sql_query)){
echo "<h3>Data insertion success</h3>";
}
else{
echo "<Data insertion error</h3>".mysqli_error($con);
}
Keep in mind that your background task will not do the inserts immediately when called. I think you are passing reference parameters (that is not scalars) and so the caller could be changing them before the background task has a chance to use them.
Two choices: don't do in background - for a local db, a single insert is really fast and can usually be done on the UI thread.
Or, make copies of the strings before you send them to the background task.