url에 두 개의 JSON 사전을 보냅니다. 이것은 NSURLRequest와 함께 작동하지만 ASIFormDataRequest에서는 작동하지 않습니다. ASIFormDataRequest와 같은 요청을 보낼 때 다음과 같은 오류 표시 -JSON 요청 NSURLRequest로 작업하지만 ASIFormDataRequest로 작업하지 않습니다.
{ "응답": "오류", "코드": "400", "세부": " form.request에서 JSON을 구문 분석 할 수 없습니다
NSString *request=[NSString stringWithFormat:@"REQUEST={\"request\":{\"api_username\":\"%@\", \"api_password\":\"%@\",\"method\":\"%@\"},",@"iphone",@"xlq229320#",@"getUser"]; NSString *methodParameter=[NSString stringWithFormat:@"\"methodparam\":{\"email\":\"%@\",\"password\":\"%@\"}}",@"[email protected]",@"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];
내가 잘못하고있는 중이 야 어디 이해할 수 없었다 - "}
아래는 내가 ASIFormDataRequest으로 사용하고있는 코드입니다. 아무에게도 이것에 대한 단서가 있다면 알려주십시오. 감사합니다.
AFNetworking을 사용해 보았습니다.하지만 너무 효과가 없었습니다. 당신이 다음을 NSData을 만들 NSJSONSerialization를 사용있는 NSDictionary에 키/값 쌍으로 보낼 데이터를 전송할 필요가 오브젝트
NSDictionary *dict1 = [[NSDictionary alloc]initWithObjectsAndKeys:@"iphone",@"api_username",@"xlq229320#",@"api_password",@"getUser",@"method", nil];
NSDictionary *dict2 = [[NSDictionary alloc]initWithObjectsAndKeys:@"[email protected]",@"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으로도 시도했지만 너무 효과가 없었다. 내 질문을 코드로 업데이트했습니다. 확인해주십시오. –