【问题标题】:JSON Request Working with NSURLRequest but not with ASIFormDataRequestJSON 请求使用 NSURLRequest 但不使用 ASIFormDataRequest
【发布时间】:2013-03-09 04:44:09
【问题描述】:

我将两个 JSON 字典发送到一个 url。这适用于 NSURLRequest,但不适用于 ASIFormDataRequest。当使用 ASIFormDataRequest 发送相同的请求时,它会显示以下错误 -

{"Response":"Error","Code":"400","Detail":"无法解析 JSON form.request"}

下面是我与 ASIFormDataRequest 一起使用的代码 -

NSString *request=[NSString stringWithFormat:@"REQUEST={\"request\":{\"api_username\":\"%@\", \"api_password\":\"%@\",\"method\":\"%@\"},",@"iphone",@"xlq229320#",@"getUser"];
NSString *methodParameter=[NSString stringWithFormat:@"\"methodparam\":{\"email\":\"%@\",\"password\":\"%@\"}}",@"ganesh@gmail.com",@"ganesh"];
NSString *finalRequest=[request stringByAppendingString:methodParameter];

NSMutableData* requestData = [NSMutableData dataWithBytes: [finalRequest UTF8String] length: [finalRequest length]];

ASIFormDataRequest *asiRequest = [ASIFormDataRequest requestWithURL:[NSURL URLWithString:urlForClinic]];

[asiRequest setTimeOutSeconds:60.0];
[asiRequest setPostBody:requestData];
[asiRequest setDelegate:self];
[asiRequest setRequestMethod:@"POST"];
[asiRequest setDidFailSelector:@selector(requestFailed:)];
[asiRequest setDidFinishSelector:@selector(requestFinished:)];
[asiRequest startAsynchronous];

我不明白我在哪里做错了。如果有人对此有任何线索,请告诉我。非常感谢。

我什至用 AFNetworking 尝试过,但也没有用。这是代码 -

NSDictionary *dict1 = [[NSDictionary alloc]initWithObjectsAndKeys:@"iphone",@"api_username",@"xlq229320#",@"api_password",@"getUser",@"method", nil];
NSDictionary *dict2 = [[NSDictionary alloc]initWithObjectsAndKeys:@"ganesh@gmail.com",@"email",@"ganesh",@"password",nil];
NSMutableDictionary *req = [[NSMutableDictionary alloc]initWithObjectsAndKeys:dict1,@"request",dict2,@"methodparam",nil];

NSData *jsonData = [NSJSONSerialization dataWithJSONObject:req options:NSJSONWritingPrettyPrinted error:nil];
NSString *jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];    
NSData *requestData = [NSData dataWithBytes:[jsonString UTF8String] length:[jsonString length]];

NSMutableURLRequest *request=[[NSMutableURLRequest alloc]initWithURL:[NSURL URLWithString:urlForClinic] cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:30.0];
[request setHTTPMethod:@"POST"];
[request setHTTPBody:requestData];

AFJSONRequestOperation *operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) {
  NSLog(@"Public Timeline: %@ %@", JSON, response);
} failure:nil];

[operation start];

【问题讨论】:

  • 你为什么不使用AFNetworking?
  • @rajan-balan 我坚持使用 ASIHTTP 的原因是因为它也能够在应用程序处于后台时工作,使用“setShouldContinueWhenAppEntersBackground”属性。我也尝试过使用 AFNetworking,但它也没有奏效。我已经用代码更新了我的问题。请检查。

标签: objective-c json asihttprequest afnetworking asiformdatarequest


【解决方案1】:

将您要发送的数据格式化为 NSDictionary 中的键/值对,然后使用 NSJSONSerialization 创建您需要发送的 NSData 对象。 会更兼容。

NSDictionary *jsonDict = @{key:value,key:value};
NSData *requestData = [NSJSONSerialisation dataWithJSONObject:jsonDict options:NSJSONWritingPrettyPrinted error:error];

【讨论】:

  • 谢谢@mightyleader,我也试过这种方式。但它没有用。
  • 啊,很遗憾听到这个消息。您使用 ASIHTTP 的东西是有原因的吗?带有块方法的较新版本的 NSURLConnection 非常适合异步连接。 Duh刚刚阅读了您对其他人评论的回答。仍然可能值得一试使用标准方法的新方法。
  • 得到了解决方案。问题是因为我没有为请求设置任何请求标头。将以下行添加到请求中解决了问题 - NSMutableDictionary *requestheaders =[[NSMutableDictionary alloc]init]; [requestheaders setValue:@"application/x-www-form-urlencoded; charset=utf-8" forKey:@"Content-Type"]; [asiRequest setRequestHeaders:requestheaders];
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-05-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-02-13
  • 1970-01-01
相关资源
最近更新 更多