我的回复显示为空。但是当你在浏览器中输入 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
orNSDictionary
.All objects are instances of
NSString
,NSNumber
,NSArray
,NSDictionary
, orNSNull
.All dictionary keys are instances of
NSString
.
Numbers
are notNaN
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
orNSDictionary
.Available in OS X v10.7 and later.
顺便说一句:返回带有错误输出参数的nil
(不是NULL
)不是开玩笑,而是一个提示,问题出在哪里。 ;-)
关于ios - 响应为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29846648/