0

Qtを使用してc ++コードスニペットをobjective-cに変換しようとする考古学の仕事に苦労しているのは、サーバーからのxmlを応答として解析するために要求を送信するためです。私は本当に努力していますが、それを達成することはできません。誰かが私に助けを与えることができれば。

void RestClient::_prepareRequest( QNetworkRequest& a_request, const QString& a_uri )                  {  
        QSslConfiguration config(QSslConfiguration::defaultConfiguration());
        config.setProtocol(QSsl::SslV3);
        config.setSslOption(QSsl::SslOptionDisableServerNameIndication, true);
        a_request.setSslConfiguration(config);
        a_request.setRawHeader("Accept", "application/xml");
        a_request.setRawHeader("Content-Type", "application/x-www-form-urlencoded");
        QByteArray l_api_key; l_api_key.append( toQString( m_api_key) );
        QByteArray l_request_hash; 
    l_request_hash.append( toQString( _buildRequestHash( toStlString(a_uri) ) ) );
        a_request.setRawHeader("EMApikey", l_api_key );
        a_request.setRawHeader("EMRequestHash", l_request_hash );


        a_request.setUrl( QUrl( a_uri ) );
    }

- (NSString *)hmacsha1:(NSString *)data secret:(NSString *)key {

const char *cKey  = [key cStringUsingEncoding:NSASCIIStringEncoding];
const char *cData = [data cStringUsingEncoding:NSASCIIStringEncoding];

unsigned char cHMAC[CC_SHA1_DIGEST_LENGTH];

CCHmac(kCCHmacAlgSHA1, cKey, strlen(cKey), cData, strlen(cData), cHMAC);

NSData *HMAC = [[NSData alloc] initWithBytes:cHMAC length:sizeof(cHMAC)];

NSString *hash = [HMAC base64EncodedStringWithOptions:(nil)];

return hash;

}

ここに私のコードがあります:

- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];

if ([stories count] == 0) {

    NSString * myServerUrl = @"**************";
    NSString * l_api_key = @"*******************************";
    NSString * l_secret_key = @"************************************";

    NSMutableURLRequest *theRequest=[NSMutableURLRequest requestWithURL:[NSURL URLWithString:[myServerUrl stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]] cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:60.0];
    //do post request for parameter passing
    [theRequest setHTTPMethod:@"GET"];


    [theRequest setValue:@"xml" forHTTPHeaderField:@"xml"];

    [theRequest addValue:@"l_api_key" forHTTPHeaderField: l_api_key];

    [theRequest addValue:@"l_request_hash" forHTTPHeaderField:[self hmacsha1:l_api_key secret:l_secret_key]];

    NSURLConnection *theConnection = [[NSURLConnection alloc] initWithRequest:theRequest delegate:self];

    if( theConnection )
    {
        NSLog(@"we get here !");
        NSURL *webData = [[NSMutableData data] retain];
        [self parseXMLFileAtURL: webData];
        cellSize = CGSizeMake([newsTable bounds].size.width, 60);
    }
    else
    {
        NSLog(@"theConnection is NULL");
    }

    [theConnection release];


}

//cellSize = CGSizeMake([newsTable bounds].size.width, 60);

}

4

1 に答える 1

2

ビデオゲーム開発のバックグラウンドがあるため、コードをある言語/アーキテクチャから別の言語/アーキテクチャに移植する必要があるのは一般的です。これに取り組むには 2 つの方法があります。

1)各関数/クラスを取得し、コード行を行ごとに直接コピーします。

2) 入力と出力を備えたブラック ボックスとしてシステムが行うことを見て、自分でコードを記述します。

実装に関して別の開発者の意図が何であったかを理解するのに苦労するよりも、自分のコードを他の人のコードよりもよく知っており、その方がより良い仕事をすることができるため、通常は2番の方が良いオプションです。

したがって、元のコードが何をすべきかを理解し、Objective-C で独自の実装を作成することをお勧めします。それを実装する方法の学習曲線が含まれる場合は、それを行うために何が必要かを調査して学習したほうがよいでしょう。

于 2013-11-04T10:33:49.800 に答える