2012-03-23 3 views
0

ではありません。サーバーにJSON値を「POST」し、jsonデータバッグに応答します。ASIHttpRequest POST JSONは正常ですが、AFNetworkingは

URL:http://solok.com:8080/soloo/phone/execute?content= {"method": "tet_123"、 "version"、 "1"}は、ブラウザで正しい値(JSON)を取得できます。

ASIHTTPRequest方法:

NSDictionary *postDic = [NSDictionary dictionaryWithObjectsAndKeys:@"tet_123",@"method",@"1",@"version",nil]; 
NSString *postString; 

//Then convert the "postDic" to NSString, the value is:{"method":"tet_123","version","1"} assign to postString; 
psotString = ...; 
ASIFormDataRequest *req=[ASIFormDataRequest requestWithURL:url]; 
[req setRequestMethod:@"POST"]; 
[req setPostValue:posStr forKey:@"content"]; 
[req startAsynchronous]; 
[req setDelegate:self]; 
[req setCompletionBlock:^{ 
    NSData *d = [req responseData]; 
    NSLog(@"respond is %@".d); 
} 

それはスムーズに動作します!しかし、AFNetworkdingではなく、コードはここにあります。

NSURL *url = [NSURL URLWithString:@"http://localhost:8080"]; 
AFHTTPClient *httpClient = [[AFHTTPClient alloc] initWithBaseURL:url]; 
[httpClient setParameterEncoding:AFJSONParameterEncoding]; 
NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:@"tet_123",@"method",@"1",@"version",nil]; 
NSDictionary *dic1 = [NSDictionary dictionaryWithObjectsAndKeys:dic,@"content", nil]; 
[httpClient postPath:@"/soloo/phone/execute" parameters:dic1 success:^(AFHTTPRequestOperation *operation, id responseObject) { 
    NSDictionary *d = (NSDictionary *)responseObject; 
    NSLog(@"success is %@",d); 
} failure:^(AFHTTPRequestOperation *operation, NSError *error) { 
    NSLog(@"fail"); 
}]; 

出力は、成功は<>です。

またはI AFNetworkingの別の方法を使用する:

NSMutableURLRequest *request = [httpClient requestWithMethod:@"POST"path:@"/soloo/phone/execute" parameters:dic1]; 
AFJSONRequestOperation *ope = [AFJSONRequestOperation JSONRequestOperationWithRequest:request success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) { 
    NSLog(@"response %d",response.statusCode); 
} failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON) { 
    NSLog(@"fail%d JSON %@",response.statusCode,JSON); 
}]; 

応答コードは、接続が正確であるが、それでも正しい結果がNOを意味し、200です。 理由がわかりません。どのような助け、事前に感謝!

答えて

0

なぜなら、バックエンドは「GET」メソッドですが、私は「POST」しましたが、一方、私は[Operation start]メソッドを忘れていました。

関連する問題