How to use K2Soap Library on android platform to consume Magento SOAP API v2.

While working one of project, we had to do some research on how to consume Magento soap api from android and found a very nice library K2soap, which allows us to connect with Magento soap api. We won’t go in to details about android programming and it is assumed you are an android programming expert. Basically article is about How to use K2Soap Library on android platform to consume Magento SOAP API v2.

If you had not gone through first article of this series, please go through for basic understanding.

Basic steps are as follows

  1. Download K2soap library from this link
  2. Include library jar file to your project
  3. Add library path to Build Path for your project
  4. Set permission.INTERNET in AndroidManifest.xml file.
  5. For purpose of article, add your code to main Activity’s OnCreate Method
  6. Below piece of code will just add required references and set up methods and class which will be executed when android app will run. readingData() method would be our main method under which we would be calling Magento webservice.
package com.example.hello; 
import org.ksoap2.SoapEnvelope;
import org.ksoap2.SoapFault;
import org.ksoap2.serialization.MarshalHashtable;
import org.ksoap2.serialization.PropertyInfo;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;
import org.xmlpull.v1.XmlPullParserException;
 
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Hashtable;
import java.util.List;
import java.util.Set;
 
import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ProgressBar;
 
public class MainActivity extends Activity {
 
private static final String NAMESPACE = "urn:Magento";
private static final String URL = "http://yourdomain.com/index.php/api/v2_soap";
private static final String METHOD_NAME = "shoppingCartProductAdd";
ProgressBar pBar;
 
@Override
public void onCreate(Bundle savedInstanceState) {
 
super.onCreate(savedInstanceState);
new AccessTask().execute();
}
 
class AccessTask extends AsyncTask<Void, Void, Void> {
@Override
protected void onPreExecute() {
//pBar.setVisibility(View.VISIBLE);
Log.e("onPreExecute",
"----------------onPreExecute-----------------");
}
 
@Override
protected Void doInBackground(Void... params) {
readingData();
return null;
}
 
@Override
protected void onPostExecute(Void unused) {
 
//pBar.setVisibility(View.INVISIBLE);
 
}
 
}
 
}

7. Below would be the stub for readingData method

public void readingData() {try {



} catch (Exception e) {

e.printStackTrace();

}

}

8. Now let us start by creating  SoapSerializationEnvelope

public void readingData() {try {
SoapSerializationEnvelope env = new  SoapSerializationEnvelope(SoapEnvelope.VER11);
 
env.dotNet = false;
env.xsd = SoapSerializationEnvelope.XSD;
env.enc = SoapSerializationEnvelope.ENC;
 
} catch (Exception e) {
e.printStackTrace();
}
 
}

9. Now we will create request object and assign it to above envelope. We will first make call tp “login” method from Magento API in order to authenticate and retrieve the sessionId.

10. We then will add request parameters to login method, and it’s can be done withaddProperty and then assign our request object to envelope.
After that, we need to call SOAP method and retrieve result.

public void readingData() {
try {
SoapSerializationEnvelope env = new  SoapSerializationEnvelope(SoapEnvelope.VER11);
 
env.dotNet = false;
env.xsd = SoapSerializationEnvelope.XSD;
env.enc = SoapSerializationEnvelope.ENC;
 
SoapObject request = new SoapObject(NAMESPACE, "login");
 
request.addProperty("username", "soapusername");
request.addProperty("apiKey", "soapapikey");
env.setOutputSoapObject(request);
HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
androidHttpTransport.call("", env);
Object result = env.getResponse();
Log.d("sessionId", result.toString());
String sessionId = result.toString();
} catch (Exception e) {
e.printStackTrace();
}
}

11. Please check then retrieved SessionId is logged in LogCat window or not. If you get SessionId then congrats your basic configuration is ready and you are able to connect to magento web service from your android application

12. Now after this, we can make actual service call method to perform all the supported operations. For this article we will call catalogCategoryAssignedProductsmethod which will return us list of products under a particular category id.

public void readingData() {try {
SoapSerializationEnvelope env = new  SoapSerializationEnvelope(SoapEnvelope.VER11);
 
env.dotNet = false;
env.xsd = SoapSerializationEnvelope.XSD;
env.enc = SoapSerializationEnvelope.ENC;
SoapObject request = new SoapObject(NAMESPACE, "login");
 
request.addProperty("username", "soapusername");
request.addProperty("apiKey", "soapapikey");
 
env.setOutputSoapObject(request);
 
HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
 
androidHttpTransport.call("", env);
Object result = env.getResponse();
Log.d("sessionId", result.toString());
 
String sessionId = result.toString();
 
SoapObject requestCart = new SoapObject(NAMESPACE, "catalogCategoryAssignedProducts");
requestCart.addProperty("sessionId", sessionId);
requestCart.addProperty("categoryId", 321);
env.setOutputSoapObject(requestCart);
androidHttpTransport.call("", env);
result = env.getResponse();
Log.d("List of Products", result.toString());
} catch (Exception e) {
e.printStackTrace();
}
}

13. if call is successful, it will return you list of products in form of array.

Hope this helps!!!! Happy Coding!!!!

(Visited 146 times, 1 visits today)