JsonAPI for file Upload with Flutter

thumbnail
Yan Paing, modified 1 Year ago. Regular Member Posts: 130 Join Date: 3/11/10 Recent Posts

Dear All,

I am working on flutter calling liferay JsonAPI xxxxxServiceImpl.java Our code works for without file attachment, however if we attached file then it is fail and following errors are showing in log. Please suggest. Thank you

1. Unable to deserialize object error is showing in log if we use blue color highlighted line cmdSb.write('"file":$file'); 
ERROR [http-nio-8080-exec-30][JSONWebServiceServiceAction:126] Unable to deserialize object

2. Conversion failed error is showing if we use red color highlighted line cmdSb.write('"file":$filebyte'); 
ERROR [http-nio-8080-exec-31][JSONWebServiceServiceAction:126] Conversion failed: [255, 216, 255, 224, 0, 16, 74, 70, 73, 70, 0, 1, 1, 0, 0, 72, 0, 72, 0, 0, 255, 225, 4, 76, 69, 120, 105, 102, 0, 0, 77, 77, 0, 42, 0, 0, 0, 8, 0, 12, 1, 15..............

 

class JsonSendMessageService {

JsonSendMessageService();

static String jsonString = '[{"/abc.messagesend/';

static Future<dynamic> sendMessageToSchoolForSingleStudent({
    required int schoolId,
    required String title,
    required String messageBody,
    required File? file,
  }) async {

    final StringBuffer cmdSb = StringBuffer(jsonString);
    cmdSb.write('send-message":');
    cmdSb.write('{"schoolId":$schoolId,');
    cmdSb.write('"title":"$title",');
    cmdSb.write('"messageBody":"$messageBody",');
 

    cmdSb.write('"file":$file'); 

 

var fileByte = await file!.readAsBytes().then((value) {

return value.cast();

});

cmdSb.write('"file":$fileByte');

 

    cmdSb.write('}}]');

    return await SMPHttpHelper(starJsonRequest: cmdSb.toString()).getBody();
  }

}

 

class SMPHttpHelper {

SMPHttpHelper({required this.starJsonRequest});

 

String starJsonRequest;

 

Uri getSMPUrl() {

return Uri.https(smpServer, jsonInvoke);

}

 

Future<dynamic> getBody() async {

final String authenString = await SMPSession.getStringValue(userCredential);

final response = await http.post(

getSMPUrl(),

headers: {

'Content-Type': contentTypeJson,

'Charset': 'utf-8',

'Authorization': '$smpAuthorizationType$authenString',

},

body: starJsonRequest,

);

return await json.decode(response.body);

}

 

Future<int> getResponseStatus() async {

final String authenString = await SMPSession.getStringValue(userCredential);

final response = await http.post(

getSMPUrl(),

headers: {

'Content-Type': contentTypeJson,

'Charset': 'utf-8',

'Authorization': '$smpAuthorizationType$authenString',

},

body: starJsonRequest,

);

 

return response.statusCode;

}

thumbnail
Yan Paing, modified 1 Year ago. Regular Member Posts: 130 Join Date: 3/11/10 Recent Posts

Finally, successfully upload the file with flutter remote call to xxxxserviceImpl.java. Following is the code.

 

static Future<dynamic> sendMessageToSchoolForSingleStudent({

required int userId,

required String title,

required String messageBody,

required File? file,

}) async {

 

final String authenString = await SMPSession.getStringValue(userCredential);

 

var formData = FormData.fromMap(

{

'userId': '$userId',

'title': title,

'messageBody': messageBody,

'file': await MultipartFile.fromFile(file!.path),

},

);

Dio dio = Dio();

var response = await dio.post(

'https://yourserver/api/jsonws/customizedliferayapi.serviceimplapi/method-from-serviceimpljava',

data: formData,

options: Options(

headers: {

'Content-Type': contentTypeFile,

'Charset': 'utf-8',

'Authorization': '$smpAuthorizationType$authenString',

},

),

);