在我的应用程序中,用户可以发布某些内容,而其他人可以对其进行评论。无论如何,要查看帖子,您会在桌面视图上看到它的标题,而不是推送到详细视图。那个segue带有帖子ID。要显示注释,我必须将post id发送到php文件。然后我根据post id从数据库中提取数据并在json数组中回显它,这样客观c就可以读取并显示它。我的问题是我永远不会回应数据,因为post id没有被转移到php文件或其他事情发生。
我在其他文件中使用相同的目标c代码发送数据并将数据插入数据库,它工作正常,所以我不确定是什么问题。
这是我的目标c:
-(void) getData:(NSData *) data{
NSError *error;
json = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error];
}
-(void) start{
NSMutableString *postString = [NSMutableString stringWithString:kRecieveUrl];
[postString appendString:[NSString stringWithFormat:@"?%@=%@", kId, _post_id]];
[postString setString:[postString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:postString]];
[request setHTTPMethod:@"POST"];
postConnection = [[NSURLConnection alloc] initWithRequest:request delegate:self startImmediately:YES];
NSURL *url = [NSURL URLWithString:kRecieveUrl];
NSData *data = [NSData dataWithContentsOfURL:url];
[self getData:data];
}
我的php:
<?php
$db_connect = mysql_connect("localhost", "root", "")
or die("Our servers are down at the moment");
mysql_select_db("My DB") or die("Couldnt find db");
$post_id = $_GET['id'];
$query = "SELECT * FROM Comments WHERE post_id='$post_id'";
$results = mysql_query($query);
$num = mysql_numrows($results);
mysql_close();
$rows = array();
while($r = mysql_fetch_assoc($results))
{
$rows[] = $r;
}
echo json_encode($rows);
?>
答案 0 :(得分:1)
尝试这样的事情
- (void)start {
NSMutableString *postString = [NSMutableString stringWithString:RecieveUrl];
[postString appendString:[NSString stringWithFormat:@"?%@=%@", kId, _post_id]];
[postString setString:[postString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
NSURLRequest *request = [[NSURLRequest alloc] initWithURL:[NSURL URLWithString:postString]];
[NSURLConnection sendAsynchronousRequest:request
queue:[NSOperationQueue currentQueue]
completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {
if (error) {
// do error handling
} else {
id json = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error];
if (error) {
// do error handling
} else {
// do something with json
}
}
}];
}
答案 1 :(得分:0)
您正在使用GET方法,因此只需删除行[request setHTTPMethod:@"POST"];
,否则您必须传递包含POST数据的字典