如何在android中使用SOAP从magento框架获取数据?

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

我想在android中使用SOAP从magento框架获取数据。 参考这个网址:http://inchoo.net/dev-talk/android-development/magento-v2-api-soap-android/

但是我无法获取会话ID

这是我的代码

 import java.io.IOException;
 import org.ksoap2.SoapEnvelope;
 import org.ksoap2.serialization.SoapObject;
 import org.ksoap2.serialization.SoapSerializationEnvelope;
 import org.ksoap2.transport.AndroidHttpTransport;
 import org.xmlpull.v1.XmlPullParserException;

 import android.app.Activity;
 import android.os.Bundle;
 import android.util.Log;

public class SoapTestActivity extends Activity {
         /** Called when the activity is first created. */

      private static final String NAMESPACE = "urn:Magento";
      private static final String Method_Name="login";
      private static final String URL ="http://localhost/onlinesales/api/v2_soap/?wsdl=1";
      private static final String SOAP_ACTION ="urn:Magento/login";

 @Override
 public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    try {
      SoapObject request = new SoapObject(NAMESPACE,Method_Name);

        request.addProperty("username", "test");
        request.addProperty("apiKey", "test123");
        SoapSerializationEnvelope envelopes = new SoapSerializationEnvelope(
                SoapEnvelope.VER11);

        envelopes.dotNet = false;
        envelopes.xsd = SoapSerializationEnvelope.XSD;
        envelopes.enc = SoapSerializationEnvelope.ENC;


       envelopes.setOutputSoapObject(request);

        AndroidHttpTransport androidHttpTransport = new AndroidHttpTransport(URL);
        androidHttpTransport.debug =true;
        androidHttpTransport.call(SOAP_ACTION, envelopes);//Getting the Exception here
        Object result = envelopes.getResponse();

        Log.d("sessionId", result.toString());

        //making call to get list of customers

        String sessionId = result.toString();

        request = new SoapObject(NAMESPACE,"customerCustomerList");
        request.addProperty("sessionId",sessionId );

        envelopes.setOutputSoapObject(request);
        androidHttpTransport.call(SOAP_ACTION, envelopes);

        result = envelopes.getResponse();

        Log.d("Customer List", result.toString());

    } catch (Exception e) {
        e.printStackTrace();
    }
 }
 } 

我遇到了这样的错误

02-12 14:05:33.282: W/ResourceType(455): Attempt to retrieve bag 0x7f060000 which is invalid or in a cycle.
02-12 14:05:35.173: W/System.err(455): org.xmlpull.v1.XmlPullParserException: unexpected type (position:END_DOCUMENT null@1:0 in java.io.InputStreamReader@40527be0) 
02-12 14:05:35.173: W/System.err(455):  at org.kxml2.io.KXmlParser.exception(KXmlParser.java:273)
02-12 14:05:35.173: W/System.err(455):  at org.kxml2.io.KXmlParser.nextTag(KXmlParser.java:1413)
02-12 14:05:35.183: W/System.err(455):  at org.ksoap2.SoapEnvelope.parse(SoapEnvelope.java:126)
02-12 14:05:35.183: W/System.err(455):  at org.ksoap2.transport.Transport.parseResponse(Transport.java:63)
02-12 14:05:35.183: W/System.err(455):  at org.ksoap2.transport.HttpTransportSE.call(HttpTransportSE.java:100)
02-12 14:05:35.183: W/System.err(455):  at com.bugtreat.megento.SoapTestActivity.onCreate(SoapTestActivity.java:43)
02-12 14:05:35.183: W/System.err(455):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
02-12 14:05:35.183: W/System.err(455):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
02-12 14:05:35.192: W/System.err(455):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
02-12 14:05:35.192: W/System.err(455):  at android.app.ActivityThread.access$1500(ActivityThread.java:117)
02-12 14:05:35.192: W/System.err(455):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
02-12 14:05:35.192: W/System.err(455):  at android.os.Handler.dispatchMessage(Handler.java:99)
02-12 14:05:35.192: W/System.err(455):  at android.os.Looper.loop(Looper.java:123)
02-12 14:05:35.204: W/System.err(455):  at android.app.ActivityThread.main(ActivityThread.java:3683)
02-12 14:05:35.204: W/System.err(455):  at java.lang.reflect.Method.invokeNative(Native Method)
02-12 14:05:35.204: W/System.err(455):  at java.lang.reflect.Method.invoke(Method.java:507)
02-12 14:05:35.212: W/System.err(455):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
02-12 14:05:35.212: W/System.err(455):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
02-12 14:05:35.212: W/System.err(455):  at dalvik.system.NativeStart.main(Native Method)

请帮助我...

php android magento soap magento-soap-api
2个回答
0
投票

网址不正确。 代替 URL =“http://localhost/onlinesales/api/v2_soap/?wsdl=1” 和 URL =“http://localhost/onlinesales/api/v2_soap”


0
投票

您尝试使用计算机的 IP 地址,而不是您尝试使用的 localhost 。并且使用 SOAP 版本 1 ,可能您的 SOAP 版本 2 不起作用。 用这个

 private static final String URL ="http://your machine IP address/onlinesales/api/soap";

以及 SOAP 操作

private static final String SOAP_ACTION ="login";

这会对你有所帮助,因为我得到了结果,谢谢

© www.soinside.com 2019 - 2024. All rights reserved.