2

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]; 
+0

왜 AFNetworking을 사용하지 않습니까? –

+0

@ rajan-balan ASIHTTP를 계속 사용하는 이유는 앱이 백그라운드에있을 때 "setShouldContinueWhenAppEntersBackground"속성을 사용하여 작업 할 수 있기 때문입니다. 나는 AFNetworking으로도 시도했지만 너무 효과가 없었다. 내 질문을 코드로 업데이트했습니다. 확인해주십시오. –

답변

0

형식 - 여기에 코드입니다. 더 호환됩니다.

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

감사합니다. @mightyleader, 나는이 방법을 시도했습니다. 그러나 그것은 효과가 없었습니다. –

+0

아, 죄송합니다. ASIHTTP를 사용하는 이유가 있습니까? 블록 메서드를 사용하는 NSURLConnection의 최신 버전은 비동기 연결에 매우 적합합니다. Duh는 누군가에게 당신의 대답을 읽었습니다. 표준 방법을 사용하는 새로운 방법을 살펴볼 가치가 있습니다. – Cocoadelica

+0

해결책을 얻었습니다. 문제는 내가 요청 헤더를 요청에 설정하지 않았기 때문입니다. 요청에 다음 줄을 추가하면 문제가 해결됩니다. - NSMutableDictionary * requestheaders = [[NSMutableDictionary alloc] init]; [requestheaders setValue : @ "application/x-www-form-urlencoded; charset = utf-8"forKey : @ "Content-Type"]; [asiRequest setRequestHeaders : requestheaders]; –