我是一名 Android 应用程序开发人员。我想通过肥皂网络服务将我的 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;
}
}
}
我使用此代码获取 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();
v2_soap 无法在您的 magento 框架中工作。所以你最好选择版本 1 的 SOAP。
用这个
http://abcd.com/api/soap/
并为您的 Android 应用程序项目授予适当的权限。比如互联网,如果你正在使用Wifi状态,还需要蓝牙,网络状态,
如果您使用的是 android studio ,请将此代码粘贴到您的 MainActivity
StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
.detectDiskReads()
.detectDiskWrites()
.detectNetwork() // or .detectAll() for all detectable problems
.penaltyLog()
.build());
StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
.detectLeakedSqlLiteObjects()
.detectLeakedClosableObjects()
.penaltyLog()
.penaltyDeath()
.build());
尝试这个课程...它对我有用...我正在使用 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();
}
}