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

问题描述 投票:0回答:4

我一直在尝试使用 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 天开始,我也一直在绞尽脑汁地调试这个问题! 非常感谢!

android magento ksoap2 android-ksoap2 magento-soap-api
4个回答
0
投票

这是我正在使用的代码。它非常适合我.. 我认为您使用的网址可能有问题。

公共无效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());
    }

0
投票

尝试更换 字符串 URL =“http://www.example.com/api/v2_soap/?wsdl”;

字符串 URL =“http://www.example.com/api/v2_soap”;


0
投票

命名空间应该有一个尾随

/

尝试将其更改为

String NAMESPACE = "urn:Magento/";


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);
© www.soinside.com 2019 - 2024. All rights reserved.