如何通过意图发送从另一个活动发送的数据?

Before listing out the items, I have a function of the app to create a new data where it includes a unique id from another activity. The unique data is "sid". I used PHP to connect to a phpmyadmin database.

<?php

// array for JSON response
$response = array();

// check for required fields
if (isset($_POST['foodName']) && isset($_POST['foodPrice']) && isset($_POST['foodType']) && isset($_POST['sid']) {

$foodName = $_POST['foodName'];
$foodPrice = $_POST['foodPrice'];
$foodType = $_POST['foodType'];
$sid = $_POST['sid'];

// include db connect class
require_once __DIR__ . '/db_connect.php';

// connecting to db
$db = new DB_CONNECT();

// mysql inserting a new row
$result = mysql_query("INSERT INTO food(foodName, foodPrice, foodType, sid) VALUES('$foodName', '$foodPrice', '$foodType', '$sid')");

// check if row inserted or not
if ($result) {
    // successfully inserted into database
    $response["success"] = 1;
    $response["message"] = "Food successfully created.";

    // echoing JSON response
    echo json_encode($response);
} else {
    // failed to insert row
    $response["success"] = 0;
    $response["message"] = "Oops! An error occurred.";

    // echoing JSON response
    echo json_encode($response);
}
} else {
// required field is missing
$response["success"] = 0;
$response["message"] = "Required field(s) is missing";

// echoing JSON response
echo json_encode($response);
}
?>

And this is the intent to receive the data from the other activity :

Intent i = getIntent();

    // getting product id (sid) from intent
    sid = i.getStringExtra(TAG_SID);

Lastly, I have the this in the class when it is executed :

protected String doInBackground(String... args) {

        // Building Parameters
        List<NameValuePair> params = new ArrayList<NameValuePair>();
        params.add(new BasicNameValuePair("foodName", getFoodName));
        params.add(new BasicNameValuePair("foodPrice", getFoodPrice));
        params.add(new BasicNameValuePair("foodType", getFoodType));
        params.add(new BasicNameValuePair("sid", sid));

Is there something that I did wrong somewhere? On my table I have this "sid" too.

1个回答



您必须执行以下操作
在第一个活动</ p>

  Intent intent = 第二个活动中的新Intent(getBaseContext(),SignoutActivity.class); 
intent.putExtra(“sid”,VALUE);
startActivity(intent);
</ code> </ pre>

</ p>

  Bundle extras = getIntent()。getExtras(); 
if(extras!= null){
String value = extras.getString(“sid”);
}
</ code> </ pre>
</ div>

展开原文

原文

you have to do the following in the first Activity

Intent intent = new Intent(getBaseContext(), SignoutActivity.class);
intent.putExtra("sid", VALUE);
startActivity(intent);

in second activity

Bundle extras = getIntent().getExtras();
if (extras != null) {
    String value = extras.getString("sid");
}

douji5746
douji5746 if条件只是额外检查,有时您需要调用一个活动而不添加任何带有intent的数据。
大约 6 年之前 回复
drsfgwuw61488
drsfgwuw61488 谢谢它的作品!
大约 6 年之前 回复
duanjianlu0506
duanjianlu0506 如果你知道你是一个额外的String,你可以直接使用String extra = getIntent()。getExtraS string(“sid”);
大约 6 年之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐