0

レコードが空かどうかを確認するためにURLに接続する必要があります。応答は次のようになります。

<?xml version = "1.0" encoding = "UTF-8"?>
<find>
<record_id>1234</record_id>
<no_record>00001</no_record>
<entry_num>00001</entry_num>
<session-id>aijheifaohqrihelrkqn324tlejaofjaf</session-id>
</find>

私のコード:

                NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init]
                                                autorelease];
                [request setURL:[NSURL URLWithString: finalSearchURL]];

                // Content-Type related.
                [request setValue:@"application/x-www-form-urlencoded"
               forHTTPHeaderField:@"Content-Type"];

                // Create Connection.
                NSURLConnection *conn = [[NSURLConnection alloc] initWithRequest:request delegate:self];

                if (conn) {
                    // The connection was established.
                    NSMutableData *receivedData =  [[NSMutableData alloc] initWithContentsOfURL:[NSURL URLWithString:request]];
                    NSLog( @"Data will be received from URL: %@", request.URL );
                    NSLog(@"Recieved Data 2: %@", receivedData);
                }
                else
                {
                    // The download could not be made.
                    NSLog( @"Data could not be received from: %@", request.URL );
                }

しかし、それは私を返します:

Recieved Data : <3c3f786d 6c207665 7273696f 6e203d20 22312e30 2220656e 636f6469 6e67203d 20225554 462d3822 3f3e0a3c 66696e64 3e0a3c73 65745f6e 756d6265 723e3031 39303633 3c2f7365 745f6e75 6d626572 3e0a3c6e 6f5f7265 636f7264 733e3030 30303030 3030313c 2f6e6f5f 7265636f 7264733e 0a3c6e6f 5f656e74 72696573 3e303030 30303030 30313c2f 6e6f5f65 6e747269 65733e0a 3c736573 73696f6e 2d69643e 4d505843 33323433 58564336 4534454a 41464232 45473541 39374237 584e3832 43554631 4e314234 584e4c37 424c5947 4e533c2f 73657373 696f6e2d 69643e0a 3c2f6669 6e643e0a 20>

誰かが私が間違っていることを教えてくれるのを手伝ってもらえますか?これは、URLから応答を取得するための私の最初の試みです。ありがとうございます。

4

3 に答える 3

0

See the data as a string this way:

NSString *string = [[NSString alloc] initWithData:receivedData encoding:NSUTF8StringEncoding];
NSLog(@"the xml string is %@", string);

If the parsing goal is simple enough - like just to find the value of one tag - you can use string methods to parse. Otherwise, NSXMLParser or several other options are available.

To see if the string contains a substring, you can do something like this:

if (string) {
    NSRange range = [string rangeOfString:@"<session-id>"];
    if (range.location != NSNotFound) {
        // session-id tag is at index range.location, so we know it's there
    }
}
于 2012-08-15T03:44:23.680 に答える
0

使用した方法は、URLから生データを取得することです。生データを理解可能な構造(おそらくNSArrayではなくNSDictionary)に変換するためのパーサーが必要です。

Appleは、URLからxml構造を取得するためのNSXMLParserを提供しています。または、他のxmlパーサーライブラリを見つけることもできます。

于 2012-08-15T03:16:41.977 に答える
0

実際、コードは正しいデータを返しています。NSDataはあらゆる種類のデータを保持できるため、16進値のみを表示します。16進データをスティングに変換すると、正しいテキストが含まれていることがわかります。

これで、コードを大幅に簡略化できます。NSURLConnectionを設定するためのすべてのコードはまったく必要ありません。必要なのは次の行だけです。

NSString *recievedText = [NSString stringWithContentsOfFile:finalSearchURL encoding:NSUTF8StringEncoding error:NULL];
于 2012-08-15T03:18:40.917 に答える