gpt4 book ai didi

ios - 如何在 iOS 应用程序中使用 Objective C 解析 Ruby on rails webservice

转载 作者:行者123 更新时间:2023-11-28 22:05:42 25 4
gpt4 key购买 nike

我对Ruby on rails一无所知 我的任务是使用Ruby on rails 创建网络服务。我正在尝试使用 AFNetworking 和 NSURLConnection,但没有得到响应。

TIMESTAMP_KEY = '6ad3e2c0ffa4a89217e0'
SECRET_KEY = 'fd83be9e64ddec6ee8344708a189d039ad9e9d51'
HOST = 'http://em.dev.apb.com
URL = 'company_users/login'

他们说上面的参数是预定义的,它的 ruby​​ 代码是这样的:

1 params = []
2 sorted_params = params.sort.join
3 puts "sorted_params=#{sorted_params}"
4 digest = Digest::SHA1.hexdigest("#{timestamp}#{TIMESTAMP_KEY}")
5 puts "digest=#{digest}"
6 puts "SHA1(#{URL}#{sorted_params}#{SECRET_KEY}#{digest})"
7 signature = Digest::SHA1.hexdigest("#{URL}#{sorted_params}#{SECRET_KEY}#{digest}")

puts "#{HOST}#{URL}"

response = RestClient.post "#{HOST}/#{URL}/#{signature}/#{timestamp}", :email => email, :password => password

我不知道如何在 Objective-C 中获得它的响应:

我正在尝试 AFnetworking

 AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];

NSString *signature =[self sha1:@"fd83be9e64ddec6ee8344708a189d039ad9e9d51"];
NSString *timestep =[self sha1:@"6ad3e2c0ffa4a89217e0"];

NSDictionary *params = @{@"email": @"abc@superuser.com",@"password": @"aaa111"};

NSLog(@"%@",params);

NSString *url =[NSString stringWithFormat:@"http://em.dev.abc.com/company_users/login/%@/%@",signature,timestep];

[manager POST:url parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(@"JSON: %@", responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"Error: %@", error);
}];

我知道了 Link

所以我就用这个方法

-(NSString*) sha1:(NSString*)input
{
const char *cstr = [input cStringUsingEncoding:NSUTF8StringEncoding];
NSData *data = [NSData dataWithBytes:cstr length:input.length];

uint8_t digest[CC_SHA1_DIGEST_LENGTH];

CC_SHA1(data.bytes, data.length, digest);

NSMutableString* output = [NSMutableString stringWithCapacity:CC_SHA1_DIGEST_LENGTH * 2];

for(int i = 0; i < CC_SHA1_DIGEST_LENGTH; i++)
[output appendFormat:@"%02x", digest[i]];

return output;

}

每次我得到这样的错误输出时:

Error: Error Domain=NSCocoaErrorDomain Code=3840 "The operation couldn’t be completed. (Cocoa error 3840.)" (JSON text did not start with array or object and option to allow fragments not set.) UserInfo=0x8c56190 {NSDebugDescription=JSON text did not start with array or object and option to allow fragments not set.

最佳答案

从您得到的错误代码:

JSON text did not start with array or object and option to allow fragments not set

看起来您的请求或服务器响应不是有效的 JSON。

你确定你发布的是一个有效的 JSON 结构吗

{
"email": "myemail@test.com",
"password": "mypassword"
}

到远程主机?您确定您发布的 URL 是正确的吗?您是否尝试过在 Postman 之类的工具中测试 url ?

关于ios - 如何在 iOS 应用程序中使用 Objective C 解析 Ruby on rails webservice,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24079105/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com