Question

I'm trying to send data to the server but it seems that I always send null values, any idea? The idea is to add a new customer through the mobile application to my database hosted in a server.

Here's my code:

    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_nuevo_insert);

    //etResponse = (EditText) findViewById(R.id.etResponse2);
    etNombre = (EditText) findViewById(R.id.etNombre);
    etApellido = (EditText) findViewById(R.id.etApellido);
    etEdad = (EditText) findViewById(R.id.etEdad);

    nombre = etNombre.getText().toString();
    apellido = etApellido.getText().toString();
    edad = etEdad.getText().toString();

    } 

    public void insertar(View view) {
    // Call AsyncTask to perform network operation on separate thread
    // working in localhost you CAN'T put localhost in that address, you
    // MUST put your IP address or it will crush
    new HttpAsyncTask().execute("http://192.168.1.34/android/insertCustomer.php");
    }

    public static String GET(String url) {
    InputStream inputStream = null;
    String result = "";
    try {
        // create HttpClient
        HttpClient httpClient = new DefaultHttpClient();

        // make GET request to the given URL
        HttpResponse httpResponse = httpClient.execute(new HttpGet(url+ "?nombre=" +    nombre + "&apellido=" + apellido + "&edad="+ edad));

        // receive response as InputStream
        inputStream = httpResponse.getEntity().getContent();

        // convert InputStream to string
        if (inputStream != null) {
        result = convertInputStreamToString(inputStream);
        } else {
        result = "No ha funcionat!";
        }
     } catch (Exception e) {
        Log.d("InputStream", e.getLocalizedMessage());
     }
        return result;
    }

    private class HttpAsyncTask extends AsyncTask<String, Void, String> {

    @Override
    protected String doInBackground(String... urls) {
        return GET(urls[0]);
    }

    // onPostExecute displays the results of the AsyncTask
    @Override
    protected void onPostExecute(String result) {
        String s = "";
        Toast.makeText(getBaseContext(),getResources().getString(R.string.rebut), Toast.LENGTH_LONG).show();
    JSONArray jArray;
    try {
        jArray = new JSONArray(result);
        for (int i = 0; i < jArray.length(); i++) {
        JSONObject json = jArray.getJSONObject(i);

        s = s + "Nom: " + json.getString("FirsName") + " "
            + json.getString("LastName") + "\n" + "Edat: "+ json.getInt("Age") + "\n\n";
        }
        etResponse.setText(s);
    } catch (JSONException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

}
}

This is my php file:

    <?php
        $con = mysql_connect('localhost', 'root', '');

        if(!$con){

        die("No se ha podido realizar la conexion: ".mysql_error());

        }

        mysql_select_db("TestDatabase", $con);

        $nombre = $_GET['nombre'];
        $apellido = $_GET['apellido'];
        $edad = $_GET['edad'];

        print_r($nombre."-".$apellido."-".$edad);

        $result = mysql_query("insert into customer(FirsName, LastName, Age) values         ('$nombre', '$apellido', '$edad')");

        mysql_close($con);

?>
Was it helpful?

Solution 2

If you are getting null value means that mean u r passing wrong type parameters or url may be wrong you do check it out

OTHER TIPS

OK the problem was that I was retrieving the data from EditText boxes in the onCreate and I had to do it in the GET method :-)

Change

 HttpResponse httpResponse = httpClient.execute(new HttpGet(url+ "?nombre=" +    nombre + "&apellido=" + apellido + "&edad="+ edad));

to this:

String request = url+ "?nombre=" +    nombre + "&apellido=" + apellido + "&edad="+ edad;
Log.d("DEBUG", request); 
HttpResponse httpResponse = httpClient.execute(request);

and see your logcat for your url, maybe it is broken. if the url is ok, then try opening this url in your browser and check the results.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top