【问题标题】:JSON parsing, How to get response from serverJSON解析,如何从服务器获取响应
【发布时间】:2012-07-09 23:24:01
【问题描述】:

我有以下详细信息,用于从服务器获取数据。 methodIdentifier 和 web service name 有什么用?

{"zip":"12345","methodIdentifier":"s_dealer"}

url:- http://xxxxxxxxxxxxxxx.com/api.php
method: post
web service name: s_dealer

response : {"success":"0","dealer":[info...]}

我不知道如何用 url 发送 邮编“12345”。请指引我正确的方向。我使用以下。

-(void)IconClicked:(NSString *)zipNumber 
{


NSString *post = [NSString stringWithFormat:@"&zipNumber=%@",zipNumber];



NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];

NSString *postLength = [NSString stringWithFormat:@"%d",[postData length]];

NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];

[request setURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://xxxxxxxxxxxxxxxxxxx.com/api.php"]]];

[request setHTTPMethod:@"POST"];

[request setValue:postLength forHTTPHeaderField:@"Content-Length"];

[request setHTTPBody:postData];

NSURLConnection *conn = [[NSURLConnection alloc]initWithRequest:request delegate:self];

if(conn)
{
    NSLog(@"Connection Successful");
}
else
{
    NSLog(@"Connection could not be made");
}

  receivedData = [[NSMutableData alloc]init];


}

当我在控制台打印响应时:\"Unexpected end of string\"

【问题讨论】:

    标签: iphone ios json parsing


    【解决方案1】:

    在不了解 API 的情况下,我无法确定您的要求,但似乎服务器希望请求包含 JSON。您当前为请求创建正文的方式是使用标准 POST 变量。

    您是否尝试过更改:

    NSString *post = [NSString stringWithFormat:@"&zipNumber=%@",zipNumber];
    

    到:

    NSString *post = [NSString stringWithFormat:@"{\"zip\":\"%@\",\"methodIdentifier\":\"s_dealer\"}",zipNumber];
    

    关于您的其他问题,我猜该 API 有一个 URL。服务器使用methodidentifier 来确定要运行的服务器方法。

    【讨论】:

      【解决方案2】:

      您收到此错误是因为您没有收到 json 作为响应,而是来自 Apache(或其他)的错误,它具有不同的结构,并且 json 无法解析它。尝试我的方法发起连接,以获得成功。

      声明一个 NSURLConnection 属性并合成它。现在:

      NSString *post = [NSString stringWithFormat:@"zipNumber=%@",zipNumber];
      
          NSString *toServer = [NSString stringWithString:@"your server with the last slash character"];
      
          NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"%@api.php?", toServer]];
          NSData *postData = [post dataUsingEncoding:NSUTF8StringEncoding allowLossyConversion:NO];
          NSString *postLength = [NSString stringWithFormat:@"%d", [postData length]];
          NSMutableURLRequest *urlRequest = [[[NSMutableURLRequest alloc] init] autorelease];
          [urlRequest setURL:url];
          [urlRequest setHTTPMethod:@"POST"];
          [urlRequest setValue:postLength forHTTPHeaderField:@"Content-Length"];
          [urlRequest setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
          [urlRequest setValue:@"utf-8" forHTTPHeaderField:@"charset"];
          [urlRequest setHTTPBody:postData];
          [urlRequest setTimeoutInterval:30];
      
          NSURLConnection *tmpConn = [[[NSURLConnection alloc] initWithRequest:urlRequest delegate:self] autorelease];
      
          self.yourConnectionProperty = tmpConn;
      

      现在您可以在连接委托中使用 self.yourConnectionProperty。干杯!

      【讨论】:

        【解决方案3】:

        嘿,兄弟,检查我对相同问题的回答可能会对您有所帮助...您必须使用 NSURLConnection Delegates 来获取数据

        Could not make Json Request body from Iphone

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2014-02-04
          • 2019-02-03
          • 1970-01-01
          • 2021-08-14
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多