0
我正在編寫一個示例PHP Web服務,它正在向iPhone發送GET Http請求到Web服務器。服務器端代碼返回JSON數據到iPhone,服務器端的代碼如下所示:無法從iPhone應用程序獲取HTTP GET請求
<?php
function getUsers(){
$con=mysql_connect("localhost","root","123456") or die(mysql_error());
if(!mysql_select_db("eventsfast",$con))
{
echo "Unable to connect to DB";
exit;
}
$sql="SELECT * from users";
$result=mysql_query($sql);
if(!$result)
{
die('Could not successfully run query Error:'.mysql_error());
}
$data = array();
while($row = mysql_fetch_assoc($result)){
$data['username'][] = $row['username'];
$data['password'][]= $row['password'];
}
mysql_close($con);
//print_r(json_encode($data));
//return (json_encode($data));
return json_encode('success');
}
getUsers();
?>
它是一個簡單的代碼,從用戶表中獲取的所有數據,並將其發送到iPhone應用程序。
電話應用程序
- (void)viewDidLoad {
[super viewDidLoad];
responseData = [[NSMutableData data] retain];
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://localhost:8888/GetData.php"]];
[[NSURLConnection alloc] initWithRequest:request delegate:self];
}
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
[responseData setLength:0];
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
[responseData appendData:data];
}
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
label.text = [NSString stringWithFormat:@"Connection failed: %@", [error description]];
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
[connection release];
NSString *responseString = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
[responseData release];
NSError *error;
SBJSON *json = [[SBJSON new] autorelease];
NSArray *luckyNumbers = [json objectWithString:responseString error:&error];
[responseString release];
if (luckyNumbers == nil)
label.text = [NSString stringWithFormat:@"JSON parsing failed: %@", [error localizedDescription]];
else {
NSMutableString *text = [NSMutableString stringWithString:@"Lucky numbers:\n"];
for (int i = 0; i < [luckyNumbers count]; i++)
[text appendFormat:@"%@\n", [luckyNumbers objectAtIndex:i]];
NSLog(text);
label.text = text;
}
}
的問題
的問題是,什麼是對應用程序的iPhone那邊傳來。該響應不包含任何內容。
你能修正代碼清單1.固定間距,以便代碼格式正確,並且2.只包括與你的問題相關的班級部分? – 2010-04-22 07:29:00
我認爲你的web服務需要你還沒有通過的USERID和PASSWORD。 – Tirth 2010-04-22 07:48:10