2013-12-22 43 views
0

我不確定如何嘗試爲每個應用用戶拉特定請求,例如每次爲每個應用用戶顯示一個單獨的好友列表。我在NSURL中嘗試了POST/GET請求,但沒有運氣。如何根據登錄用戶指定加載用戶參數以加載不同的表格?目前,表格視圖中沒有返回任何內容。如果您需要更多信息,請與我們聯繫。UITableView每用戶拉特定請求

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

} 
?> 

回答

0

我是這樣做的:

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

NSURL *url = [NSURL URLWithString:userParam];