以前は投稿する方法がありました:
-(void) execMethod:(NSString*)methodName andParams:(NSArray*)parameters withID:(NSString*)identificator {
//RPC
NSMutableDictionary* reqDict = [NSMutableDictionary dictionary];
[reqDict setObject:methodName forKey:@"method"];
[reqDict setObject:parameters forKey:@"params"];
[reqDict setObject:identificator forKey:@"id"];
//RPC JSON
NSString* reqString = [NSString stringWithString:[reqDict JSONRepresentation]];
NSData* requestData = [NSData dataWithBytes:[reqString UTF8String] length:[reqString length]];
//Request
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:url];
//prepare http body
[request setHTTPMethod: @"POST"];
[request setValue:[NSString stringWithFormat:@"%d", [requestData length]] forHTTPHeaderField:@"Content-Length"];
[request setValue:@"application/json" forHTTPHeaderField:@"Accept"];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
[request setHTTPBody: requestData];
if (urlConnection != nil) {
[urlConnection release];
urlConnection = nil;
}
urlConnection = [[NSURLConnection alloc] initWithRequest:request delegate:self startImmediately:YES];
[request release];
}
今、私は ASIHttp を使用したいので、その関数を書きました:
-(void)startRequest:(NSString*)methodName andParams:(NSArray*)parameters withID:(NSString*)identificator
{
NSMutableDictionary* reqDict = [NSMutableDictionary dictionary];
[reqDict setObject:methodName forKey:@"method"];
[reqDict setObject:parameters forKey:@"params"];
[reqDict setObject:identificator forKey:@"id"];
//RPC JSON
NSString* reqString = [NSString stringWithString:[reqDict JSONRepresentation]];
//Request
NSData* requestData = [NSData dataWithBytes:[reqString UTF8String] length:[reqString length]];
asiRequest=[ASIFormDataRequest requestWithURL:url];
[asiRequest addRequestHeader:@"Accept" value:@"application/json"];
[asiRequest addRequestHeader:@"Content-Length" value:[NSString stringWithFormat:@"%d", [requestData length]]];
[asiRequest addRequestHeader:@"Content-Type" value:@"application/x-www-form-urlencoded"];
[asiRequest appendPostData:requestData];
[asiRequest setDelegate:self];
[asiRequest startAsynchronous];
}
しかし、それは同じように動作しません.jsonを返す必要があります. 最初のものは正しく機能しますが、2 つ目は機能しません。私のせいはどこですか?
ありがとう