簡體   English   中英

JSON解析,如何從服務器獲取響應

[英]JSON parsing, How to get response from server

我有以下詳細信息,用於從服務器獲取數據。 methodIdentifier和Web服務名稱的用途是什么?

{"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];


}

當我在控制台中打印響應時:\\“意外的字符串結尾\\”

在不了解有關API的更多信息的情況下,我無法確定您的要求,但是服務器似乎期望該請求包含JSON。 您當前為請求創建主體的方式是使用標准POST變量。

您是否嘗試過更改:

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

至:

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

關於您的其他問題,我猜想該API有一個URL。 服務器使用methodidentifier來確定要運行的服務器方法。

之所以會收到此錯誤,是因為您沒有得到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。 干杯!

嘿兄弟,檢查我的回答是否有相同的問題可能會對您有幫助...您必須使用NSURLConnection代表來獲取數據

無法從Iphone制作Json Request正文

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM