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?