I used ksoap2 api as reference in my android application to store data from my android application to remote SQL server database. The idea is to save user data which are the information collected to build the user profile. I used this inside doInBackground()
method in AsyncTask
as below :
SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME1);
request.addProperty("userName",username.getText().toString());
request.addProperty("eamil",email.getText().toString());
request.addProperty("gender",gender.getSelectedItem().toString());
request.addProperty("country",country.getSelectedItem().toString());
request.addProperty("about",about.getText().toString() );
request.addProperty("pic",byteArray);
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.setOutputSoapObject(request);
envelope.dotNet = true;
try {
HttpTransportSE androidHttpTransport = new HttpTransportSE(URL,20000);
androidHttpTransport.call(SOAP_ACTION1, envelope);
if (envelope.bodyIn instanceof SoapFault) {
String str= ((SoapFault) envelope.bodyIn).faultstring;
Log.i("fault", str);
} else {
SoapObject result = (SoapObject)envelope.bodyIn;
if(result != null)
{
message=result.getProperty(0).toString();
}
}
} catch (Exception e) {
e.printStackTrace();
}
return message;
The problem is that when I added request.addProperty("pic",byteArray);
I received an error that state that the Ksoap2 can not be serialized but when I change the type of byteArray
from type byte[ ]
to string
the request executed properly and the data saved in my Database. Here is snipshote from my webservice
Public Function AddUser(userName As String, email As String, gender As String, country As String, about As String, pic As Byte()) as String
// Some code to add data to databae
Return "you are done"
End Function
any help regarding this issue will be completely appreciated
regards
I think I figure out how to solve my problem stated above and that will be as followed:
Instead of Sending a byte[ ] to web service I changed my mind to send string which built as below:
Bitmap selectedImage = BitmapFactory.decodeFile(filePath);
ByteArrayOutputStream stream = new ByteArrayOutputStream();
selectedImage.compress(Bitmap.CompressFormat.JPEG, 100, stream);
byte[] byteArray = stream.toByteArray();
String strBase64=Base64.encodeToString(byteArray, 0);
and then I send strBase64
to my webservice as string using the request.addProperty("pic",strBase64);
then to retrieve that string and make it as picture again I just retrive the string from my remote database and then do as below:
byte[] decodedString = Base64.decode(strBase64, Base64.DEFAULT);
Bitmap decodedByte = BitmapFactory.decodeByteArray(decodedString, 0, decodedString.length);
image.setImageBitmap(decodedByte);
where strBase64
is the string that I retrieved from remote database.