Rok_ivi
Rok_ivi

Reputation: 13

editText message is not getting saved into the database

I am stuck where my editText message in android is not getting stored int he database and i cannot find the problem. I am quite new to programing so any help here would be much appritiated.

MY Php file:

    <?php

$DB_HOST = "localhost";
$DB_USER = "root";
$DB_PASSWORD = "";
$DB_DATABASE = "vas_uporabniki";

$conn = mysqli_connect($DB_HOST, $DB_USER, $DB_PASSWORD, $DB_DATABASE);

$jakost_bolecine = $_POST["jakost_bolecine_post"];

$mysqli_qry ="INSERT into test (jakost_bolecine,datum_in_cas) 
values ('$jakost_bolecine', CURRENT_TIMESTAMP())";

if($conn->query($mysqli_qry)=== TRUE){

    echo "Uspešno shranjeno";
}
else {

    echo "Napaka pri shranjevanju".$mysqli_qry."<br>".$conn->error;
}
$conn->close();

?>

My mainactivity file:

public class MainActivity extends ActionBarActivity {

   // private TextView test;
    private TextView textView;
    private ImageView imageView;
    private SeekBar seekBar;
    private TextView strI;

    EditText test;


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

       seekBar = (SeekBar) findViewById( seek_bar_id );

        test = (EditText)findViewById(R.id.test_id);

       initializeVariables();

       textView.setText( "Jakost bolečine: " + seekBar.getProgress() + "/" + seekBar.getMax() );

       seekBar.setOnSeekBarChangeListener( new OnSeekBarChangeListener() {
           int progress = 0;

           @Override
           public void onProgressChanged(SeekBar seekBar, int progresValue, boolean fromUser) {
               progress = progresValue;
           }

           @Override
           public void onStartTrackingTouch(SeekBar seekBar) {
           }

           @Override
           public void onStopTrackingTouch(final SeekBar seekBar) {

               textView.setText( "Jakost bolečine: " + progress + "/" + seekBar.getMax() );
               if (progress == 0) {

                   imageView.setImageResource( R.drawable.no_pain );
               }
               if (progress <= 2 && progress >= 1) {
                   imageView.setImageResource( R.drawable.little_pain );
                   Toast.makeText( getApplicationContext(), "Blaga bolečina!", Toast.LENGTH_SHORT ).show();
               }

               if (progress <= 4 && progress >= 3) {
                   imageView.setImageResource( R.drawable.moderata_pain );
                   Toast.makeText( getApplicationContext(), "Zmerna bolečina!", Toast.LENGTH_SHORT ).show();
               }

               if (progress <= 6 && progress >= 5) {
                   imageView.setImageResource( R.drawable.severe_pain );
                   Toast.makeText( getApplicationContext(), "Srednja bolečina!", Toast.LENGTH_SHORT ).show();
               }

               if (progress <= 8 && progress >= 7) {
                   imageView.setImageResource( R.drawable.very_severe_pain );
                   Toast.makeText( getApplicationContext(), "Dokaj močna bolečina!", Toast.LENGTH_SHORT ).show();
               }

               if (progress <= 10 && progress >= 9) {
                   imageView.setImageResource( R.drawable.worst_pain );
                   Toast.makeText( getApplicationContext(), "Zelo močna bolečina!", Toast.LENGTH_SHORT ).show();
               }


               String strI = String.valueOf(progress);


           }

       } );


   }
    private void initializeVariables() {
        seekBar = (SeekBar) findViewById(R.id.seek_bar_id);
        textView = (TextView) findViewById(R.id.jakost_bolecin_id);
        //test = (TextView) findViewById(R.id.test);
        imageView=(ImageView)findViewById( R.id.slika_bolecine );

    }

    public void poslji (View view){

        String poslji_data = test.getText().toString().trim();
        String type ="Poslji";
        BackgroundWorker backgroundWorker = new BackgroundWorker(this);
        backgroundWorker.execute(type, poslji_data);
        }
}

BackgroundWorker.java

public class BackgroundWorker extends AsyncTask<String,Void,String> {

    Context context;
    AlertDialog alertDialog;
    BackgroundWorker (Context ctx) {
        context=ctx;
    }
    @Override
    protected  String doInBackground (String... params){
        String type = params [0];
        String poslji_url= "http://localhost/vas_lestvica_login/posiljanje_podatkov/poslji_podatke.php";
        if(type.equals("Poslji")) {
            try {
                String jakost_bolecine = params[1];
                URL url = new URL( poslji_url );
                HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
                httpURLConnection.setRequestMethod( "POST" );
                httpURLConnection.setDoOutput( true );
                httpURLConnection.setDoInput( true );
                OutputStream outputStream = httpURLConnection.getOutputStream();
                BufferedWriter bufferedWriter = new BufferedWriter( new OutputStreamWriter( outputStream, "UTF-8" ) );
                String post_data = URLEncoder.encode( "jakost_bolecine_post", "UTF-8" ) + "=" + URLEncoder.encode( jakost_bolecine, "UTF-8");
                bufferedWriter.write( post_data );
                bufferedWriter.flush();
                bufferedWriter.close();
                InputStream inputStream = httpURLConnection.getInputStream();
                BufferedReader bufferedReader = new BufferedReader( new InputStreamReader( inputStream, "iso-8859-1" ) );
                String result = "";
                String line = "";
                while ((line = bufferedReader.readLine()) != null) {

                    result += line;
                }
                bufferedReader.close();
                inputStream.close();
                httpURLConnection.disconnect();
                return result;
            } catch (MalformedURLException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
            return null;
        }

    }

And the xml main_Act:

<?xml version="1.0" encoding="utf-8"?>

<ScrollView
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.rok.myapplication.MainActivity">

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <TextView
        android:paddingTop="50dp"
        android:id="@+id/krog"
        android:textSize="20dp"
        android:text="@string/povleci_krot"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:textAlignment="center"
        android:textColor="#000"/>

    <ImageView
        android:paddingTop="40dp"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/slika_bolecine"
        android:src="@drawable/no_pain"
        />

    <SeekBar
        android:id="@+id/seek_bar_id"
        android:thumb="@drawable/seekbar"
        android:paddingTop="40dp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:max="10"
        android:progress="0"
        />

    <TextView
        android:paddingTop="40dp"
        android:id="@+id/jakost_bolecin_id"
        android:textSize="20dp"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:textAlignment="center"
        android:textColor="@color/TextMainColor"/>



    <Button
        android:id="@+id/gumb_poslji"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:background="#008000"
        android:layout_marginTop="50dp"
        android:text="@string/gumb_shrani"
        android:textAllCaps="false"
        android:textColor="@color/white"
        android:textSize="15dp"
        android:onClick="poslji"/>

    <EditText
        android:id="@+id/test_id"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:paddingTop="40dp"
        android:hint="vnesi nekaj"
        android:textAlignment="center"
        android:textColor="@color/TextMainColor"
        android:textSize="20dp"/>

</LinearLayout>
</ScrollView>

Upvotes: 0

Views: 38

Answers (1)

user5125586
user5125586

Reputation:

The problem is in BackgroundWorker doInBackgroundMethod. You cannot connect to localhost from android by simply using localhost.

Here is the problem:

String poslji_url= "http://localhost/vas_lestvica_login/posiljanje_podatkov/poslji_podatke.php";

You need to make sure your testing machine and development machine are on the same network. You may connect mobile devices via wifi and/or usb thetering. Depending on your setup.

If you are testing with the android emulator, change it to:

String poslji_url= "http://10.0.2.2/vas_lestvica_login/posiljanje_podatkov/poslji_podatke.php";

If you are using a third party emulator like Genymotion for example, you may try:

String poslji_url= "http://10.0.3.2/vas_lestvica_login/posiljanje_podatkov/poslji_podatke.php";

Otherwise, for example while testing on a real device, try this:

  1. Get your IP address: for windows, simple type ipconfig in command prompt or ifconfig on mac and linux systems.

It should give you something like 192.168.43.129 etc...

  1. Then change the line to:

    String poslji_url= "http://192.168.43.129/vas_lestvica_login/posiljanje_podatkov/poslji_podatke.php";

Replacing 192.168.43.129 with whatever ipconfig or ifconfig gives you.

Upvotes: 1

Related Questions