简体   繁体   中英

JSON parsing, How to get response from server

I have the following details, for get the data from server. What is the use of methodIdentifier and 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...]}

I don't know how to send zip number "12345" with the url. Please direct me on right direction. I use the following.

-(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];


}

when i print the response in console :\\"Unexpected end of string\\"

Without knowing more about the API, I can't be certain about your requirements, but it seems that the server is expecting the request to contain JSON. The way you currently are creating the body for the request is using standard POST variables.

Have you tried changing:

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

to:

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

Regarding your other questions, I'm guessing that there is a single URL for the API. The methodidentifier is used by the server in order to determine which server method(s) to run.

You get this error because you do not get a json as a response, but an error from Apache (or whatever), that has different structure, and json cannot parse it. Try my method to initiate the connection, in order to gain a successful one.

Declare a NSURLConnection property and synthesize it. Now:

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;

Now you work with self.yourConnectionProperty in connection delegates. Cheers!

hey bro check my answer for same problem may help you... You have to use the NSURLConnection Delegates to get the data

Could not make Json Request body from Iphone

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM