简体   繁体   English

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

[英]Android app can't insert data into MySQL DB

I'm fairly new to Android coding and I'm trying to create app that inserts some info into my MySQL DB. 我对Android编码还很陌生,我正在尝试创建将一些信息插入我的MySQL DB的应用程序。 I've found a lot of tutorials and tips on web and created lite app to try procedures. 我在网上找到了很多教程和技巧,并创建了lite应用程序来尝试操作过程。 Everything compiles OK, app runs and it seems to send data successfully. 一切编译正常,应用程序运行,似乎可以成功发送数据。 But in fact, no data appears in my table. 但实际上,我的表中没有数据。
Here's my PHP code android_add.php: 这是我的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);
?>

And here's my MainActivity.java: 这是我的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);
    }

}

As I said, I'm just beginner, so there may be something really stupid, but I can't figure out what. 正如我所说,我只是个初学者,所以可能有些愚蠢的事情,但我不知道是什么。 And there can be some trash lines from different tries. 并且可能会有来自不同尝试的一些垃圾线。 PHP code should be OK, I'm using practically the same to insert from HTML, so I'm guessing, there is problem in my Java code. PHP代码应该可以,我实际上使用的是从HTML插入的代码,所以我猜我的Java代码有问题。
I will be really thankful for advices/responses. 我将非常感谢您的建议/回应。
Thanks! 谢谢!

PS: Response code I'm getting is 200. PS:我得到的响应码是200。

You are sending null values through AsyncTask Have you printed the values that you are sending through 您通过AsyncTask发送空值是否打印了要通过发送的值
try this 尝试这个

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

try again and let me know if it solves your problems.... 再试一次,让我知道是否可以解决您的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM