androidmagentoksoap2android-ksoap2magento-soap-api

Creating a new Order in Magento using KSOAP from Android


I've been trying to use SOAP API of Magento to create a new order, since REST API doesn't seem to be allowing to create new orders.

The code that I am using is following ( it currently just tries to login and retrieve the session ID ), but I keep getting either of the following two errors:

I am using KSOAP2 Library to do this, and have followed Inchoo's article to try and do this : http://inchoo.net/dev-talk/android-development/magento-v2-api-soap-android/ I have been a REST person all my life, so don't really understand what's going wrong and where.

Has anybody got any pointers to any working example etc?

    protected int login() {

        String NAMESPACE = "urn:Magento";
        String URL = "http://www.example.com/api/v2_soap/?wsdl";

        SharedPreferences sharedPref = getActivity().getSharedPreferences("tokens",
                getActivity().MODE_PRIVATE);

        SoapObject request = new SoapObject(NAMESPACE, "login");

        request.addProperty("username", sharedPref.getString("consumerKey", ""));
        request.addProperty("apiKey", sharedPref.getString("consumerSecret", ""));

        SoapSerializationEnvelope env = new SoapSerializationEnvelope(SoapEnvelope.VER11);
        env.dotNet = false;
        env.xsd = SoapSerializationEnvelope.XSD;
        env.enc = SoapSerializationEnvelope.ENC;
        env.setOutputSoapObject(request);

        HttpTransportSE se = new HttpTransportSE(URL);
        se.debug = true;
        try {
            se.call("", env);
            Object result = env.getResponse();
            Log.d("MMD Order Response", result.toString());
        } catch (IOException e) {
            e.printStackTrace();
            log.d("Req: "+se.requestDump.toString());
            errorMessage = e.toString();
            return -1;
        } catch (XmlPullParserException e) {
            e.printStackTrace();
            errorMessage = e.toString();
            return -1;
        }
        return 1;
    }

The error that I am receiving is:

java.net.SocketTimeoutException
java.net.PlainSocketImpl.read(PlainSocketImpl.java:491)
java.net.PlainSocketImpl.access$000(PlainSocketImpl.java:46)
java.net.PlainSocketImpl$PlainSocketInputStream.read(PlainSocketImpl.java:240)
java.io.InputStream.read(InputStream.java:163)
java.io.BufferedInputStream.fillbuf(BufferedInputStream.java:142)
java.io.BufferedInputStream.read(BufferedInputStream.java:227)
libcore.io.Streams.readAsciiLine(Streams.java:201)
libcore.net.http.HttpEngine.readResponseHeaders(HttpEngine.java:560)
libcore.net.http.HttpEngine.readResponse(HttpEngine.java:813)
libcore.net.http.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:274)
libcore.net.http.HttpURLConnectionImpl.getResponseCode(HttpURLConnectionImpl.java:486)
org.ksoap2.transport.ServiceConnectionSE.getResponseCode(ServiceConnectionSE.java:103)
org.ksoap2.transport.HttpTransportSE.call(HttpTransportSE.java:197)
org.ksoap2.transport.HttpTransportSE.call(HttpTransportSE.java:118)
org.ksoap2.transport.HttpTransportSE.call(HttpTransportSE.java:113)
com.example.uifragments.Fragment_CartBasketView$CreateNewOrder.doInBackground(Fragment_CartBasketView.java:202)
com.example.uifragments.Fragment_CartBasketView$CreateNewOrder.doInBackground(Fragment_CartBasketView.java:145)
android.os.AsyncTask$2.call(AsyncTask.java:287)
java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
java.util.concurrent.FutureTask.run(FutureTask.java:137)
android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
java.lang.Thread.run(Thread.java:856)

Another error that I receive is the following:

org.xmlpull.v1.XmlPullParserException: expected: START_TAG {http://schemas.xmlsoap.org/soap/envelope/}Envelope (position:START_TAG <html lang='en'>@2:17 in java.io.InputStreamReader@5381ee0c)
org.kxml2.io.KXmlParser.require(KXmlParser.java:2046)
org.ksoap2.SoapEnvelope.parse(SoapEnvelope.java:128)
org.ksoap2.transport.Transport.parseResponse(Transport.java:118)
org.ksoap2.transport.HttpTransportSE.call(HttpTransportSE.java:275)
org.ksoap2.transport.HttpTransportSE.call(HttpTransportSE.java:118)
org.ksoap2.transport.HttpTransportSE.call(HttpTransportSE.java:113)
com.example.uifragments.Fragment_CartBasketView$CreateNewOrder.doInBackground(Fragment_CartBasketView.java:193)
com.example.uifragments.Fragment_CartBasketView$CreateNewOrder.doInBackground(Fragment_CartBasketView.java:145)
android.os.AsyncTask$2.call(AsyncTask.java:287)
java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
java.util.concurrent.FutureTask.run(FutureTask.java:137)
android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
java.lang.Thread.run(Thread.java:856)

I tried printing the error dump, which says the following is being sent

`<v:Envelope xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns:d="http://www.w3.org/2001/XMLSchema" xmlns:c="http://schemas.xmlsoap.org/soap/encoding/" xmlns:v="http://schemas.xmlsoap.org/soap/envelope/"><v:Header /><v:Body><n0:login id="o0" c:root="1" xmlns:n0="urn:Magento"><username i:type="d:string">someusername</username><apiKey i:type="d:string">somePass</apiKey></n0:login></v:Body></v:Envelope>`

Any help on where am I missing?
I've been pulling my hair out debugging this as well, since last 5-6 days! Much thanks in advance!


Solution

  • This is the code I m using. It works perfect for me.. I think the url you are using might be the problem.

    public void getMethod() {

        try {
            SoapObject request = new SoapObject(NAMESPACE, "login");
    
            request.addProperty("username", "xxxx");
            request.addProperty("apiKey", "xxxxx");
    
            SoapSerializationEnvelope env = new SoapSerializationEnvelope(SoapEnvelope.VER11);
    
            env.dotNet = false;
            env.xsd = SoapSerializationEnvelope.XSD;
            env.enc = SoapSerializationEnvelope.ENC;
    
            env.setOutputSoapObject(request);
    
            HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
            androidHttpTransport.debug=true;
    
    
    
            try {
    
                androidHttpTransport.call("", env);
                Object res = env.getResponse();
                ssid=res.toString();
                Log.v("sessionId", ssid);
    
            }catch (Exception e){
                ss=e.getMessage();
                Log.v("Soapfault",ss);
            }            
    
        } catch (Exception e) {
    
            Log.e("Error",e.toString());
        }