Android应用无法将数据插入MySQL DB

时间:2016-01-12 18:52:00

标签: java php android mysql mysqli

我对Android编码很新,我正在尝试创建将一些信息插入MySQL数据库的应用程序。我在网上找到了很多教程和技巧,并创建了精简版应用来尝试程序。一切都编译好,应用程序运行,似乎成功发送数据。但实际上,我的表中没有数据出现。
这是我的PHP代码android_add.php:

<?php
$con = mysqli_connect(localhost, user, psswd, name);   //those works
mysqli_set_charset($con, "utf8");     //working with special symbols

$name = $_POST['name'];       //get name & author from App
$author = $_POST['author'];

$sql = "insert into kniha_test (k_autor_pr,k_nazev) values ('$name','$address')";                
if(mysqli_query($con,$sql)){
    echo 'success';
} else {
    echo 'failure';
}
mysqli_close($con);
?>

这是我的MainActivity.java:

import android.content.ContentValues;
import android.os.AsyncTask;
import android.provider.Settings;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;


import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.ProtocolException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {

    private EditText editTextName;
    private EditText editTextAuthor;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        editTextName = (EditText) findViewById(R.id.editTextName);
        editTextAuthor = (EditText) findViewById(R.id.editTextAuthor);
    }

    public void insert (View view){
        String name = editTextName.getText().toString();
        String author = editTextAuthor.getText().toString();

        insertToDatabase(name,author);
    }

    protected void insertToDatabase(String name, String author){
        class SendPostReqAsyncTask extends AsyncTask<String, Void, String> {


            String name;
            String author;
            public void saveNameAut(String name, String author){
                this.name = name;
                this.author = author;
                name = editTextName.getText().toString();
                author = editTextAuthor.getText().toString();
            }

            @Override
            protected String doInBackground(String... params){
                String paramName = params[0];
                String paramAuthor = params[1];



                ContentValues values = new ContentValues();
                values.put("name", this.name);
                values.put("author", this.author);

                String addUrl = "http://glaserproject.com/knihovna_kyber/android/android_add.php";

                try {URL url = new URL(addUrl);
                HttpURLConnection conn = (HttpURLConnection) url.openConnection();
                conn.setRequestMethod("POST");

                System.out.println("Response Code: " + conn.getResponseCode());

                } catch (IOException e){};

                return "Succes";
            }

            @Override
            protected void onPostExecute(String result){
                super.onPostExecute(result);

                Toast.makeText(getApplicationContext(), result, Toast.LENGTH_LONG).show();
                TextView textViewResult = (TextView) findViewById(R.id.textViewResult);
                textViewResult.setText("inserted");
            }
        }
        SendPostReqAsyncTask sendPostReqAsyncTask = new SendPostReqAsyncTask();
        sendPostReqAsyncTask.execute(name, author);
    }

}

正如我所说,我只是初学者,所以可能有些东西真的很蠢,但我无法弄清楚是什么。并且可能存在来自不同尝试的一些垃圾线。 PHP代码应该没问题,我使用几乎相同的从HTML插入,所以我猜,我的Java代码存在问题。
我将非常感谢建议/回复。
感谢!

PS:我得到的响应代码是200。

1 个答案:

答案 0 :(得分:0)

您正在通过AsyncTask发送空值 您是否打印了通过发送的值? 试试这个

protected void insertToDatabase(String name, String author){
    class SendPostReqAsyncTask extends AsyncTask<String, Void, String> {


        String cName=name;
        String cAuthor=author;


        @Override
        protected String doInBackground(String... params){

            ContentValues values = new ContentValues();
            values.put("name", cName);
            values.put("author", cAuthor);

            String addUrl = "http://glaserproject.com/knihovna_kyber/android/android_add.php";

            try {URL url = new URL(addUrl);
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            conn.setRequestMethod("POST");

            System.out.println("Response Code: " + conn.getResponseCode());

            } catch (IOException e){};

            return "Succes";
        }

        @Override
        protected void onPostExecute(String result){
            super.onPostExecute(result);

            Toast.makeText(getApplicationContext(), result, Toast.LENGTH_LONG).show();
            TextView textViewResult = (TextView) findViewById(R.id.textViewResult);
            textViewResult.setText("inserted");
        }
    }
    SendPostReqAsyncTask sendPostReqAsyncTask = new SendPostReqAsyncTask();
    sendPostReqAsyncTask.execute(name, author);
}

再试一次,让我知道它是否解决了你的问题......