Reputation: 85
I am new to objective c and cocoa framework. i have made following code and send request to php from cocoa but cannot get response from php /server please help.
NSString *fname=@"nam1";
NSString *lname=@"nam2";
NSString *urlString = [[NSString alloc] initWithFormat:@"http://url/send.php?
first_name=%@&last_name=%@",fname,lname];
NSLog(urlString);
NSURL *url = [ NSURL URLWithString:urlString];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
NSAlert *dq= [[NSAlert alloc] init];
[dq setMessageText:urlString];
[dq runModal];
[request setHTTPMethod:@"GET"];
NSURLConnection* connection = [[NSURLConnection alloc] initWithRequest:request
delegate:self];
[connection start];
NSAlert *d= [[NSAlert alloc] init];
[d setMessageText:@"connection"];
[d runModal];
And for response the following code doesn't work and i don't receive response.
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse
*)response
{
_responseData = [[NSMutableData alloc] init];
}
-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
// Append the new data to the instance variable
[_responseData appendData:data];
NSLog(@"response string: %@", _responseData);
}
And in response to this NSLog(@"response string: %@", _responseData); response string: efbbbf3c 3f786d6c 20766572 73696f6e 3d22312e 30222065 6e636f64 696e673d 22757466 2d382220 3f3e0a3c 41727261 794f6646 69726554 65787441 70694d73 673e0a3c 2f417272 61794f66 46697265 54657874 4170694d 73673e appears in console.
Upvotes: 0
Views: 507
Reputation: 437582
If you simply want to examine the NSData
as a NSString
, you could put the following in the connectionDidFinishLoading:
method:
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
NSString *string = [[NSString alloc] initWithData:_responseData encoding:NSUTF8StringEncoding];
NSLog(@"_responseData string = %@", string);
}
Having said that, the response would appear to be XML:
<?xml version="1.0" encoding="utf-8" ?>
<ArrayOfFireTextApiMsg>
</ArrayOfFireTextApiMsg>
Is that what you were expecting? If so, you can use NSXMLParser
to parse that.
For example, let's assume you had an XML response with some data in it:
<?xml version="1.0" encoding="utf-8" ?>
<ArrayOfFireTextApiMsg>
<FireTextApiMsg>Message1</FireTextApiMsg>
<FireTextApiMsg>Message2</FireTextApiMsg>
<FireTextApiMsg>Message3</FireTextApiMsg>
</ArrayOfFireTextApiMsg>
In connectionDidFinishLoading
, you can do something like:
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
NSXMLParser *parser = [[NSXMLParser alloc] initWithData:_responseData];
[parser setDelegate:self];
if ([parser parse])
NSLog(@"fireTextApiMsgs = %@", self.fireTextApiMsgs);
else
NSLog(@"parse failed");
}
Clearly, you'd want to declare your class to conform to the delegate protocol and define some properties to keep track of the values you parse:
@interface ViewController () <NSXMLParserDelegate>
@property (nonatomic, strong) NSMutableArray *fireTextApiMsgs;
@property (nonatomic, strong) NSMutableString *currentValue;
@end
And you'd implement the NSXMLParserDelegate
methods, e.g.:
#pragma mark - NSXMLParserDelegate
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict
{
if ([elementName isEqualToString:@"ArrayOfFireTextApiMsg"])
self.fireTextApiMsgs = [NSMutableArray array];
else if ([elementName isEqualToString:@"FireTextApiMsg"])
self.currentValue = [NSMutableString string];
}
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
{
[self.currentValue appendString:string];
}
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
{
if ([elementName isEqualToString:@"FireTextApiMsg"]) {
[self.fireTextApiMsgs addObject:self.currentValue];
self.currentValue = nil;
}
}
- (void)parser:(NSXMLParser *)parser parseErrorOccurred:(NSError *)parseError
{
NSLog(@"%s: %@", __FUNCTION__, parseError);
}
That will generate output like:
2013-12-20 12:08:55.167 XMLTestApp[12531:70b] fireTextApiMsgs = ( Message1, Message2, Message3 )
Clearly, your final XML will undoubtedly be different (I just made up an example), so you'll have to tweak the code according to what your XML response looks like and what you're trying to parse from it, but hopefully this illustrates the idea.
Upvotes: 1
Reputation: 615
make new method:
-(void)connectionDidFinishLoading:(NSURLConnection *)connection {
NSError* error;
NSDictionary* json = [NSJSONSerialization _responceData options:kNilOptions error:&error];
if (error != nil) {
NSLog(@"ERROR!");
}
NSLog(@"response string: %@", _responseData);
}
NSData simply stores a buffer of bytes, so when you simply make:"nslog(@"%@", data), you get a bunchf of bytes. So you need to translate this to json, because often responses are json objects. So for this we use native library "NSJSONSerialization", that helps us to translate NSData to NSDictionary with a couple of options. If it's error, you will see this in log.
Some words about didReceiveResponse and didReceiveData methods. When first is worked, you understand, that you get response from server, you know it's alive and connection between client and server are alive. When second worked, that a couple of data added to your nsmutabledata parameter, so data came from server, you added this to your property.
And final method called connectionDidFinishLoading in which you can free connected with your data.
Upvotes: 0