-1

I am very new to Android and I have tried the solutions given in lots of posts from here and other websites and none works for me. I would like to pass two variables from an Android Activity (when a button is clicked).

What I am trying is to pass variables from an Activity to a php page. Will this work on the simulator? Or I will have to release the app to see it working?

Thank you for your time

By now, I have the following code (thanks to Sohail Zahid):

public class Main3Activity extends AppCompatActivity {

public String num_pal, precio, l_origen, l_destino, texto, banner_id, full_id;

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

    Intent intent=getIntent();
    double precio = intent.getExtras().getDouble("precio");
    String l_origen = intent.getExtras().getString("l_origen");
    String l_destino = intent.getExtras().getString("l_destino");
    String texto= intent.getExtras().getString("texto");
    int pal = intent.getExtras().getInt("num_pal");
    num_pal = String.valueOf(pal);
    String precio_rounded = String.format("%.2f", precio);
    TextView txtCambio = (TextView) findViewById(R.id.textView4);
    txtCambio.setText("Precio Total: "+ precio_rounded + " €");
}

void MakePostRequest() {
        String posting_url ="https://www.example.org/movil/ingles/page.php";

        StringRequest postRequest = new StringRequest(Request.Method.POST, posting_url ,
            new Response.Listener<String>() {
                @Override
                public void onResponse(String response) {
                    try {
                        JSONObject jsonResponse = new JSONObject(response);
                        precio= jsonResponse.getString("precio_rounded");
                        l_origen= jsonResponse.getString("l_origen");
                        l_destino= jsonResponse.getString("l_destino");
                        texto= jsonResponse.getString("texto");
                        num_pal= jsonResponse.getString("num_pal");

                    } catch (JSONException e) {
                        e.printStackTrace();
                        banner_id = null;
                        full_id = null;
                    }
                }
            },
            new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    error.printStackTrace();
                    precio= null;
                    l_origen= null;
                    l_destino= null;
                    texto= null;
                }
            }
    ) {
        // here is params will add to your url using post method
        @Override
        protected Map<String, String> getParams() {
            Map<String, String> params = new HashMap<>();
            params.put("precio", precio);
            params.put("l_origen", l_origen);
            params.put("l_destino", l_destino);
            params.put("texto", texto);
            params.put("num_pal", num_pal);
            //params.put("2ndParamName","valueoF2ndParam");
            return params;
        }
    };
    Volley.newRequestQueue(this).add(postRequest);
}

And a button with an "onclick"

But the application closes and reports error when this button is clicked.

Do anyone have an idea of why?

alberzyzz
  • 277
  • 2
  • 15
  • I also read that I won't be able to do something else with those variables than showing them in the screen, so if I want to "play" with them, I will have to store them in a database. Is that true? What do You mean by that? – Whencesoever Jul 12 '16 at 09:39
  • For exemple, if I want to resend those variables to an other php page, I will need to have them stored in a database? Thank you for your answer – alberzyzz Jul 12 '16 at 09:41
  • That's not true. Let's say You send a variable from Android to first php script. It manipulates it. Manipulated variable is send to another php. You must know that that is not the same object. You can just pass values. Not variables by names. – Whencesoever Jul 12 '16 at 09:44
  • For sending params using post methed http://stackoverflow.com/questions/37908320/failed-to-send-parameter-to-php-post-parameter-android/37909623#37909623 – Sohail Zahid Jul 12 '16 at 09:45
  • Yes, that was I wanted to mean, if the value would remain the same or it would have a "null" value. Thanks! – alberzyzz Jul 12 '16 at 09:46

2 Answers2

2

Send parameters/values to php script using Post Method here is simple example.

Post Request

void MakePostRequest() {
            String posting_url ="http://webIpaddress/android/ads/ids.php";
            // its your url path ok
            StringRequest postRequest = new StringRequest(Request.Method.POST, posting_url ,
                    new Response.Listener<String>() {
                        @Override
                        public void onResponse(String response) {
                            try {
                                JSONObject jsonResponse = new JSONObject(response);
                               String value1= jsonResponse.getString("Your ID1");
                               String value2= jsonResponse.getString("Your ID2");

                            } catch (JSONException e) {
                                e.printStackTrace();
                                banner_id = null;
                                full_id = null;
                            }
                        }
                    },
                    new Response.ErrorListener() {
                        @Override
                        public void onErrorResponse(VolleyError error) {
                            error.printStackTrace();
                           String  value1= null;
                           String  value2= null;
                        }
                    }
            ) {
           // here is params will add to your url using post method
                @Override
                protected Map<String, String> getParams() {
                    Map<String, String> params = new HashMap<>();
                    params.put("app","sendingValuesHere");
                    //params.put("2ndParamName","valueoF2ndParam");
                    return params;
                }
            };
            Volley.newRequestQueue(this).add(postRequest);
        }
Community
  • 1
  • 1
Sohail Zahid
  • 8,099
  • 2
  • 25
  • 41
1

You can use post or get to send variables by URL to PHP

And a final question: I also read that I won't be able to do something else with those variables than showing them in the screen, so if I want to "play" with them, I will have to store them in a database. Is that true?

can store them in a database or assign to global variable to use it later

Phu Duy
  • 187
  • 1
  • 8