我正在开发一个 iOS 应用程序,它会触发一些 NSURL 请求。我创建了一个实现 didReceiveData 的 WebService 类,如下所示:
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
// Append the new data to the instance variable you declared
[_responseData appendData:data];
}
其中 _responseData 是一个 NSMutableData* 属性。然后我创建了几个子类来创建它们的特定请求并实现 connectionDidFinishLoading。具体来说,我有一个像这样实现它的 RegisterService 类:
- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
// The request is complete and data has been received
// You can parse the stuff in your instance variable now
NSError *error = nil;
_parsedData = [NSJSONSerialization JSONObjectWithData:_responseData options:kNilOptions error:&error];
NSLog(@"register data: %@",_parsedData);
registerAnswer serverAnswer = [[_parsedData objectForKey:@"success"] integerValue];
if (serverAnswer == REGISTER_SUCCESS) {
// Give data to delegate to handle
[self.delegate successRegister];
}
else { // serverAnswer == REGISTER_FAIL
NSLog(@"register message: %@",[[_parsedData objectForKey:@"message"] stringValue]);
[self.delegate failedRegister];
}
}
我有 2 个不同的 View Controller 使用此服务。其中一位这样调用它:
self.personRegistering.username = self.txtUsername.text;
self.personRegistering.password = self.txtPassword.text;
if (self.personRegistering.isCustomer) {
// register customer
DGRegisterService* myRegisterService = [[DGRegisterService alloc] initWithDelegate:self];
[myRegisterService registerPerson:self.personRegistering];
}
else {
// continue to get picture
[self performSegueWithIdentifier:@"RegisterPageThreeToFour" sender:self];
}
而且效果很好。另一个(第四页)让用户在注册前拍照。这是代码:
- (IBAction)btnTakePicture:(id)sender {
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
// Camera is available
UIImagePickerController* myCamera = [[UIImagePickerController alloc] init];
myCamera.sourceType = UIImagePickerControllerSourceTypeCamera;
myCamera.delegate = self;
[self presentViewController:myCamera animated:YES completion:NULL];
}
}
#pragma mark - UIImagePicker Delegate
- (void) imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
[picker dismissViewControllerAnimated:YES completion:NULL];
// Register person first
DGRegisterService* myRegisterService = [[DGRegisterService alloc] initWithDelegate:self];
[myRegisterService registerPerson:self.personRegistering];
// // Now send pic
// UIImage* picture = [info objectForKey:UIImagePickerControllerOriginalImage];
// DGUploadService* myUploadService = [[DGUploadService alloc] initWithDelegate:self];
// [myUploadService uploadImage:picture forUsername:self.personRegistering.username];
}
现在,第二个发送请求,服务器正确处理它,发回这个 JSON 对象:
{"success":1,"message":"Account successfully created."}
这是我所期望的。但是,两个 NSLog 语句都显示“(null)”。有时,应用程序崩溃是因为 objectForKey 被发送到一个不响应它的实例。有时,在我点击 XCode 上的“停止”后,控制台将显示 JSON 对象。其余时间它不会崩溃或显示对象。
由于此页面使用相机,我必须只在 iPhone 上进行测试,而不是在模拟器上进行测试。另一个页面在模拟器和 iPhone 中每次都正确显示 JSON 对象。我用的 iPhone 是 iOS7.1.1 的 4S。
最佳答案
如果你的意思是下面几行中的NSLog:
NSError *error = nil;
_parsedData = [NSJSONSerialization JSONObjectWithData:_responseData options:kNilOptions error:&error];
NSLog(@"register data: %@",_parsedData);
有时会打印“(null)”,这可能是 JSONObjectWithData 调用错误造成的。
我建议添加
NSLog(@"received data: %@", _responseData);
检查数据是否一切正常,以及
NSLog(@"register data: %@ -- (error: %@)",_parsedData, [error localizedDescription]);
查看是否返回错误。
关于ios - NSURLConnection 在它应该之前调用 connectionDidFinishLoading?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24460482/
为了将Cucumber用于命令行脚本,我按照提供的说明安装了arubagem。它在我的Gemfile中,我可以验证是否安装了正确的版本并且我已经包含了require'aruba/cucumber'在'features/env.rb'中为了确保它能正常工作,我写了以下场景:@announceScenario:Testingcucumber/arubaGivenablankslateThentheoutputfrom"ls-la"shouldcontain"drw"假设事情应该失败。它确实失败了,但失败的原因是错误的:@announceScenario:Testingcucumber/ar
我有一个服务模型/表及其注册表。在表单中,我几乎拥有服务的所有字段,但我想在验证服务对象之前自动设置其中一些值。示例:--服务Controller#创建Action:defcreate@service=Service.new@service_form=ServiceFormObject.new(@service)@service_form.validate(params[:service_form_object])and@service_form.saverespond_with(@service_form,location:admin_services_path)end在验证@ser
我正在尝试编写一个将文件上传到AWS并公开该文件的Ruby脚本。我做了以下事情:s3=Aws::S3::Resource.new(credentials:Aws::Credentials.new(KEY,SECRET),region:'us-west-2')obj=s3.bucket('stg-db').object('key')obj.upload_file(filename)这似乎工作正常,除了该文件不是公开可用的,而且我无法获得它的公共(public)URL。但是当我登录到S3时,我可以正常查看我的文件。为了使其公开可用,我将最后一行更改为obj.upload_file(file
这里有一个很好的答案解释了如何在Ruby中下载文件而不将其加载到内存中:https://stackoverflow.com/a/29743394/4852737require'open-uri'download=open('http://example.com/image.png')IO.copy_stream(download,'~/image.png')我如何验证下载文件的IO.copy_stream调用是否真的成功——这意味着下载的文件与我打算下载的文件完全相同,而不是下载一半的损坏文件?documentation说IO.copy_stream返回它复制的字节数,但是当我还没有下
如何在ruby中调用C#dll? 最佳答案 我能想到几种可能性:为您的DLL编写(或找人编写)一个COM包装器,如果它还没有,则使用Ruby的WIN32OLE库来调用它;看看RubyCLR,其中一位作者是JohnLam,他继续在Microsoft从事IronRuby方面的工作。(估计不会再维护了,可能不支持.Net2.0以上的版本);正如其他地方已经提到的,看看使用IronRuby,如果这是您的技术选择。有一个主题是here.请注意,最后一篇文章实际上来自JohnLam(看起来像是2009年3月),他似乎很自在地断言RubyCL
我正在尝试解析一个文本文件,该文件每行包含可变数量的单词和数字,如下所示:foo4.500bar3.001.33foobar如何读取由空格而不是换行符分隔的文件?有什么方法可以设置File("file.txt").foreach方法以使用空格而不是换行符作为分隔符? 最佳答案 接受的答案将slurp文件,这可能是大文本文件的问题。更好的解决方案是IO.foreach.它是惯用的,将按字符流式传输文件:File.foreach(filename,""){|string|putsstring}包含“thisisanexample”结果的
我正在尝试使用boilerpipe来自JRuby。我看过guide从JRuby调用Java,并成功地将它与另一个Java包一起使用,但无法弄清楚为什么同样的东西不能用于boilerpipe。我正在尝试基本上从JRuby中执行与此Java等效的操作:URLurl=newURL("http://www.example.com/some-location/index.html");Stringtext=ArticleExtractor.INSTANCE.getText(url);在JRuby中试过这个:require'java'url=java.net.URL.new("http://www
我需要一些关于TDD概念的帮助。假设我有以下代码defexecute(command)casecommandwhen"c"create_new_characterwhen"i"display_inventoryendenddefcreate_new_character#dostufftocreatenewcharacterenddefdisplay_inventory#dostufftodisplayinventoryend现在我不确定要为什么编写单元测试。如果我为execute方法编写单元测试,那不是几乎涵盖了我对create_new_character和display_invent
我只想对我一直在思考的这个问题有其他意见,例如我有classuser_controller和classuserclassUserattr_accessor:name,:usernameendclassUserController//dosomethingaboutanythingaboutusersend问题是我的User类中是否应该有逻辑user=User.newuser.do_something(user1)oritshouldbeuser_controller=UserController.newuser_controller.do_something(user1,user2)我
在应用开发中,有时候我们需要获取系统的设备信息,用于数据上报和行为分析。那在鸿蒙系统中,我们应该怎么去获取设备的系统信息呢,比如说获取手机的系统版本号、手机的制造商、手机型号等数据。1、获取方式这里分为两种情况,一种是设备信息的获取,一种是系统信息的获取。1.1、获取设备信息获取设备信息,鸿蒙的SDK包为我们提供了DeviceInfo类,通过该类的一些静态方法,可以获取设备信息,DeviceInfo类的包路径为:ohos.system.DeviceInfo.具体的方法如下:ModifierandTypeMethodDescriptionstatic StringgetAbiList()Obt