2

私はさまざまな解決策を検索して試しましたが、うまくいきませんでした。

サーバーにJSONリクエストを作成しようとしていますが、PHPでテストしたため、確実に機能します(単純な呼び出し、文字列名と文字列パスワードを受け取り、ユーザーが正しい場合はデータベースにチェックインします)。リクエストは、プロパティ「success」のみを含む JSON を受け取る必要があります (「true」または「false」の可能性があります)。

これが私のコードです:

- (void)logonService:(NSString *) name widthArg2:(NSString *) password {
// Create the request.
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"http://46.51.169.145/ios/index.php/user/login/"]];

// Specify that it will be a POST request
request.HTTPMethod = @"POST";

//setting json fields
[request setValue:@"application/json; charset=utf-8" forHTTPHeaderField:@"Content-Type"];

//prepare output data
NSString *in1;
NSDictionary *outputData = [NSDictionary dictionaryWithObjectsAndKeys:in1, @"success", nil];
NSError *error = nil;
NSData *jsonOutputData = [NSJSONSerialization dataWithJSONObject:outputData options:NSJSONWritingPrettyPrinted error:&error];
NSString *jsonOutputString = [[NSString alloc] initWithData:jsonOutputData encoding:NSUTF8StringEncoding];
//set json string to body data
NSData *requestInputBodyData = [jsonOutputString dataUsingEncoding:NSUTF8StringEncoding];
[request setHTTPBody: requestInputBodyData];

//prepare input data
NSString *input = [NSString stringWithFormat:@"&name=%@&password=%@",name,password];
//Encode the post string using NSASCIIStringEncoding and also the post string you need to send in NSData format.
NSData *inputData = [input dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
//calculate length of post data
NSString *inputLength = [NSString stringWithFormat:@"%d",[inputData length]];
//Set HTTP header field with length of the post data.
[request setValue:inputLength forHTTPHeaderField:@"Content-Length"];
//encode type
//[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Current-Type"];
//add it to http body
[request setHTTPBody:inputData];

// Create url connection and fire request
NSURLConnection *conn = [[NSURLConnection alloc] initWithRequest:request delegate:self];
if(conn){
    NSLog(@"Connection Successful");
}else{
    NSLog(@"Connection could not be made");
}

}

デリゲート DidReceiveData は正しく起動しますが、結果は常に NIL です。

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
NSString *jsonResponseData = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:nil];
NSLog(@"RESULT1: %@", jsonResponseData);
[_responseData appendData:data];

}

ありがとう、誰かが私を助けてくれることを願っています!

4

1 に答える 1

2

コメントをありがとう、私は最終的にそれを修正し、その後JSONライブラリの使用を開始することにしました-> https://github.com/stig/json-framework/

これは私が最終的にコードを持っている方法です:

//prepare connection
NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"%@user.php", _urlServer]];
NSMutableDictionary *data = [[NSMutableDictionary alloc] init];
[data setObject:@"login" forKey:@"action"];
[data setObject:username forKey:@"username"];
[data setObject:password forKey:@"password"];

//start connection
// Create the request, if there is a connection going on just cancel it.
[_connection cancel];

//initialize new mutable data
NSMutableData *receivedData = [[NSMutableData alloc] init];
_receivedData = receivedData;

//initialize the url which will be fetched
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[url standardizedURL]];

//set http method
[request setHTTPMethod:@"POST"];

//initialize a post data
NSString *dataString = [[NSString alloc] init];
for(id key in data) {
    id value = [data objectForKey:key];
    //keys string
    NSString *newData =  [NSString stringWithFormat:@"&%@=%@", key, value];
    dataString = [dataString stringByAppendingString: newData];
}

//set request content type we MUST set this value.
[request setValue:@"application/x-www-form-urlencoded;charset=UTF-8" forHTTPHeaderField:@"Content-Type"];

//set post data of request
[request setHTTPBody:[dataString dataUsingEncoding:NSUTF8StringEncoding]];

//initialize a connection from request
NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
_connection = connection;

//NSLog(@"Sending request with parameters: %@", dataString);

//begin the connection
[_connection start];

そして、その魅力のように機能します。次のステップは、何らかの方法で認証することです。現在、接続は世界中に開かれています。

于 2013-11-19T12:17:55.923 に答える