skip to main |
skip to sidebar
RSS Feeds
Nope. nothing .. just some random thoughts come to my mind ...
Nope. nothing .. just some random thoughts come to my mind ...
6:47 PM | Saturday, February 4, 2012
Posted by harshadura
Hi Guys!
After lot of effort I have completed one of the tutorials regarding KSOAP Web Services with Android. :D
So I would like to describe how I have solved the things for any case some one will get the use and well for my future reference as well.
package com.pxr.tutorial.soap.weather; import org.ksoap2.SoapEnvelope; import org.ksoap2.serialization.SoapObject; import org.ksoap2.serialization.SoapSerializationEnvelope; import org.ksoap2.transport.HttpTransportSE; import android.app.Activity; import android.os.Bundle; import android.widget.TextView; public class Main extends Activity { private static String SOAP_ACTION = "http://tempuri.org/HelloWorld"; private static String NAMESPACE = "http://tempuri.org/"; private static String METHOD_NAME = "HelloWorld"; private static String URL = "http://bimbim.in/Sample/TestService.asmx?WSDL"; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); //Initialize soap request + add parameters SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME); request.addProperty("Parameter","Value"); SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11); envelope.setOutputSoapObject(request); // Make the soap call. HttpTransportSE androidHttpTransport = new HttpTransportSE(URL); try { //this is the actual part that will call the webservice androidHttpTransport.call(SOAP_ACTION, envelope); } catch (Exception e) { e.printStackTrace(); } // Get the SoapResult from the envelope body. SoapObject result = (SoapObject)envelope.bodyIn; if(result != null){ TextView t = (TextView)this.findViewById(R.id.resultbox); t.setText("SOAP response:\n\n" + result.getProperty(0).toString()); } } }
package com.harshadura.img_wsdl; import java.io.File; import java.io.FileOutputStream; import java.io.OutputStream; import org.ksoap2.SoapEnvelope; import org.ksoap2.serialization.SoapObject; import org.ksoap2.serialization.SoapSerializationEnvelope; import org.ksoap2.transport.HttpTransportSE; import android.app.Activity; import android.app.ProgressDialog; import android.content.Intent; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.os.Bundle; import android.os.Environment; import android.os.Handler; import android.util.Base64; import android.util.Log; import android.view.View; import android.widget.Button; import android.widget.ImageView; public class Main extends Activity { ImageView image; String extStorageDirectory; Bitmap decodedByte; private int iterator = 0; Button btnStartProgress; ProgressDialog progressBar; private int progressBarStatus = 0; private Handler progressBarHandler = new Handler(); private static String SOAP_ACTION = "http://tempuri.org/TestMethod"; private static String NAMESPACE = "http://tempuri.org/"; private static String METHOD_NAME = "TestMethod"; private static String URL = "http://test.org/ws/testService.asmx?WSDL"; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); Button buttonDownload = (Button) findViewById(R.id.downloadImages); Button buttonDisplay = (Button) findViewById(R.id.displayImages); buttonDownload.setOnClickListener(buttonSaveOnClickListener); buttonDisplay.setOnClickListener(buttonDisplayClickListener); createDirIfNotExists("MyImages"); extStorageDirectory = Environment.getExternalStorageDirectory() .toString() + "/MyImages/"; Log.v("path", extStorageDirectory); } public int doSomeTasks() { String[] imageIDs = { "123", "124", "125" }; while (iterator < imageIDs.length) { getEncodedImageFromService(imageIDs[iterator]); switch (iterator) { case 1: return 10; case 2: return 20; case 3: return 30; case 4: return 40; case 5: return 50; case 6: return 60; case 7: return 70; case 8: return 80; case 9: return 90; case 10: return 100; } } return 100; } public static boolean createDirIfNotExists(String path) { boolean ret = true; File file = new File(Environment.getExternalStorageDirectory(), path); if (!file.exists()) { if (!file.mkdirs()) { Log.e("TravellerLog :: ", "Problem creating Image folder"); ret = false; } } return ret; } Button.OnClickListener buttonDisplayClickListener = new Button.OnClickListener() { @Override public void onClick(View v) { Intent intent = new Intent(Main.this, ImageGallery.class); startActivity(intent); } }; Button.OnClickListener buttonSaveOnClickListener = new Button.OnClickListener() { @Override public void onClick(View v) { progressBar = new ProgressDialog(v.getContext()); progressBar.setCancelable(true); progressBar.setMessage("Retrieving Data..."); progressBar.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); progressBar.setProgress(0); progressBar.setMax(100); progressBar.show(); progressBarStatus = 0; new Thread(new Runnable() { public void run() { while (progressBarStatus < 100) { progressBarStatus = doSomeTasks(); try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } progressBarHandler.post(new Runnable() { public void run() { progressBar.setProgress(progressBarStatus); } }); } if (progressBarStatus >= 100) { try { Thread.sleep(2000); } catch (InterruptedException e) { e.printStackTrace(); } iterator = 0; progressBar.dismiss(); } } }).start(); } }; public void getEncodedImageFromService(String imageID) { SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME); request.addProperty("ImageId", imageID); SoapSerializationEnvelope envelope = new SoapSerializationEnvelope( SoapEnvelope.VER11); envelope.dotNet = true; envelope.setOutputSoapObject(request); HttpTransportSE androidHttpTransport = new HttpTransportSE(URL); try { androidHttpTransport.call(SOAP_ACTION, envelope); } catch (Exception e) { e.printStackTrace(); } SoapObject result; result = (SoapObject) envelope.bodyIn; if (result != null) { String encodedImage = result.getProperty(0).toString(); Log.v("TAG", encodedImage); byte[] decodedString = Base64.decode(encodedImage, Base64.DEFAULT); decodedByte = BitmapFactory.decodeByteArray(decodedString, 0, decodedString.length); SaveToSDCard(); } } public void SaveToSDCard() { String imagename = "image" + (iterator + 1) + ".png"; OutputStream outStream = null; File file = new File(extStorageDirectory, imagename); try { outStream = new FileOutputStream(file); decodedByte.compress(Bitmap.CompressFormat.PNG, 100, outStream); outStream.flush(); outStream.close(); } catch (Exception e) { e.printStackTrace(); } iterator++; } }
May 1, 2013 at 12:55 PM
Thanx for uploading,when i run this code its give me a logcat error "problem creating image folder" plz some body help.
May 1, 2013 at 1:09 PM
probably you may not given permission to access the sd card.
Add to manifest.xml
>>> android.permission.WRITE_EXTERNAL_STORAGE