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.

Sheler
  • 3,969
  • 3
  • 12
  • 21
  • Is `$address` in the INSERT statement supposed to be `$author`? – Benjy1996 Jan 12 '16 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`. – ddelnano Jan 12 '16 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`](http://stackoverflow.com/a/273090/5663450) to detect any exceptions thrown by the `mysqli_*` statements and see what the issue might be – Benjy1996 Jan 12 '16 at 18:58
  • 2
    I would check the PHP script first. Change your query vars to $_GET, and go to your browser http://domain.com/script.php?name=name&author=author - does your database populate? – markt Jan 12 '16 at 19:03
  • I checked the php script using [Postman](https://www.getpostman.com/), which can be very helpful to debug errors with a php script like this. – ddelnano Jan 12 '16 at 19:10
  • the $address error was from all the trying to repair that - didn't help. Thanks to @markt - Good idea that helped - there was somewhere error, but I still don't know where. I had to completely rewrite whole code, but it's finally working. Thanks for tips! Now I have to find out my Java code is sending blank strings. – Sheler Jan 12 '16 at 22:02

1 Answers1

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....

Mahesh Giri
  • 1,810
  • 19
  • 27
  • 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. – Sheler Jan 12 '16 at 22:10