ios - 响应为空

标签 ios objective-c

我的回复显示为空。但是当你在浏览器中输入 url 时它显示 1。但在我的代码中它返回 0。 网址是http://boomagift.ramansingla.com/forgotpassword.php?email=nihal@gmail.com 。如果有人能帮助我,我将不胜感激。我是 iOS 新手。

+(NSDictionary *)forgotpassword:(NSString *)email
{
    NSDictionary *dict=[[NSDictionary alloc]init];


    NSString *urlStr=[NSString stringWithFormat:@"http://boomagift.ramansingla.com/forgotpassword.php?email=%@",email];
    NSLog(@"%@",urlStr);

    NSMutableURLRequest *request=[[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:urlStr]];
    dict  =  [self sendRequest:request];
    NSLog(@"%@",dict);

    return dict;
}

+(NSDictionary *)sendRequest:(NSMutableURLRequest *)request
{
    NSHTTPURLResponse *response;
    NSError *error;
    NSData *responseData;
    responseData=[NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
    if(responseData&&[responseData length])
    {

       NSDictionary *dictionary=[NSJSONSerialization JSONObjectWithData:responseData options:0 error:&error];
        return dictionary;
    }
    else
    {
        UIAlertView *noInternetAlert=[[UIAlertView alloc] initWithTitle:@"Boom A GIft" message:@"Server Error" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
        [noInternetAlert show];
        noInternetAlert=nil;
        return nil;
    }
}

最佳答案

响应数据不是有效的 JSON,因为其顶部项目是数字,而不是集合(列表或对象)。

An object that may be converted to JSON must have the following properties:

  • The top level object is an NSArray or NSDictionary.

  • All objects are instances of NSString, NSNumber, NSArray, NSDictionary, or NSNull.

  • All dictionary keys are instances of NSString.

  • Numbers are not NaN or infinity.

您可以使用 options 参数将序列化程序配置为在顶层接受非集合对象。

enum {
  NSJSONReadingMutableContainers = (1UL << 0),
  NSJSONReadingMutableLeaves = (1UL << 1),
  NSJSONReadingAllowFragments = (1UL << 2)
};
typedef NSUInteger NSJSONReadingOptions;

NSJSONReadingAllowFragments

Specifies that the parser should allow top-level objects that are not an instance of NSArray or NSDictionary.

Available in OS X v10.7 and later.

顺便说一句:返回带有错误输出参数的nil(不是NULL)不是开玩笑,而是一个提示,问题出在哪里。 ;-)

关于ios - 响应为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29846648/

相关文章:

ios - 为什么包含图像的 launch.xib 没有出现在 ios 7 模拟器中,而在 ios 8 模拟器中完美运行

ios - 如何使用 Alamofire multipart 在参数中发送数组

ios - HLS 流不适用于 Apple 设备

ios - SpriteKit : Square to Circle animation using SKShapeNode UIBezierPath rounded corners

objective-c - NSTableView渲染 "Upside Down"

objective-c - 更改 UINavigationController View 和按钮(例如返回)中按钮的颜色?

iphone - 如果用自定义子类替换默认 View ,则 cornerRadius 不起作用

ios - 为什么 tableviewcell 返回一个可选的 ("")

ios - 如何在图片上写文字并保存

objective-c - 分配已分配的内存