Android应用程序通过soap webservice连接到magento

时间:2013-11-25 07:16:17

标签: android web-services magento soap

我是一名Android应用开发者。我想用肥皂网服务将我的机器人与magento连接起来。

我运行此代码,但我无法获得会话ID。 所以,请帮助我如何获得会话ID,如果此代码有问题,请更正此。

  public class MainActivity extends Activity 
 {
private static final String SOAP_ACTION ="urn:Mage_Api_Model_Server_HandlerAction";
private static final String NAMESPACE = "urn:Magento";
private  static final String Method_Name="login";
private static final String URL ="http://abcd.com/api/v2_soap/";

TextView tv;
Context mContext;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    tv=(TextView)findViewById(R.id.tv);

}

public class getData extends AsyncTask<String, Void, String>{

    @Override
    protected String doInBackground(String... params) {
        // TODO Auto-generated method stub

         SoapObject request = new SoapObject(NAMESPACE,Method_Name);
            request.addProperty("username", "suman");
            request.addProperty("apiKey", "suman123");

         SoapSerializationEnvelope envelopes = new SoapSerializationEnvelope(
                    SoapEnvelope.VER11);
                    envelopes.dotNet = false;
                    envelopes.setOutputSoapObject(request);

                    try 
                    {
                        AndroidHttpTransport androidHttpTransport = new AndroidHttpTransport(URL);
                        androidHttpTransport.debug =true;
                        androidHttpTransport.call(SOAP_ACTION, envelopes);//Getting the Exception here
                        SoapPrimitive resultString=(SoapPrimitive)envelopes.getResponse();
                        tv.setText("Status "+resultString);
                        new AlertDialog.Builder(mContext).setMessage(""+resultString).show();
                    } 
                        catch (Exception e) {e.printStackTrace();
                        new AlertDialog.Builder(mContext).setMessage(""+e.toString()).show();
                    }


        return null;
    }

}

}

3 个答案:

答案 0 :(得分:0)

我使用此代码获取sessionID:

private static final String NAMESPACE = "urn:Magento";
private static final String URL = "http://your_domain/index.php/api/v2_soap/";

SoapSerializationEnvelope env;
HttpTransportSE androidHttpTransport;
SoapObject request;
String sessionId = "";
Object result;

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", "xxxxx");
        request.addProperty("apiKey", "123456");
        env.setOutputSoapObject(request);
        androidHttpTransport = new HttpTransportSE(URL);
        androidHttpTransport.debug = true;
        (new MarshalHashtable()).register(env);
        androidHttpTransport.call("", env);
        result = env.getResponse();
        Log.d("sessionId", result.toString());
        sessionId = result.toString();

答案 1 :(得分:0)

v2_soap无法在您的magento框架中运行。因此,最好使用版本为1的SOAP。

使用此

cam = cam*expm(vc*dt)

并为您的Android应用程序项目提供适当的权限。喜欢上网,如果你使用的是Wifi状态,还需要bluethooth,网络状态,

如果您使用的是Android工作室,请将此代码粘贴到您的MainActivity

http://abcd.com/api/soap/

答案 2 :(得分:0)

试试这堂课......它为我工作......我正在使用 Android ksoap2 库......

private static final String NAMESPACE = "urn:Magento";
private static final String URL = "http://localhost:8888/Magento/index.php/api/v2_soap/";

private class magentoUserlogin extends AsyncTask<String, String, String>
{

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
    }

    @Override
    protected String doInBackground(String... params) {
        Object result= null;
        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", "<urSOAP/XML username>");
            request.addProperty("apiKey", "<yourAPIKey>");

            env.setOutputSoapObject(request);

            HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);

            androidHttpTransport.call("", env);
            result = env.getResponse();

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

        }

        return result.toString();
    }

    @Override
    protected void onPostExecute(String s) {
        super.onPostExecute(s);
        Log.d("sessionId", s.toString());
        Toast.makeText(MainActivity.this, "Session ID: "+s, Toast.LENGTH_LONG).show();
    }
}