Well, the problem is that you are parsing your request data instead of the response data.
Try this instead:
id jsonObject = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:Nil];
質問
[For testing purpose I have hardcoded the URL which should return YES. I will get the scanned URL later]
How to extract the projectID I get in log and check the condition.
Here's what I coded
NSError *error;
NSOperationQueue *queue = [[NSOperationQueue alloc] init];
NSDictionary *dictionary = [NSDictionary dictionaryWithObjectsAndKeys: @"test@yahoo.com" ,@"EmailID", @"http://www.yahoo.com", @"URL", @"", @"Phone", @"", @"UserID", nil];
NSURL *url = [NSURL URLWithString:@"http:// some url"];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:url];
NSData *requestData = [NSJSONSerialization dataWithJSONObject:dictionary options:kNilOptions error:&error];
[request setHTTPMethod:@"POST"];
[request setValue:@"application/json" forHTTPHeaderField:@"Accept"];
[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[request setValue:[NSString stringWithFormat:@"%d", [requestData length]] forHTTPHeaderField:@"Content-Length"];
[request setHTTPBody: requestData];
[request setTimeoutInterval:180];
NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
[connection start];
[NSURLConnection sendAsynchronousRequest:request queue:queue completionHandler:^(NSURLResponse *response, NSData *data, NSError *error){
if(error || !data)
{
NSLog(@"JSON NOT posted");
}
else
{
id jsonObject = [NSJSONSerialization JSONObjectWithData:requestData options:NSJSONReadingAllowFragments error:Nil];
if([jsonObject respondsToSelector:@selector(objectForKey:)])
{
NSDictionary *dictionaryForUserID = [jsonObject valueForKey:@"ProjID"];
NSLog(@" Project Id = %@", dictionaryForUserID);
/* I get "Project ID = (null)" here whereas I get one positive value in the log. My question is how to get the projectID in log? */
/* log output ::
2013-08-20 11:57:34.765 appname[585:5903] Project Id = (null)
2013-08-20 11:57:34.766 appname[585:5903] JSON data posted!
2013-08-20 11:57:34.769 appname[585:c07] Data:
{"ProjID":"5","ProjName":null,"URL":null,"EmailID":null,"Phone":null,"userId":null}{"ProjID":"5","ProjName":null,"URL":null,"EmailID":null,"Phone":null,"userId":null}
*/
}
NSLog(@"JSON data posted!");
}
}];
解決
Well, the problem is that you are parsing your request data instead of the response data.
Try this instead:
id jsonObject = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:Nil];
他のヒント
Take a close look at your code. You are parsing requestData
, not the data
that's received from the server. Good Luck!