Unable to read JSON from service

I am implementing a forgetting password service in which I would pass an email address and it will return JSON to find out about the sent email. The problem is that I cannot read the response line in json, and my exception message shows that the data parameter is zero, but if I look at the URL in my web browser, the service looks fine as below. my code is:

NSURL *url = [LokalmotionCommonTask getURLForgotPassword:emailFieldTxt.text];

    NSData* data = [NSData dataWithContentsOfURL: url];

    @try {
        NSError* error;
        NSDictionary* jsonDict = [NSJSONSerialization 
                                  JSONObjectWithData:data //1
                                  options:0 
                                  error:&error];

        NSLog(@"%@", [jsonDict objectForKey:@"response"]);
    }
    @catch (NSException *exception) {
        NSLog(@"forgot password exception: %@: %@", [exception name], [exception reason]);
    }

and the response of the service that I get in my web browser is as follows:

{"status":400,"response":"Your request to change password is already sent. Please check your email."}

An exception:

forgot password exception: NSInvalidArgumentException: data parameter is nil
+3
source share
4 answers

in Objective-C, exceptions are used only for fatal errors, and not for recoverable errors. Instead, just check the nil data:

, , :

NSError* error;
NSURL *url = [LokalmotionCommonTask getURLForgotPassword:emailFieldTxt.text];
NSData* data = [NSData dataWithContentsOfURL: url options:NULL error:&error];
if (data) {
    NSDictionary* jsonDict = [NSJSONSerialization 
                              JSONObjectWithData:data //1
                              options:0 
                              error:&error];
    NSLog(@"%@", [jsonDict objectForKey:@"response"]);
}
else {
    NSLog(@"forgot password error, %@", [error localizedFailureReason]);
}

: getURLForgotPassword:
, "get", , . : forgotPasswordURL:

, get Jave.

+3

data parameter is nil. , , JSON, , nil.
[NSData dataWithContentsOfURL:url], , nil . .

, , , , .

+1

i , , url, url @ "http:...." , url @ "https:...."

+1

It was decided that by setting the service url my url was not encoded and had inline spaces

0
source

All Articles