如何在 android 中的 svc(soap) 服务中发送请求 post 方法?
How to send request post method in svc(soap) service in android?
我不知道如何在 soap post 方法中发送请求 service.Please 有人帮我解决这个问题。
到目前为止,这是我的代码。
//Code
String NAMESPACE = "http://tempuri.org/";
String METHOD_NAME = "Login";
String SOAP_ACTION = "http://tempuri.org/Login";
String URL = "http://hostname/AllServices.svc/Login";
protected void soap() {
// TODO Auto-generated method stub
SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
// Set all input params
request.addProperty("sapcode", "3232323");
request.addProperty("password", "3232323");
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
SoapEnvelope.VER12);
// Enable the below property if consuming .Net service
envelope.dotNet = true;
envelope.encodingStyle = SoapEnvelope.ENC;
envelope.setAddAdornments(false);
envelope.implicitTypes = false;
envelope.setOutputSoapObject(request);
HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
try {
envelope.headerOut = new Element[1];
envelope.headerOut[0] = buildAuthHeader();
androidHttpTransport.call(SOAP_ACTION, envelope);
SoapObject response = (SoapObject) envelope.getResponse();
Log.i("response", response.getProperty(0).toString());
// response.getProperty(0).toString();
} catch (Exception e) {
e.printStackTrace();
}
}
private Element buildAuthHeader() {
Element h = new Element().createElement(NAMESPACE, "AuthHeader");
Element username = new Element().createElement(NAMESPACE, "User Name");
username.addChild(Node.TEXT, "tech");
h.addChild(Node.ELEMENT, username);
Element pass = new Element().createElement(NAMESPACE, "Password");
pass.addChild(Node.TEXT, "tech@001");
h.addChild(Node.ELEMENT, pass);
return h;
}
基本Header 身份验证:
来自 API 的每个方法调用都需要基本 header 身份验证
用户名:
密码:
方法类型:POST
Return类型是json格式。
try {
// Build JSON string
JSONStringer loginCredentials = new JSONStringer().object()
.key("sapcode").value(userNameSend).key("password")
.value(passwordSend).endObject();
JSONObject jsonObj = new ServerResponse(
Urlgenerator.loginCheck()).getJSONObjectfromURL(
RequestType.POST, loginCredentials);
if (jsonObj != null) {
JSONObject loginObj = jsonObj.getJSONObject("Login");
if (loginObj != null) {
String errorcode = loginObj.getString("errorcode");
final String result = loginObj.getString("result");
if (errorcode.equalsIgnoreCase("0")) {
Utils.updateSharedPreference("remember", "true");
Utils.updateSharedPreference("sapcode",
userNameSend);
startActivity(new Intent(LoginScreen.this,
HomeScreen.class));
finish();
} else {
runOnUiThread(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
ToastUtils.displayToast(
ToastTypeEnum.ALERT_TOAST, result,
Toast.LENGTH_SHORT);
}
});
}
}
}
pDialog.dismiss();
} catch (Exception e) {
pDialog.dismiss();
}
我不知道如何在 soap post 方法中发送请求 service.Please 有人帮我解决这个问题。
到目前为止,这是我的代码。
//Code
String NAMESPACE = "http://tempuri.org/";
String METHOD_NAME = "Login";
String SOAP_ACTION = "http://tempuri.org/Login";
String URL = "http://hostname/AllServices.svc/Login";
protected void soap() {
// TODO Auto-generated method stub
SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
// Set all input params
request.addProperty("sapcode", "3232323");
request.addProperty("password", "3232323");
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
SoapEnvelope.VER12);
// Enable the below property if consuming .Net service
envelope.dotNet = true;
envelope.encodingStyle = SoapEnvelope.ENC;
envelope.setAddAdornments(false);
envelope.implicitTypes = false;
envelope.setOutputSoapObject(request);
HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
try {
envelope.headerOut = new Element[1];
envelope.headerOut[0] = buildAuthHeader();
androidHttpTransport.call(SOAP_ACTION, envelope);
SoapObject response = (SoapObject) envelope.getResponse();
Log.i("response", response.getProperty(0).toString());
// response.getProperty(0).toString();
} catch (Exception e) {
e.printStackTrace();
}
}
private Element buildAuthHeader() {
Element h = new Element().createElement(NAMESPACE, "AuthHeader");
Element username = new Element().createElement(NAMESPACE, "User Name");
username.addChild(Node.TEXT, "tech");
h.addChild(Node.ELEMENT, username);
Element pass = new Element().createElement(NAMESPACE, "Password");
pass.addChild(Node.TEXT, "tech@001");
h.addChild(Node.ELEMENT, pass);
return h;
}
基本Header 身份验证: 来自 API 的每个方法调用都需要基本 header 身份验证
用户名:
密码:
方法类型:POST
Return类型是json格式。
try {
// Build JSON string
JSONStringer loginCredentials = new JSONStringer().object()
.key("sapcode").value(userNameSend).key("password")
.value(passwordSend).endObject();
JSONObject jsonObj = new ServerResponse(
Urlgenerator.loginCheck()).getJSONObjectfromURL(
RequestType.POST, loginCredentials);
if (jsonObj != null) {
JSONObject loginObj = jsonObj.getJSONObject("Login");
if (loginObj != null) {
String errorcode = loginObj.getString("errorcode");
final String result = loginObj.getString("result");
if (errorcode.equalsIgnoreCase("0")) {
Utils.updateSharedPreference("remember", "true");
Utils.updateSharedPreference("sapcode",
userNameSend);
startActivity(new Intent(LoginScreen.this,
HomeScreen.class));
finish();
} else {
runOnUiThread(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
ToastUtils.displayToast(
ToastTypeEnum.ALERT_TOAST, result,
Toast.LENGTH_SHORT);
}
});
}
}
}
pDialog.dismiss();
} catch (Exception e) {
pDialog.dismiss();
}