3

I need to do this GET request:

http://api.testmy.co.il/api/sync?BID=1049&ClientCode=3847&Discount=2.34&Service=0&Items=[{"Name":"Tax","Price":"2.11","Quantity":"1","SerialID":"1","Remarks":"","Toppings":""}]&Payments=[]

In browser I get this response:

{
    "Success": true,
    "Atava": [],
    "Pending": [],
    "CallWaiter": false
}

But in iOS its not working.

I try:

NSString *requestedURL=[NSString stringWithFormat:@"http://api.testmy.co.il/api/sync?BID=%i&ClientCode=%i&Discount=2.34&Service=0&Items=[{\"Name\":\"Tax\",\"Price\":\"2.11\",\"Quantity\":\"1\",\"SerialID\":\"1\",\"Remarks\":\"\",\"Toppings\":\"\"}]&Payments=[]",BID,num];
NSURL *url = [NSURL URLWithString:requestedURL];
NSURLResponse *response;
NSData *GETReply = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:nil];
NSString *theReply = [[NSString alloc] initWithBytes:[GETReply bytes] length:[GETReply length] encoding: NSASCIIStringEncoding];
NSLog(@"Reply: %@", theReply);

OR

NSString *requestedURL = [NSString stringWithFormat:@"http://api.testmy.co.il/api/sync?BID=%i&ClientCode=%i&Discount=2.34&Service=0&Items=[{'Name':'Tax','Price':'2.11','Quantity':'1','SerialID':'1','Remarks':'','Toppings':''}]&Payments=[]", BID, num];

OR

NSMutableDictionary *params = [[NSMutableDictionary alloc] init];
[params setObject:@"Tax" forKey:@"Name"];
[params setObject:@"2.11" forKey:@"Price"];
[params setObject:@"1" forKey:@"Quantity"];
[params setObject:@"1" forKey:@"SerialID"];
[params setObject:@"" forKey:@"Remarks"];
[params setObject:@"" forKey:@"Toppings"];

NSData *jsonData = nil;
NSString *jsonString = nil;

if([NSJSONSerialization isValidJSONObject:params])
{
    jsonData = [NSJSONSerialization dataWithJSONObject:params options:0 error:nil];
    jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
    NSLog(@"%@", jsonString);
}

NSString *get = [NSString stringWithFormat:@"&Items=%@", jsonString];

NSData *getData = [get dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];

NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:url];
[request setHTTPMethod:@"GET"];
[request setTimeoutInterval:8];
[request setHTTPBody:getData];
[request setValue:@"application/json;charset=UTF-8" forHTTPHeaderField:@"Content-Type"];

I tried all above code but it doesn't work(long time out.. just stuck on this).
How to fixing this?

gideon
  • 19,329
  • 11
  • 72
  • 113
phnmnn
  • 12,813
  • 11
  • 47
  • 64
  • 2
    please explain "doesn't work" : do you get any error message or so ? – Laurent S. May 27 '14 at 09:21
  • Have you tried to output your url string and see if all get values are present? – Pancho May 27 '14 at 09:22
  • Yes i tried. "doesn't work" = long time out.. just stuck on this. – phnmnn May 27 '14 at 09:24
  • Did you set the delegate and implement the - (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response (https://developer.apple.com/librarY/mac/documentation/Foundation/Reference/NSURLConnectionDataDelegate_protocol/Reference/Reference.html#//apple_ref/occ/intfm/NSURLConnectionDataDelegate/connection:didReceiveResponse:) ? – Zhang May 27 '14 at 09:29
  • [NSURLConnection sendAsynchronousRequest:request queue:queue completionHandler:^(NSURLResponse *response, NSData *data, NSError *error){}]; – phnmnn May 27 '14 at 09:32
  • Did u try NSURLRequest? – odukku May 27 '14 at 09:34

2 Answers2

4

This is because your URL is not correct, this string should add percent escape. Try with this:

NSString *requestedURL=[NSString stringWithFormat:@"http://api.testmy.co.il/api/sync?BID=%i&ClientCode=%i&Discount=2.34&Service=0&Items=[{'Name':'Tax','Price':'2.11','Quantity':'1','SerialID':'1','Remarks':'','Toppings':''}]&Payments=[]",BID,num];
NSURL *url = [NSURL URLWithString:[requestedURL stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
//and you use this url
nmh
  • 2,497
  • 1
  • 15
  • 27
1
// Send a synchronous request
NSURLRequest * urlRequest = [NSURLRequest requestWithURL:[NSURL URLWithString:@"enter your url here"]];
NSURLResponse * response = nil;
NSError * error = nil;
NSData * data = [NSURLConnection sendSynchronousRequest:urlRequest
                                          returningResponse:&response
                                                      error:&error];

if (error == nil)
{
    // Parse data here
}

The NSURLResponse and NSError vars are passed into the sendSynchronousReqeust method so when it returns, they will be populated with the raw response and error (if any). If you need to check for stuff like response codes you can do so via the “response” variable you pass.

Nilesh Kumar
  • 2,141
  • 3
  • 16
  • 20
  • //then you can log and check the response NSString *responseString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]; NSLog(@"%@", responseString); – Nilesh Kumar May 27 '14 at 09:49