UITableView为每个用户提取特定请求

时间:2013-12-22 20:00:34

标签: php ios json uitableview post

我不确定如何尝试为每个应用用户提取特定请求,例如每次为每个应用用户显示一个单独的好友列表。我在NSURL尝试过POST / GET请求,但没有运气。如何在加载时指定用户参数以根据登录用户加载不同的表?目前,表视图中没有返回任何内容。如果您需要更多信息,请与我们联系。

NSURL *url = [NSURL URLWithString:@"https://***/friendsList.php"];

    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60];

    NSURLConnection *conneection = [[NSURLConnection alloc]initWithRequest:request delegate:self];

    [request setHTTPMethod:@"GET"];

    NSString *postString = savedUser;

    [request setHTTPBody:[postString dataUsingEncoding:NSUTF8StringEncoding]];

    //NSURLRequest *request = [NSURLRequest requestWithURL:url];

    //[[NSURLConnection alloc] initWithRequest:request delegate:self];
}

-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
    data = [[NSMutableData alloc]init];
}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)theData
{
    [data appendData:theData];
}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
    [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;

    self.friendsList = [NSJSONSerialization JSONObjectWithData: data options:kNilOptions error: nil];

    [friendsListTable reloadData];
}

PHP:

<?php

    header('Content-type: application/json');

    $username = 's***'; 
    $password = '***'; 
    $host = 'localhost'; 
    $dbname = '***'; 

    $link = mysql_connect('localhost', $username, $password);

if (!$link) 
{
    die('Could not connect: ' . mysql_error());
}
else
{

$appUser = $_POST['username'];

// make foo the current db
$db_selected = mysql_select_db($dbname, $link);
if (!$db_selected) {
    die ('Can\'t login to shipstudent : ' . mysql_error());
}

$get_news = mysql_query("Select username, first_name, last_name from Users u inner join Friends f on f.friendid = u.iduser where u.username = '$appUser'", $link);
$articles = array();

while($row = mysql_fetch_assoc($get_news))
{   
    $articles [] = $row;
}

echo json_encode($articles);

}
?>

1 个答案:

答案 0 :(得分:0)

我是这样做的:

NSString *userParam = [NSString stringWithFormat:@"https://***.php?username=%@",savedUser];

NSURL *url = [NSURL URLWithString:userParam];