0

JSON値をサーバーに「POST」し、jsonデータバックに応答したい。

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");
}];

出力は次のとおりです。成功は<>です。

または、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);
}];

応答コードは 200 です。これは接続は正しいことを意味しますが、それでも正しい結果は得られません。理由がわからない。どんな助けでも、事前に感謝します!

4

1 に答える 1

0

理由は、バックエンドが「GET」メソッドなのですが、「POST」したのですが、忘れていました:[操作開始]メソッド。

于 2014-01-15T03:34:15.607 に答える