只需将变量从Android发送到php即可

只需将变量从Android发送到php即可,php,android,variables,Php,Android,Variables,我对安卓非常陌生,我在这里和其他网站的许多帖子中尝试过这些解决方案,但没有一个适合我。我想从Android活动中传递两个变量(单击按钮时) 我尝试的是将变量从活动传递到php页面这能在模拟器上工作吗?或者我必须发布应用程序才能看到它工作? 谢谢你抽出时间 现在,我有以下代码(感谢Sohail Zahid): public class main3活动扩展了appcompative活动{ 公共字符串num_pal、precio、l_origen、l_destino、texto、banner_id、f

我对安卓非常陌生,我在这里和其他网站的许多帖子中尝试过这些解决方案,但没有一个适合我。我想从Android活动中传递两个变量(单击按钮时)

我尝试的是将变量从活动传递到php页面这能在模拟器上工作吗?或者我必须发布应用程序才能看到它工作?

谢谢你抽出时间

现在,我有以下代码(感谢Sohail Zahid):

public class main3活动扩展了appcompative活动{
公共字符串num_pal、precio、l_origen、l_destino、texto、banner_id、full_id;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main3);
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_四舍五入+”€);
}
void MakePostRequest(){
字符串发布\u url=”https://www.example.org/movil/ingles/page.php";
StringRequest postRequest=新的StringRequest(Request.Method.POST,posting\u url,
新的Response.Listener(){
@凌驾
公共void onResponse(字符串响应){
试一试{
JSONObject jsonResponse=新的JSONObject(响应);
precio=jsonResponse.getString(“precio_取整”);
l_origen=jsonResponse.getString(“l_origen”);
l_destino=jsonResponse.getString(“l_destino”);
texto=jsonResponse.getString(“texto”);
num_pal=jsonResponse.getString(“num_pal”);
}捕获(JSONException e){
e、 printStackTrace();
banner_id=null;
完整id=空;
}
}
},
新的Response.ErrorListener(){
@凌驾
公共无效onErrorResponse(截击错误){
错误。printStackTrace();
precio=null;
l_origen=null;
l_destino=null;
texto=null;
}
}
) {
//以下是参数将使用post方法添加到您的url
@凌驾
受保护的映射getParams(){
Map params=新的HashMap();
参数put(“precio”,precio);
参数put(“l_origen”,l_origen);
参数put(“l_destino”,l_destino);
参数put(“texto”,texto);
参数put(“num_-pal”,num_-pal);
//参数put(“2ndParamName”、“valueoF2ndParam”);
返回参数;
}
};
newRequestQueue(this.add)(postRequest);
}
还有一个带“onclick”的按钮

但单击此按钮时,应用程序将关闭并报告错误


有人知道为什么吗?

您可以使用post或get通过URL将变量发送到PHP


最后一个问题:我还读到,除了在屏幕上显示这些变量之外,我无法对这些变量执行其他操作,因此,如果我想“玩”它们,我必须将它们存储在数据库中。这是真的吗


可以将它们存储在数据库中,也可以分配给全局变量以便以后使用

使用Post方法将参数/值发送到php脚本这里是一个简单的示例

void MakePostRequest(){
字符串发布\u url=”http://webIpaddress/android/ads/ids.php";
//这是你的url路径吗
StringRequest postRequest=新的StringRequest(Request.Method.POST,posting\u url,
新的Response.Listener(){
@凌驾
公共void onResponse(字符串响应){
试一试{
JSONObject jsonResponse=新的JSONObject(响应);
stringvalue1=jsonResponse.getString(“您的ID1”);
stringvalue2=jsonResponse.getString(“您的ID2”);
}捕获(JSONException e){
e、 printStackTrace();
banner_id=null;
完整id=空;
}
}
},
新的Response.ErrorListener(){
@凌驾
公共无效onErrorResponse(截击错误){
错误。printStackTrace();
字符串值1=null;
字符串值2=null;
}
}
) {
//以下是参数将使用post方法添加到您的url
@凌驾
受保护的映射getParams(){
Map params=新的HashMap();
参数put(“应用程序”、“发送估值器”);
//参数put(“2ndParamName”、“valueoF2ndParam”);
返回参数;
}
};
newRequestQueue(this.add)(postRequest);
}

我还了解到,除了在屏幕上显示这些变量之外,我无法对这些变量执行其他操作,因此如果我想“玩”它们,我必须将它们存储在数据库中。这是真的吗?你的意思是什么?例如,如果我想将这些变量重新发送到其他php页面,我需要
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);
}
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);
        }