Insert in MySQL with Android Studio using Legacy does not work

0

Like the last time you helped me in a very fast and effective way I come back to share my code. I try to insert a record in a MYSQL from an App developed on Android and although it seems that everything works there is something that escapes me.

I use the library: useLibrary 'org.apache.http.legacy'

Activity:

import android.os.AsyncTask;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class Garantia extends AppCompatActivity implements View.OnClickListener {

    String ServerURL = "http://web/get_data.php" ;
    String TempName, TempEmail;

    EditText nombre, desc;
    //TextView resultado;
    Button insertar;


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

        nombre = (EditText)findViewById(R.id.nombre);
        desc = (EditText)findViewById(R.id.desc);
        insertar = (Button)findViewById(R.id.insertar);
        //resultado = (TextView)findViewById(R.id.resultado);

        insertar.setOnClickListener(this);


    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.insertar:

                GetData();
                InsertData(TempName, TempEmail);

                break;

            default:

                break;
        }
    }

    public void InsertData(final String nombre, final String desc){

        class SendPostReqAsyncTask extends AsyncTask<String, Void, String> {
            @Override
            protected String doInBackground(String... params) {

                String NombreHolder = nombre ;
                String descHolder = desc ;

                List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
                nameValuePairs.add(new BasicNameValuePair("nombre", NombreHolder));
                nameValuePairs.add(new BasicNameValuePair("desc", descHolder));

                try {
                    HttpClient httpClient = new DefaultHttpClient();
                    HttpPost httpPost = new HttpPost(ServerURL);
                    httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
                    HttpResponse httpResponse = httpClient.execute(httpPost);
                    HttpEntity httpEntity = httpResponse.getEntity();

                } catch (ClientProtocolException e) {
                } catch (IOException e) {
                }
                return "Data Inserted Successfully";
            }

            @Override
            protected void onPostExecute(String result) {
                super.onPostExecute(result);
                Toast.makeText(Garantia.this, "Data Submit Successfully", Toast.LENGTH_LONG).show();
            }
        }
        SendPostReqAsyncTask sendPostReqAsyncTask = new SendPostReqAsyncTask();
        sendPostReqAsyncTask.execute(nombre, desc);
    }

    public void GetData(){
        TempName = nombre.getText().toString();
        TempEmail = desc.getText().toString();
    }
}

Returns Data Submit Successfully.

PHP code I attack:

<?php

$HostName = "localhost";
$HostUser = "---";
$HostPass = "---";
$DatabaseName = "---";

 $con = mysqli_connect($HostName,$HostUser,$HostPass,$DatabaseName);

 $nombre = $_POST['nombre'];
 $desc = $_POST['desc'];

 $Sql_Query = 'insert into garantia (nombre,info) values ('.$nombre.','.$desc.')';

 if(mysqli_query($con,$Sql_Query)){

 echo 'Data Submit Successfully';

 }
 else{

 echo 'Try Again';

 }
 mysqli_close($con);
?>

Any suggestions?

    
asked by Energy Panel 03.04.2018 в 09:21
source

1 answer

0

I auto answered with the solution, the problem was in the PHP code.

<?php

$enlace = new PDO("mysql:host=localhost;dbname=---;charset=utf8", "----", "---");
$sql = "insert into garantia (nombre,info) values ('".$_POST['nombre']."','".$_POST['desc']."')";

 if($enlace->query($sql)){ echo 'Data Submit Successfully'; }
 else{ echo 'Try Again'; }

 mysqli_close($con);
?>

Well, I hope you can help someone with this summary code to insert in Mysql from Android:)

    
answered by 03.04.2018 в 10:01