G.P.Reddy
G.P.Reddy

Reputation: 556

Web services issue on POST method

I am struggled on web services POST method,i used one services in android and iOS.In the android it's working fine ,but iOS the response didn't come the correct format.Here i am using iOS Code:

 Label.text=@"Bangalore";
    NSLog(@"text=%@",Label.text);

   NSString *myRequestString = [[NSString alloc] initWithFormat:@"city=%@",Label.text];
     NSData *myRequestData = [ NSData dataWithBytes: [ myRequestString UTF8String ] length: [ myRequestString length ] ];
    NSMutableURLRequest *request = [ [ NSMutableURLRequest alloc ] initWithURL: [ NSURL URLWithString:@"http://****************.php"]];
    [request setHTTPMethod:@"POST"];
  //  [request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"content-type"];
   [request setHTTPBody: myRequestData];
    NSURLResponse *response;
    NSError *err;
    NSData *returnData = [NSURLConnection sendSynchronousRequest: request returningResponse:&response error:&err];
    NSError* error;
    NSMutableArray* result = [NSJSONSerialization JSONObjectWithData:returnData
                                                             options:kNilOptions
                                                               error:&error];
    NSLog(@"sss=%@",result);

Here i am getting the response like this,

sss={
    Bangalore =     (
                {
            Price = 384;
            date = "01-12-2014";
        },
                {
            Price = 384;
            date = "02-12-2014";
        },
                {
            Price = 384;
            date = "03-12-2014";
        },
                {
            Price = 374;
            date = "08-12-2014";
        },
                {
            Price = 374;
            date = "09-12-2014";
        },

                {
            Price = 365;
            date = "13-12-2014";
        },
                {
            Price = 365;
            date = "14-12-2014";
        },

                {
            Price = 369;
            date = "18-12-2014";
        }
    );
}

In android same services i get the correct response but iOS i am facing this issue can please how can it solve?and any mistakes on my code? Thank you@

Upvotes: 1

Views: 181

Answers (2)

CouchDeveloper
CouchDeveloper

Reputation: 19106

There are a couple of issues in your POST request:

You didn't specify a Content-Type. Notice the consequences as described in section 3.1.1.5 Content-Type :

If a Content-Type header field is not present, the recipient MAY either assume a media type of application/octet-stream ([RFC2046], Section 4.5.1) or examine the data to determine its type.

That is, the server will likely assume the content is of type application/octet-stream which is not appropriate in your case. I would suggest application/json. Despite the fact that this would also require a change on the server - it's much easier to create and to process on the client and the server.

I would not recommend application/x-www-form-urlencoded since it requires encoding and its usage is much more error prone.

Furthermore, in your code you didn't correctly initialize the data object:

`NSData *myRequestData = [NSData dataWithBytes: [myRequestString UTF8String] length: [myRequestString length]];

Parameter length should be set to the number of bytes of the string representation in UTF-8 - and NOT [myRequestString length] since this returns the number of characters (more precisely, the number UTF-16 code units).

As mentioned, you can avoid a couple of potential issues when using JSON as the underlying content type. You would first create a JSON representation as your "body". That is, you would create a NSDictionary with a key @city and whose value (type NSString) becomes Label.text. Then serialize this object to JSON using NSJSONSerialization into a NSData object which contains a UTF-8 JSON and which becomes your request body.

Upvotes: 0

Rob
Rob

Reputation: 437482

You received valid JSON, but then successfully parsed it into a dictionary with an array of dictionaries using NSJSONSerialization. Don't worry that the NSLog of result doesn't look like JSON. It's not supposed to.

If you really want to see the original JSON, you can:

NSLog(@"Original JSON = ", [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding]);

That will look like what you expected (and we knew it would, because otherwise the JSON parsing would have failed). But don't worry about this original JSON. Just use result.

Note, though, result is a NSDictionary, not a NSArray (nor a NSMutableArray). So you should define it as such.

You can now use result:

NSArray *bangalore = result[@"Bangalore"];
NSDictionary *priceObject = bangalore[0];
NSString *price = priceObject[@"Price"]; // might be NSNumber, depends on whether JSON had quotes around the price or not
NSString *date = priceObject[@"date"];

Upvotes: 2

Related Questions