2014-10-16 74 views
0

在列出项目之前,我有一个应用程序的功能来创建一个新数据,其中包含来自其他活动的唯一ID。唯一的数据是“sid”。我用PHP连接到一个phpmyadmin数据库。如何通过意向发送从其他活动发送的数据?

<?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); 
} 
?> 

这是从其他活动接收数据的意图:

Intent i = getIntent(); 

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

最后,我有这样的类被执行时:

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)); 

是有什么我在某个地方做错了?在我的桌子上我也有这个“sid”。

回答

0

你必须做到以下几点 中的第一个活动

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

在第二活动

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

如果你知道你得到一个String作为一个额外的,您可以直接使用'字符串extra = getIntent()。getExtraString(“sid”);' – Snow 2014-10-16 15:01:12

+0

感谢它的工作! – danco91 2014-10-16 15:16:55

+0

如果条件只是额外的检查,有时你需要调用一个活动而不添加任何意图的数据。 – 2014-10-16 15:24:31

相关问题