1

I have a php website that returns YES or NO upon request. For example

www.test.com/test.php?variable=test1 will return me YES and www.test.com/test.php?variable=test2 will return me NO

I am trying to get this response into an Objective-C application I am creating but I have no luck up to now. Here is my code

NSURLRequest *theRequest=[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.test.com/test.php?variable=test1"]
                                          cachePolicy:NSURLRequestUseProtocolCachePolicy
                                      timeoutInterval:60.0];
NSURLConnection *theConnection=[[NSURLConnection alloc] initWithRequest:theRequest delegate:self];
if (theConnection) {
    NSMutableData *receivedData = [NSMutableData data];
    NSString *strData = [[NSString alloc]initWithData:receivedData encoding:NSUTF8StringEncoding];
    NSLog(@"This is the response: %@", strData);
}else {
 }

Can anybody help me on that? is there any other way to do it? Am I doing something wrong?

Thanks a lot guys

1
  • When you say that you "have no luck up to now", what is happening? Crash, "This is the response: " never prints? Do you have the delegate set up to receive the data as it comes in? Commented Mar 10, 2012 at 22:12

2 Answers 2

2
     - (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
        {
            [receivedData appendData:data];
        // declre receivedData as a property of the class(in xxx.m)
            NSError *error=nil;
            NSDictionary *result=[NSJSONSerialization JSONObjectWithData:data options:
                                  NSJSONReadingMutableContainers error:&error];
            NSLog("%@", result);
    //do whatever you want...

           //the result dictionary is a JSON object and you can use it as KVC(key-value-coding) rules  
 }

If your app targets iOS 5 just use this code to your xxx.m file under the method you create the request. And if you want to use it in some other versions of iOS, Take a look JSON parser frameworks..

Sign up to request clarification or add additional context in comments.

2 Comments

can you please elaborate on that? Because I am trying to use it but I get a lot of errors
in your... .h file declare a property for receivedData and sysnthesyze it in .m.. And your receivedData is nsmutabledata, declare it as nsmutabledictionary(or just nes dictionary).
0

It seems you are setting up your connection correctly. However, you then have to catch the data in the NSURLConnection Delegate methods documented in the NSURLConnection.h file:

@protocol NSURLConnectionDataDelegate <NSURLConnectionDelegate>
@optional
- (NSURLRequest *)connection:(NSURLConnection *)connection willSendRequest:(NSURLRequest *)request redirectResponse:(NSURLResponse *)response;
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response;

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data;

- (NSInputStream *)connection:(NSURLConnection *)connection needNewBodyStream:(NSURLRequest *)request;
- (void)connection:(NSURLConnection *)connection   didSendBodyData:(NSInteger)bytesWritten
                                                 totalBytesWritten:(NSInteger)totalBytesWritten
                                         totalBytesExpectedToWrite:(NSInteger)totalBytesExpectedToWrite;

- (NSCachedURLResponse *)connection:(NSURLConnection *)connection willCacheResponse:(NSCachedURLResponse *)cachedResponse;

- (void)connectionDidFinishLoading:(NSURLConnection *)connection;
@end

Of particular interest are didReceiveData and connectionDidFinishLoading.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.