dousi4950 2014-10-16 14:51
浏览 67
已采纳

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

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条回答 默认 最新

  • doubiankang2845 2014-10-16 14:58
    关注

    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");
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥30 求一段fortran代码用IVF编译运行的结果
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 lammps拉伸应力应变曲线分析
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥15 请问Lammps做复合材料拉伸模拟,应力应变曲线问题
  • ¥30 python代码,帮调试,帮帮忙吧
  • ¥15 #MATLAB仿真#车辆换道路径规划