Monday, December 24, 2007

Upload image using J2ME and KSOAP

I have used KSoap to upload photo from my mobile phone to my web server using J2ME..

Here, the UploadPhoto class calls the PaymentInfo function which is in the server side and passing parameters as soapobjects like savePaymentInfo and PaymentInfo which is send as a string and the image which is uploading is converted to Base64String which can be converted again to image in the server side...

To use this function, you have to download ksoap jar file and add it to the project library.

Here is the code from the client side i.e. mobile application

class UploadPhoto
{
try
{
SoapObject savePaymentInfo = new SoapObject("http://tempuri.org/", "SavePaymentInfo");

SoapObject PaymentInfo = new SoapObject("http://tempuri.org/", "PaymentInfo");

PaymentInfo.addProperty("AddLocation", addLocation);
PaymentInfo.addProperty("CreditCardId", "12345");
PaymentInfo.addProperty("Msisdn", "11111");

int length = new String(midlet.postCardString).length();
String stringToSend = new String(midlet.postCardString).substring(0,length-2);

PaymentInfo.addProperty("PostCards", stringToSend);
PaymentInfo.addProperty("ResizePhoto", resizePhototoPhone);
PaymentInfo.addProperty("SecurityCode", "5678");
PaymentInfo.addProperty("UploadPhotoServer", uploadPhototoServer);
PaymentInfo.addProperty("SendPhotoMyHome", sendPhototoPC);

if("true".compareTo(domesticPostcard) == 0)
{
PaymentInfo.addProperty("PostCardType", "Domestic");
}
else
{
PaymentInfo.addProperty("PostCardType", "InterNational");
}

savePaymentInfo.addProperty("paymentInfo", PaymentInfo);

SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);

envelope.bodyOut = savePaymentInfo;
envelope.dotNet = true;
envelope.encodingStyle = SoapSerializationEnvelope.XSD;

HttpTransport httpTransport = new HttpTransport("http://url.asmx");
httpTransport.debug = true;

httpTransport.call("http://tempuri.org/SavePaymentInfo", envelope);

SoapObject result = (SoapObject)envelope.bodyIn;
String res = result.getProperty("SavePaymentInfoResult").toString();

if( res.equals("true") )
{
yesNoAlertMessageType = "PostCard(s)";
textBox.setString("");

deleteSendData();
displayreadfileCanvas = true;
httpTransport.reset();

midlet.displayGaugeAlert = false;
midlet.stopProcess();
repaint();

midlet.displayYesNoAlert = true;
midlet.errorMessage = "Sent Successfully";
repaint();
}

else if( res.equals("false"))
{
yesNoAlertMessageType = "PostCard(s)";
textBox.setString("");

displayreadfileCanvas = true;
httpTransport.reset();
midlet.displayGaugeAlert = false;
midlet.stopProcess();
repaint();

midlet.displayYesNoAlert = true;
midlet.errorMessage = "Sending Failed";
repaint();
}
}

catch (IOException ex)
{
System.out.println(ex.getMessage());
midlet.displayErrAlert = true;
midlet.errorMessage = "Server does not Exist";
repaint();
displayreadfileCanvas = true;

}
catch (XmlPullParserException ex)
{
System.out.println(ex.getMessage());
midlet.displayGaugeAlert = false;
repaint();
midlet.displayErrAlert = true;
midlet.errorMessage = "Server does not Exist";
repaint();
displayreadfileCanvas = true;

}
catch(Exception ex)
{
System.out.println(ex.getMessage());
midlet.displayGaugeAlert = false;
repaint();

midlet.displayErrAlert = true;
midlet.errorMessage = "Server does not Exist";
repaint();
displayreadfileCanvas = true;
}
};

2 comments:

shirou said...
This comment has been removed by the author.
shirou said...

thaks for the share, where i can find ksoap jar file . thanks b4