GET请求,iOS [英] GET request, iOS

查看:108
本文介绍了GET请求,iOS的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要做这个GET请求:

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
}

但是在iOS中它不起作用。

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?

推荐答案

这是因为你的URL不正确,这个字符串应该加上百分号转义。试试这个:

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

这篇关于GET请求,iOS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆