2

URL に 2 つの JSON 辞書を送信しています。これは NSURLRequest では機能しますが、ASIFormDataRequest では機能しません。ASIFormDataRequest で同じリクエストを送信すると、次のエラーが表示されます -

{"Response":"Error","Code":"400","Detail":"form.request の JSON を解析できません"}

以下は、私が 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];
4

1 に答える 1

0

送信するデータをNSDictionaryのキー/値ペアにフォーマットしてから、NSJSONSerializationを使用して送信する必要のあるNSDataオブジェクトを作成します。より互換性があります。

NSDictionary *jsonDict = @{key:value,key:value};
NSData *requestData = [NSJSONSerialisation dataWithJSONObject:jsonDict options:NSJSONWritingPrettyPrinted error:error];
于 2013-03-20T09:15:52.953 に答える