GET请求,iOS

时间:2014-05-27 09:19:39

标签: ios http get

我需要做这个GET请求:

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=[]

在浏览器中,我得到了这样的回复:

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

但在iOS中它不起作用。

我试试:

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

我尝试了以上所有代码,但它不起作用(很长时间以后......只是坚持这个)。
如何解决这个问题?

2 个答案:

答案 0 :(得分:4)

这是因为您的网址不正确,此字符串应添加百分比转义。试试这个:

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

答案 1 :(得分: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
}

NSURLResponse和NSError vars被传递到sendSynchronousReqeust方法,因此当它返回时,它们将填充原始响应和错误(如果有的话)。如果您需要检查响应代码之类的内容,可以通过您传递的“响应”变量来执行此操作。