Foren

LRUploadData to send a png in custom Liferay method

thumbnail
Genar Codina, geändert vor 7 Jahren.

LRUploadData to send a png in custom Liferay method

Junior Member Beiträge: 28 Beitrittsdatum: 18.04.16 Neueste Beiträge
I have a Liferay Custom method whose signature in Objective-C is:

- (NSNumber *)setImageWithCompanyId:(long long)companyId userId:(long long)userId documentId:(long long)documentId image:(LRUploadData *)image error:(NSError **)error;


This custom Liferay method has been created using the command:
./gradlew createModule -P=all
../../gradlew zip


On the other hand, I have captured an image from the camera and that image is stored in an instance of an UIImage; that is:

var image:UIImage
// then I fill the image with the photo taken from the camera
let imageData = UIImagePNGRepresentation(image) // This method converts my image to NSData
// Note: "file.png" is only a name, not a real file stored in the device. The photo is already in the "image" variable
let uploadData:LRUploadData = LRUploadData(data: image, fileName: "file.png", mimeType: "image/png", progressDelegate: nil)
let myService= LRMyService_v62(session: self.session)  // self.session contains a valid session created previously
let result:NSNumber = try myService.setImageWithCompanyId(companyId, userId:userId, documentId:documentId, image:uploadData)


However, when the method
myService.setImageWithCompanyId
is called, then the app crashes.

And the crash complains about:
Terminating app due to uncaught exception 'Upload Exception', reason: 'Set a callback to the session before uploading files'
thumbnail
Jose M. Navarro, geändert vor 7 Jahren.

RE: LRUploadData to send a png in custom Liferay method (Antwort)

Regular Member Beiträge: 138 Beitrittsdatum: 24.01.14 Neueste Beiträge
Genar Codina:

And the crash complains about:
Terminating app due to uncaught exception 'Upload Exception', reason: 'Set a callback to the session before uploading files'


Yes, when you upload files to the server you need to specify a delegate in the LRUploadData object. The delegate should conform LRFileProgressDelegate and there you will receive the upload progress

Hope it helps
thumbnail
Alessandro Maroldi, geändert vor 7 Jahren.

RE: LRUploadData to send a png in custom Liferay method

New Member Beiträge: 5 Beitrittsdatum: 20.03.15 Neueste Beiträge
Hi,

I'm using LRUploadData for uploading a PDF to LR custom service ( field type: java.io.File ). Always return this error
'Invalid type in JSON write (LRUploadData)'

next the code where I create the LRUploadData Object

        NSData* pdfData = [NSData dataWithContentsOfFile:_pdfPath];
        
        LRUploadData *uploadData = [[LRUploadData alloc] initWithData:pdfData
                                                             fileName:@"audioplaceholder.png"
                                                             mimeType:@"image/png"
                                                     progressDelegate:self];


I debug a little bit and I found the error its generated by this line of code in LRHttpUtil.m
+ (NSArray *)post: (LRSession *)session commands: (NSArray *)commands


NSData *body = [NSJSONSerialization dataWithJSONObject:commands options:0 error:error];


Any suggestion?

Thanks a lot for your help
thumbnail
Alessandro Maroldi, geändert vor 7 Jahren.

RE: LRUploadData to send a png in custom Liferay method

New Member Beiträge: 5 Beitrittsdatum: 20.03.15 Neueste Beiträge
SOLVED

There's a problem in Custom SDK Mobile Service generation, if the first parameters is not a file (LRUploadData) the command is called with :
 [self.session invoke:_command error:error];

but it has to be called like
 [self.session upload:_command error:error];
thumbnail
Bruno Farache, geändert vor 7 Jahren.

RE: LRUploadData to send a png in custom Liferay method

Liferay Master Beiträge: 603 Beitrittsdatum: 14.05.07 Neueste Beiträge
How does the signature of the remote method looks like? The position of file arguments in the signature shouldn't matter.
thumbnail
Alessandro Maroldi, geändert vor 7 Jahren.

RE: LRUploadData to send a png in custom Liferay method

New Member Beiträge: 5 Beitrittsdatum: 20.03.15 Neueste Beiträge
Hi Bruno,

you're right the answer is not in the position of the parameters but it's in the return value, if I use a return value VOID or BOOL all the methods are called with invoke. Otherwise if there's a return object the variable invokeMethod it's used by the builder.

https://github.com/liferay/liferay-mobile-sdk/blob/master/builder/src/main/resources/templates/ios/m.vm [line 81 - 87]

There is any reason why you can't return VOID with an upload service?

Thanks a lot for your help.
thumbnail
Bruno Farache, geändert vor 7 Jahren.

RE: LRUploadData to send a png in custom Liferay method

Liferay Master Beiträge: 603 Beitrittsdatum: 14.05.07 Neueste Beiträge
Uploads are always asynchronous . The library we use for uploads (AFNetworking) doesn't allow synchronous requests when uploading files.

Because of that, the method returns right away and doesn't wait for the response, thus it should always be void.

Take a look at the test for upload. It sets itself as the session callback and the onSuccess method is called with the response once upload has finished.
thumbnail
Denis Signoretto, geändert vor 7 Jahren.

RE: LRUploadData to send a png in custom Liferay method

Expert Beiträge: 375 Beitrittsdatum: 21.04.09 Neueste Beiträge
Hi Bruno,

I'm helping Alessandro investigating the issue. There was a misunderstanding. When Alessandro wrote
If I use a return value VOID or BOOL all the methods are called with invoke


he was meaning that iOS SDK Builder always generate a service that use "invoke" when remote method has return value void (or boolean). The issue arise when there is a remote service that has a java.io.File as input parameter and returns void (or boolean). In this particular case SDK Builder (for iOS) genereates a service without the "upload method invocation" but still use the "Liferay invoke method".

The workaround we have found at the moment, is changing retrun value of custom JSONWS remote service to a value different from void (or boolean).

HTH invetigating the issue.

Thanks
Denis.

P.S. Is there a particular reason why iOS SDK Builder always use invoke method when the retrun value is void? (Take a look at the iOS template file https://github.com/liferay/liferay-mobile-sdk/blob/master/builder/src/main/resources/templates/ios/m.vm at lines [81 - 87])