我曾经有一个方法来发布:
-(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。第一个工作正常,但第二个没有。我的错在哪里?
谢谢