0

I'm fairly new to Android coding and I'm trying to create app that inserts some info into my MySQL DB. I've found a lot of tutorials and tips on web and created lite app to try procedures. 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
$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:

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.
I will be really thankful for advices/responses.
Thanks!

PS: Response code I'm getting is 200.

6
  • Is $address in the INSERT statement supposed to be $author? Commented Jan 12, 2016 at 18:55
  • The php code is not correct. I get a 200 status but it returns the string "failure". I believe you meant to put $author in the query and not $address. Commented Jan 12, 2016 at 18:55
  • 1
    Are you getting exceptions in your error log for the PHP? You could also try surrounding the code in a try-catch to detect any exceptions thrown by the mysqli_* statements and see what the issue might be Commented Jan 12, 2016 at 18:58
  • 2
    I would check the PHP script first. Change your query vars to $_GET, and go to your browser domain.com/script.php?name=name&author=author - does your database populate? Commented Jan 12, 2016 at 19:03
  • I checked the php script using Postman, which can be very helpful to debug errors with a php script like this. Commented Jan 12, 2016 at 19:10

1 Answer 1

0

You are sending null values through AsyncTask Have you printed the values that you are sending through
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....

Sign up to request clarification or add additional context in comments.

1 Comment

I resolved one issue in PHP, but even with your code, I am sending blank strings (input null value into DB). Strange is that even when I define value just before putting them to content values, it's still not working.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.