Android将空白行添加到db

时间:2016-10-23 19:56:23

标签: java php android mysql

我正在尝试向数据库插入输入,但每当我尝试时,它都会向其添加空行。我已经创建了一个完美的html表单。这是我的代码,我将不胜感激。

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();
}

}

调试完成后,我得到了数据&#34;变量&#34; BackgroundTask&#34;如

sender = fggff&amp; header = kkkjjj&amp; text = qwwqwq

他们必须在引号中吗?或者是空白问题?

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);
}

1 个答案:

答案 0 :(得分:0)

请记住,您的后台任务在调用时不会立即执行插入操作。我认为你传递参考参数(不是标量),因此调用者可以在后台任务有机会使用它们之前更改它们。

两个选择:不要在后台执行 - 对于本地数据库,单个插入非常快,通常可以在UI线程上完成。

或者,在将字符串发送到后台任务之前复制字符串。