gpt4 book ai didi

objective-c - 街道地址验证

转载 作者:行者123 更新时间:2023-11-29 11:15:44 27 4
gpt4 key购买 nike

我的用户输入收件人地址(街道地址而非电子邮件)。我需要通过 USPS 验证它,以便我知道它实际上是一个地址。

我现在正在研究他们的 API,我想我理解它,但我不确定如何使用 objective-c 来实现它。

它几乎是这样工作的:

  1. 我必须创建一个包含收件人姓名、地址和邮政编码的 XML 请求。
  2. 我必须将其发布到他们的服务器
  3. 他们以 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

最佳答案

您的 URL 中有 % 20。它应该是 %20(没有空格)。

可能还有其他问题,但这是一个很容易发现的问题。如果您收到错误消息,您需要编辑您的问题并粘贴确切错误消息。

此外,您可能会考虑使用 Apple 的 NSURLRequestNSURLConnection 类,因为可能会有更多人熟悉它们,因此您可能更容易找到帮助。

关于objective-c - 街道地址验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9528410/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com