How to call WebService json objectiveC iOS for calling function - ios

NSString *urlString = [NSString
stringWithFormat:#"http://192.168.1.15/abc/service.asmx?op=GetCenter"];
NSURL *url = [NSURL URLWithString:[urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
NSData *data = [[NSData alloc] initWithContentsOfURL:url];
NSError *error;
NSMutableArray *json =[NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error];
NSLog(#"%#",[json description]);
I have try this but it returns null value insted of call function.
Program ended with exit code: 9(lldb)
How to recognized if webserive is called or not or how call that function?

If you could provide a bit more information as to what you mean by the function is not called, that would be very helpful, is there any stack trace or other information shown when it crashes?
I would troubleshoot this by stepping through each line of code in the debugger to make sure you are getting data back and then as suggested to print out the error from the serialization function.
It also might be worth using these methods:
NSString* newStr = [NSString stringWithContentsOfURL:url encoding: NSUTF8StringEncoding error:&error];
or
NSString* newStr = [[NSString alloc] initWithData:theData encoding:NSUTF8StringEncoding];
to get a string from your URL or data once you have downloaded it and then print out or view the contents.
Longer term you might want to think about using something like AFNetworking to do your networking calls as what you have is synchronous and ideally, you should be doing all networking asynchronously.

Related

NSJSONSerialization Returns Error of NULL

I have the following code :
NSData * jsonData = [NSData dataWithContentsOfURL:location];
NSString* newStr = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
NSData *data = [newStr dataUsingEncoding:NSUTF8StringEncoding];
NSError *jsonError;
if (jsonError) {
NSLog(#"JSON Error %#", [jsonError localizedDescription]);
}
NSArray * jsonObject = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:&jsonError];
Which is parsing the following JSON String :
{"author":"Oli Riman","content":"The museum shows us a view of pre-WWI society that includes doubts, fears, political protests etc. through newspaper cartoons of the time. Really interesting for adults who enjoy history. I wouldn't suggest this for kids who haven't studied WWI history or who don't read easily.","rating":"5","placeId":"40","date":"29-June-2015","reviewId":"9905A52D-76B2-4D42-8CA8-9158225C0D07"}
However I am getting a strange error code of :
domain: (null) - code: 0
Can anyone advise on what is causing this ?
Just tested the code on my simulator. Its working. You need to check if you are getting data from server or not.
If you want to test the parsing thing, you can do one thing-
Just store the data in json file and save it in the app bundle lets say file is "data.json"
and call below method, you will get data for sure.
- (void)readJsonData {
NSString *path = [[NSBundle mainBundle] pathForResource:#"data" ofType:#"json"];
NSURL *url = [NSURL fileURLWithPath:path];
NSData *data = [NSData dataWithContentsOfURL:url];
NSError *error;
NSDictionary *dict = [NSJSONSerialization JSONObjectWithData: data
options: NSJSONReadingAllowFragments
error: &error];
NSLog(#"Parsed json data- %#", dict);
}

Sending NSMutableArray over custom url in objective c

I am trying to send an NSMutableArray using custom URL. Below is the code to do send:
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:tempArray options:NSJSONWritingPrettyPrinted error:nil];
NSString *jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
NSString *encodedString = (NSString *)CFBridgingRelease(CFURLCreateStringByAddingPercentEscapes(
NULL,
(CFStringRef)jsonString,
NULL,
(CFStringRef)#"<>!*'();:#&=+$,/?%#[]",
kCFStringEncodingUTF8 ));
I am able to send the encoded string in custom url and access it via [url query] followed by url decode. My problem is that I am able to get contents similar to what is in "jsonString" but afterwards I am not getting how to retrieve the array from it. Can someone guide to the right direction.
Thanks

Json crash when sending space and dot

I am trying to request a URL and I am getting error saying
"Data parameter is nil".
I had found that the variables used has got space dot(.). I think this is the problem that is being caused by the URL. So is there any way to send URL having space and dot without crashing?
NSURL *url = [[NSURL alloc]initWithString:[NSString stringWithFormat:#"192.168.1.5/mobileapp?/signin=%#&%#",username,password]];
NSError *errors;
NSData *data = [NSData dataWithContentsOfURL:url];
NSDictionary *json = (NSDictionary *)[NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&errors];
Try using NSUTF8StringEncoding
NSString *myUnencodedString = [NSString stringWithFormat:#"192.168.1.5/mobileapp?/signin=%#&%#",username,password]
NSString *encodedString = [myUnencodedString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSURL *myURL = [[NSURL alloc] initWithString:encodedString]

How to Cache NSDATA json in iOS

I am using this code to fetch application screenshots of an application from appStore using only appID,
NSString *numericIDStr = appID;
NSString *urlStr = [NSString stringWithFormat:#"http://itunes.apple.com/lookup?id=%#", numericIDStr];
NSURL *url = [NSURL URLWithString:urlStr];
NSData *json = [NSData dataWithContentsOfURL:url];
NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:json options:0 error:NULL];
NSArray *results = [dict objectForKey:#"results"];
NSDictionary *result = [results objectAtIndex:0];
myArray = [result objectForKey:#"screenshotUrls"];
I managed to cache screenshots by using SDWebImage and it works perfect, but I noticed that every time that I call this method it delays about 3-4 seconds, and on 3G its even more, about 10-15 seconds.
and other issue that my app crash when not connected to the internet, is there any way to kinda cache this method or something similar?
You can save your response(cache) using the following libraries on disk or in memory.
TMCache
or EGOCache
Use AFNetworking. It will take one line of code and do the caching for you.
[yourImage setImageWithURL:yourNSUrl];
That's it! It does all the caching for you.

iOS - How to get more debugging information on nil return values

In my iOS 5.1 application, I'm trying to load a JSON text file into a NSDictionary.
NSString *filepath = [[NSBundle mainBundle] pathForResource:#"stuff" ofType:#"json"];
NSDictionary *stuff = [NSDictionary dictionaryWithContentsOfFile:filepath];
The filepath is valid, the file exists, but stuff is nil, which means there was a problem decoding the file (according to the documentation).
(How) Can I get more debugging information on why [NSDictionary dictionaryWithContentsOfFile:] - or any kind of API call - returns nil?
Read the contents as NSData and then use +[NSPropertyListSerialization propertyListWithData:options:format:error:].
As you said: Your file contains a JSON string. First, you have to load the string and then you have to parse it using some JSON library.
E.g.:
NSError *error;
NSString *content = [NSString stringWithContentsOfFile:filepath encoding:NSUTF8StringEncoding error:&error]; //error checking omitted
SBJsonParser *parser = [[SBJsonParser alloc] init];
NSDictionary *json = [parser objectWithString: content];

Resources