使用Android中的KSOAP在Magento中创建新订单

时间:2014-12-18 20:13:39

标签: android magento soap ksoap2 android-ksoap2

我一直在尝试使用Magento的SOAP API创建新订单,因为REST API似乎不允许创建新订单。

我正在使用的代码如下(它目前只是尝试登录并检索会话ID),但我一直收到以下两个错误之一:

  • XMLPullParseException
  • SocketTimeoutException

我正在使用KSOAP2库来执行此操作,并且已按照Inchoo的文章尝试执行此操作: http://inchoo.net/dev-talk/android-development/magento-v2-api-soap-android/ 我一生都是一个REST人,所以不要真正理解出了什么问题以及在哪里。

有没有人对任何工作示例有任何指示?

    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;
    }

我收到的错误是:

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)

我收到的另一个错误如下:

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)

我尝试打印错误转储,表示正在发送以下内容

`<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>`

对于我遗失的地方有任何帮助吗? 自从过去5-6天以来,我一直在调整这个问题! 非常感谢提前!

4 个答案:

答案 0 :(得分:0)

尝试更换 String URL =&#34; http://www.example.com/api/v2_soap/?wsdl&#34 ;;

字符串网址=&#34; http://www.example.com/api/v2_soap&#34 ;;

答案 1 :(得分:0)

命名空间应该有一个尾随/

尝试将其更改为     String NAMESPACE = "urn:Magento/";

答案 2 :(得分:0)

这是我使用的代码。它对我来说很完美.. 我认为您使用的网址可能是问题所在。

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());
    }

答案 3 :(得分:0)

使用"http://www.example.com/api/v2_soap"代替"http://www.example.com/api/v2_soap/?wsdl"

并使用

*// int **timeout** = TimeOutValueInInteger* 

HttpTransportSE androidHttpTransport = new HttpTransportSE(URL, **timeout**);

而不是

HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);