【问题标题】:Street address verification街道地址验证
【发布时间】:2012-03-02 05:25:16
【问题描述】:

我的用户输入了收件人地址(街道地址而不是电子邮件地址)。我需要通过 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

【问题讨论】:

    标签: objective-c ios xcode xmlhttprequest nsurlrequest


    【解决方案1】:

    您的网址中有% 20。应该是%20(没有空格)。

    可能还有其他问题,但很容易发现。如果您收到错误消息,您需要编辑您的问题并粘贴在exact错误消息中。

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

    【讨论】:

    • 您没有正确使用NSURLConnection。请阅读URL加载系统编程指南中的Using NSURLConnection
    【解决方案2】:

    Cory,我在地址验证行业工作(为SmartyStreets 工作,实际上您正在尝试做的是我们的专长)并且遇到了很多与您类似的问题。

    实际上,我们曾经为地址验证 API (LiveAddress) 支持 XML 端点。去年我们弃用了它并部署了一种新的 JSON 格式,因为 XML 使用起来很笨重,而且当它实际上只是一个简单的任务时(对于开发人员来说),它存在很多问题。

    所以要记住几件事......虽然 Rob 的回答在程序上很全面,但这些也很重要:

    • USPS 是美国的官方地址来源,但其核心域提供 API 服务。尤其是最近出现财务问题,我怀疑 API 的支持和维护会随着时间的推移而减弱。

    • 您正在使用的 API 的 License Agreement 非常严格。例如:

      用户同意仅使用 USPS 网站、API 和 USPS 数据来促进 USPS 运输交易。 [2012 年 1 月 27 日]

      意思是,如果您使用他们的 API 通过 USPS 运送邮件或包裹,这很好,但出于任何其他目的,这是不允许的,并且违反了 TOS。

    • 我看到您正在为 iOS 开发。有一个很棒的 JSON 库,名为 TouchJSON,在我看来,它比 Objective-C 中的 XML 格式更易于使用。

    虽然 USPS 服务确实有效,但他们对私人实体进行 CASS 认证,以提供更高价值的数据(更多专业、经验、功能等)。

    这些和其他问题可以通过第三方供应商的服务来解决。更多细节和原因documented here。您选择哪个提供商取决于您,但我很乐意亲自回答任何其他与地址验证相关的问题。

    【讨论】:

    • 我们实际上是使用 USPS 来运送物品,所以我们对他们的 TOS 没问题。我以前有 SmartyStreets,它是一个出色的网站/应用程序,但我们的预算有限,需要一些免费的东西 (USPS)。感谢您对TouchJSON的参考,我去看看是否有帮助!
    猜你喜欢
    • 2018-11-04
    • 1970-01-01
    • 2013-05-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-08-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多