开发者

Cannot get HTTP GET request from IPhone application

I am writing a sample PHP Web Service that is sending GET Http request from the iphone to the web server. The server side code is returning JSON Data to iphone, the server side code looks like:

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

?>

Its a simple code that Fetches all the data from the user table and send it to the iphone application.

PHONE APPLICATION

- (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;
    }
}

The Problem

The problem is that nothing is coming on iphone side of the application. The response doesn't contain anything.


@user3171192, I think there would be problem is you does not passing UserID and Password with NSURLRequest to your web service.Therefore it could may be not give you response of the connection. Please use

In the portion of

-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
    [responseData appendData:data]; 
    NSLog(@"Response of web service %@", responseData);
}

You will get exact response of your code in GDB. I hope so you it will fixing your problem. Sorry for English.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜