【发布时间】:2012-03-02 05:25:16
【问题描述】:
我的用户输入了收件人地址(街道地址而不是电子邮件地址)。我需要通过 USPS 验证它,所以我知道它实际上是一个地址。
我现在正在研究他们的 API,我想我理解它,但我不确定如何使用 Objective-c 来处理它。
它几乎是这样工作的:
- 我必须创建一个包含收件人姓名、地址和邮政编码的 XML 请求。
- 我必须将其发布到他们的服务器
- 他们使用 XML 响应进行响应
以下是他们构造的 XML 请求的示例:
http://SERVERNAME/ShippingAPITest.dll?API=Verify&XML=<AddressValidateRequest% 20USERID="xxxxxxx"><Address ID="0"><Address1></Address1>
<Address2>6406 Ivy Lane</Address2><City>Greenbelt</City><State>MD</State> <Zip5></Zip5><Zip4></Zip4></Address></AddressValidateRequest>
有点乱码但坏掉了:
http://SERVERNAME/ShippingAPITest.dll?API=Verify&XML=
<AddressValidateRequest% 20USERID="xxxxxxx">
<Address ID="0">
<Address1></Address1>
<Address2>6406 Ivy Lane</Address2>
<City>Greenbelt</City>
<State>MD</State>
<Zip5></Zip5>
<Zip4></Zip4>
</Address>
</AddressValidateRequest>
我的第一个想法似乎很明显,但也许有更好的方法来实现它。由于 XML 提要很短,我是否应该通过以下方式进行简单的构建:
NSString *request = [NSString stringWithFormat:@"......"]
按照上面发布的行填写和格式化。
第二个问题是如何正确地将它发送到服务器?
我只是创建一个 NSURL 请求并将 URL 作为构造的 XML 字符串?
这是我所拥有的,但我一直认为 URL 构造错误:
- (void)verifyAddress:(Recipient*)_recipient {
NSURL *_url = [NSURL URLWithString:@"http://testing.shippingapis.com/ShippingAPITest.dll?API=Verify&XML=<AddressValidateRequest%20USERID=\"********\"><Address ID=\"0\"><Address1></Address1><Address2>6406 Ivy Lane</Address2><City>Greenbelt</City><State>MD</State><Zip5></Zip5><Zip4></Zip4></Address></AddressValidateRequest>"];
// Create the request.
NSURLRequest *theRequest=[NSURLRequest requestWithURL:_url
cachePolicy:NSURLRequestUseProtocolCachePolicy
timeoutInterval:60.0];
// create the connection with the request
// and start loading the data
NSURLConnection *theConnection=[[NSURLConnection alloc] initWithRequest:theRequest delegate:self];
if (theConnection) {
// Create the NSMutableData to hold the received data.
// receivedData is an instance variable declared elsewhere.
receivedData = [NSMutableData data];
NSString* newStr = [[NSString alloc] initWithData:receivedData
encoding:NSUTF8StringEncoding];
NSLog(@"the response '%@'", newStr);
} else {
// Inform the user that the connection failed.
NSLog(@"error");
}
}
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
// This method is called when the server has determined that it
// has enough information to create the NSURLResponse.
// It can be called multiple times, for example in the case of a
// redirect, so each time we reset the data.
// receivedData is an instance variable declared elsewhere.
[receivedData setLength:0];
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
// Append the new data to receivedData.
// receivedData is an instance variable declared elsewhere.
[receivedData appendData:data];
}
- (void)connection:(NSURLConnection *)connection
didFailWithError:(NSError *)error
{
// inform the user
NSLog(@"Connection failed! Error - %@ %@",
[error localizedDescription],
[[error userInfo] objectForKey:NSURLErrorFailingURLStringErrorKey]);
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
NSString* newStr = [[NSString alloc] initWithData:receivedData
encoding:NSUTF8StringEncoding];
NSLog(@"the response '%@'", newStr);
// do something with the data
// receivedData is declared as a method instance elsewhere
NSLog(@"Succeeded! Received %d bytes of data",[receivedData length]);
}
我收到以下错误:
Connection failed! Error - bad URL (null)
我现在唯一的问题是,就 NSURLConnection 而言,我做的一切都好吗?我可以玩弄这个 URL,我只是想确保我的实现没问题,所以我不会绕着圈子跑。 :P
【问题讨论】:
标签: objective-c ios xcode xmlhttprequest nsurlrequest