1

AFNetworkingを使用してサーバーにPOSTリクエストを送信しようとしていますが、すべてが機能しているようです。つまり、アプリケーションがサーバーに正常にpingを実行しています。ただし、デバッガーを使用して以下のコードをステップ実行した後、値が正常に渡されたように見えても、サーバーに到達すると、送信されるパラメーター値は空白になります。これに関する助けをいただければ幸いです。

APIClient.m

#import "APIClient.h"
#import "AFJSONRequestOperation.h"

// Removed URL for privacy purposes.
static NSString * const kAPIBaseURLString = @"string goes here";

@implementation APIClient

+ (APIClient *)sharedClient {
    static APIClient *_sharedClient;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        _sharedClient = [[APIClient alloc] initWithBaseURL:[NSURL URLWithString:kAPIBaseURLString]];
    });

    return _sharedClient;
}

- (id)initWithBaseURL:(NSURL *)url {
    self = [super initWithBaseURL:url];
    if (self) {
        [self registerHTTPOperationClass:[AFJSONRequestOperation class]];

        // Accept HTTP Header; see http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.1
        [self setDefaultHeader:@"Accept" value:@"application/json"];
    }

    return self;
}

@end

LoginBrain.mのログイン方法

- (void)loginUsingEmail:(NSString *)email andPassword:(NSString *)password withBlock:(void (^)(NSDictionary *loginResults))block {
    self.email = email;
    self.password = password;

    // Removed path for privacy purposes
    [[APIClient sharedClient] postPath:@"insert path here" parameters:[NSDictionary dictionaryWithObjectsAndKeys:email, @"uname", password, @"pw", nil] success:^(AFHTTPRequestOperation *operation, id responseJSON) {
        if (block) {
            block(responseJSON);
        }
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"Error: %@", error);

        if (block) {
            block(nil);
        }
    }];

    // Store user data in app?
}

LoginViewController.mのLoginCalledメソッド

- (IBAction)loginPressed {
    [self.loginProgressIndicator startAnimating];
    NSString *email = self.emailTextField.text;
    NSString *password = self.passwordTextField.text;

    [self.brain loginUsingEmail:email andPassword:password withBlock:^(NSDictionary *loginResults) {
        [self.loginProgressIndicator stopAnimating];
        [self.delegate uloopLoginViewController:self didLoginUserWithEmail:email andPassword:password];
    }];
}

アップデート

ここparameterEncodingで推奨されているように変更してみましたが、問題は解決しませんでした。

2回目の更新

これは、POSTデータにアクセスしているサーバー側からのPHPコードです。私はサーバー側で何もしておらず、その動作に非常に慣れていないため、これは私の同僚によって書かれました。

header('Content-type: application/json');
$username = $_POST['uname'];
$pw = $_POST['pw'];

サーバーコードは非常に単純です。彼は、変数値が何であるかを確認するある種のログスクリプトを持っており、クライアントがサーバーにアクセスしていると言っていますが、変数値は空白です。

3番目の更新

print_rこれは、$_REQUEST変数のを生成することによるHTTPリクエストのダンプです。

Array ( [sid] => FwAqvZrfckw )

そして、これが$_POST変数のダンプです。ご覧のとおり、完全に空白です。

Array ( )

4番目の更新

サーバーに送信される前にWiresharkを使用してパケットをキャプチャしましたが、すべてが正常に表示されています。

Accept: application/json
Content-Type: application/x-www-form-urlencoded; charset=utf-8

そして、POSTパラメータもすべてそこにありました。また、サーバー側でテストファイルを作成し、POSTそこにあるコードが機能していることを確認するためのテストを実行しました。

4

2 に答える 2

6

ありがとうございました。

同じ問題で、AFFormURLParameterEncodingを使用することが必要でした。

したがって、すべてのスレッドを単純化するには、次を使用する必要があります。 [[APIClient sharedClient] setParameterEncoding:AFFormURLParameterEncoding];

于 2012-11-07T13:51:47.713 に答える
0

ここで問題を引き起こすものは特にありませんが、同様の問題を解決するために使用した手順を説明することから始めます。

まず、ツールであるCharlesをチェックアウトします。これは、サーバーからの応答をインターセプトするデバッグWebプロキシであり、何が問題になっているのかをより明確に把握できるはずです。30日間の無料トライアルがあり、小さなバグを見つけるのに本当に役立ちました。これを使用するには、シーケンスボタンを押して、サーバーのURLを介して結果をフィルタリングします。そこから、サーバーとの間で送受信された要求と応答を確認できます。以下で問題が解決しない場合は、Charlesが吐き出したリクエストとレスポンスを投稿してください。

賢明な方法として[[APIClient sharedClient] setParameterEncoding:AFJSONParameterEncoding]、POSTリクエストを送信する直前に追加してみてください。yallはサーバー側の形式としてJSONを使用しているようです。

したがって、loginUsingEmailでは:

self.email = email;
self.password = password;

[[APIClient sharedClient] setParameterEncoding:AFJSONParameterEncoding];

[[APIClient sharedClient] postPath:@"insert path here" parameters:[NSDictionary dictionaryWithObjectsAndKeys:email, @"uname", password, @"pw", nil] success:^(AFHTTPRequestOperation *operation, id responseJSON) {
    if (block) {
        block(responseJSON);
    }
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
    NSLog(@"Error: %@", error);

    if (block) {
        block(nil);
    }
}];

// Store user data in app?
}
于 2012-07-06T00:39:27.433 に答える