我正在创建一个需要访问在线食谱数据库的食谱应用程序。
目前,我无法在另一端接收数据。但是,我已经验证了 NSURLRequest 是否登陆了正确的页面。
这是请求代码:
_ __ _ __.m 文件
NSURL *URL = [NSURL URLWithString:@"http://localhost/"];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
request = [NSMutableURLRequest requestWithURL:URL
cachePolicy:NSURLRequestUseProtocolCachePolicy
timeoutInterval:60.0];
[request setHTTPMethod:@"POST"];
NSURLConnection *connection= [[NSURLConnection alloc] initWithRequest:request delegate:self];
// store recipe
NSString *path = [[NSBundle mainBundle] pathForResource:@"Recipes" ofType:@"plist"];
NSMutableDictionary *dictionaryOfRecipes = [[NSMutableDictionary alloc] initWithContentsOfFile:path];
NSMutableArray *arrayOfRecipes = [dictionaryOfRecipes objectForKey:@"recipesArray"];
NSData *data = [[NSData alloc] init];
data = [arrayOfRecipes objectAtIndex:self.thisRecipeIndex];
// post data
[request setHTTPBody:data];
[connection start];
打印出正在发送的数据
(NSData *) $2 = 0x09071f40 {
ingredients = (
"penne pasta",
water
);
instructions = (
"put the penne in the water",
boil
);
recipeName = Penne;
}
_ ____.php 文件
<?php
// initialize connection to database
$db = mysql_connect('localhost', 'xxxxxxx', 'xxxxxxx');
$db_name = "project3";
mysql_select_db($db_name, $db);
// store incoming data as php variables
error_log(print_r($_POST, true));
$recipeName = $_POST['recipeName'];
// create mysql query
$query = "INSERT INTO recipeNamesTable (recipeName) VALUES '$recipeName'";
mysql_query($query);
mysql_close($db);
?>
我对可能存在的潜在问题有一些猜测,但不确定我是否正确。虽然[arrayOfRecipes objectAtIndex:self.thisRecipeIndex]
是NSDictionary
,但我将其存储为NSData
,尽管这不会返回任何错误,所以我保持这种方式。我是否需要将其存储为原样NSDictionary
然后将其转换为NSData
?如果这不是问题,我很想得到一些关于上面还有什么问题的反馈。