Android 中的 WebService 错误。你调用的对象是空的
Error with WebService in Android. Object reference not set to an instance of an object
这个问题可能有点长。我试图创建一个方法来使用此 WebService,但出现错误。我可以很好地使用其他网络服务,但我没有创建它们(android 上的方法)我很新,所以我需要一些帮助。
事件日志
W/System.err: SoapFault - faultcode: 'soap:Server' faultstring: 'Server was unable to process request. ---> Object reference not set to an instance of an object.' faultactor: 'null' detail: org.kxml2.kdom.Node@107d094
W/System.err: at org.ksoap2.serialization.SoapSerializationEnvelope.parseBody(SoapSerializationEnvelope.java:147)
W/System.err: at org.ksoap2.SoapEnvelope.parse(SoapEnvelope.java:140)
W/System.err: at org.ksoap2.transport.Transport.parseResponse(Transport.java:118)
W/System.err: at org.ksoap2.transport.HttpTransportSE.call(HttpTransportSE.java:275)
W/System.err: at org.ksoap2.transport.HttpTransportSE.call(HttpTransportSE.java:118)
W/System.err: at org.ksoap2.transport.HttpTransportSE.call(HttpTransportSE.java:113)
W/System.err: at org.tempuri.WsCambioCotacaoSoap.validateCoupon(WsCambioCotacaoSoap.java:1004)
W/System.err: at com.example.home.onlineStore.payment.PurchaseDetailsActivity.run(PurchaseDetailsActivity.java:235)
W/System.err: at java.lang.Thread.run(Thread.java:761)
// 第 1004 行是: _ht.call("http://tempuri.org/ValidateCoupon", _envelope);
网络服务请求
POST /wscambiocotacao/WsCambioCotacao.asmx HTTP/1.1
Host: hom.cotacao.com.br
Content-Type: text/xml; charset=utf-8
Content-Length: length
SOAPAction: "http://tempuri.org/ValidateCoupon"
<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<ValidateCoupon xmlns="http://tempuri.org/">
<vOperID>long</vOperID>
<vUserName>string</vUserName>
<vToken>string</vToken>
<vCouponID>string</vCouponID>
</ValidateCoupon>
</soap:Body>
</soap:Envelope>
Web 服务响应
HTTP/1.1 200 OK
Content-Type: text/xml; charset=utf-8
Content-Length: length
<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<ValidateCouponResponse xmlns="http://tempuri.org/">
<ValidateCouponResult>
<Status>string</Status>
<Code>string</Code>
<Message>string</Message>
</ValidateCouponResult>
</ValidateCouponResponse>
</soap:Body>
</soap:Envelope>
WebService里面WebService的方法class(有所有webservices)
public org.tempuri.ValidateCoupon validateCoupon(long vOperID, java.lang.String vUserName, java.lang.String vToken, java.lang.String vCouponID) throws Exception {
SoapObject _client = new SoapObject(NAMESPACE, "validateCoupon");
_client.addProperty("vOperID", vOperID + "");
_client.addProperty("vUserName", vUserName);
_client.addProperty("vToken", vToken);
_client.addProperty("vCouponID", vCouponID);
SoapSerializationEnvelope _envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
_envelope.dotNet = true;
_envelope.setOutputSoapObject(_client);
WSHttpTransport _ht = new WSHttpTransport(Configuration.getWsUrl(), Configuration.getTimeout());
_ht.debug = Util.Debug();
_ht.call("http://tempuri.org/ValidateCoupon", _envelope);
if(Util.Debug()) {
Log.d(LOG_TAG, "ValidateCoupon request:"+_ht.requestDump);
Log.d(LOG_TAG, "ValidateCoupon response:"+_ht.responseDump);
}
SoapObject _ret = (SoapObject) _envelope.getResponse();
int _len = _ret.getPropertyCount();
org.tempuri.ValidateCoupon _returned = new org.tempuri.ValidateCoupon();
for(int _i = 0; _i < _len; _i++) {
_returned.setProperty(_i, _ret.getProperty(_i));
}
return _returned;
}
tempuri 包中的 ValidacteCoupon 对象
public final class ValidateCoupon extends SoapObject {
private java.lang.String status;
private java.lang.String code;
private java.lang.String message;
public ValidateCoupon() {super("", ""); }
public String getMessage() { return message; }
public void setMessage(String message) { this.message = message; }
public String getCode() { return code; }
public void setCode(String code) { this.code = code; }
public String getStatus() { return status; }
public void setStatus(String status) { this.status = status; }
public int getPropertyCount() { return 3; }
public Object getProperty(int __index) {
switch(__index) {
case 0: return status;
case 1: return code;
case 2: return message;
}
return null;
}
public void setProperty(int __index, Object __obj) {
switch(__index) {
case 0: status = (java.lang.String) __obj.toString(); break;
case 1: code = (java.lang.String) __obj.toString(); break;
case 2: message = (java.lang.String) __obj.toString(); break;
}
}
public void getPropertyInfo(int __index, Hashtable __table, PropertyInfo __info) {
switch(__index) {
case 0:
__info.name = "status";
__info.type = java.lang.String.class; break;
case 1:
__info.name = "code";
__info.type = java.lang.String.class; break;
case 2:
__info.name = "message";
__info.type = java.lang.String.class; break;
}
}
}
创建主要使用的方法class
private void validateCoupon() {
showLoading();
new Thread(new Runnable() {
@Override
public void run() {
final Resources res = getResources();
WsCambioCotacaoSoap ws = new WsCambioCotacaoSoap();
try {
HashMap<String, Object> session = session();
long operationID = 0;
if(session.containsKey("OpeID"))
operationID = (Long) session.get("OpeID");
org.tempuri.ValidateCoupon data = ws.validateCoupon(operationID,
Util.getDataWithKey(PurchaseDetailsActivity.this, Util.USER_DEFAULT_LOGIN_BK),
Util.getDataWithKey(PurchaseDetailsActivity.this, Util.USER_DEFAULT_KEY_TOKEN),
editTextCupom.getText().toString());
if(data.getStatus().compareTo("OK") == 0) // sucess
{
// // TODO: 12/2/16
Toast.makeText(PurchaseDetailsActivity.this, "Test compared to OK sucess", Toast.LENGTH_LONG);
} else {
Toast.makeText(PurchaseDetailsActivity.this, "Test compared to OK failed", Toast.LENGTH_LONG);
if(data.getCode().compareTo("INVALID_TOKEN") == 0 || data.getCode().compareTo("EXPIRED_TOKEN") == 0) {
Util.alert(PurchaseDetailsActivity.this, new AlertDialog.Builder(PurchaseDetailsActivity.this)
.setTitle(res.getString(R.string.ALERT_OK_BUTTON))
.setMessage(data.getMessage())
.setNeutralButton(res.getString(R.string.ALERT_OK_BUTTON), new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
PurchaseDetailsActivity.this.onLogoutClick();
}
}));
}
}
} catch (Exception e) {
e.printStackTrace();
}
hideLoading();
}
}).start();
}
这就是我得到的所有信息。对于这个非常非常长的问题,我深感抱歉,但是
我真的需要一些帮助。我是初学者,我什至不知道从哪里开始,我根据另一个已经创建的 web 服务方法创建了该方法(我的主管告诉我这样做)我不能向他寻求帮助,因为他说他知道与 android 无关,所以我必须 google 一些。谁能帮帮我?
创建 SoapObject
时,"method name" 应该是 ValidateCoupon
而不是 validateCoupon
。
改变,
SoapObject _client = new SoapObject(NAMESPACE, "validateCoupon");
到,
SoapObject _client = new SoapObject(NAMESPACE, "ValidateCoupon");
这个问题可能有点长。我试图创建一个方法来使用此 WebService,但出现错误。我可以很好地使用其他网络服务,但我没有创建它们(android 上的方法)我很新,所以我需要一些帮助。
事件日志
W/System.err: SoapFault - faultcode: 'soap:Server' faultstring: 'Server was unable to process request. ---> Object reference not set to an instance of an object.' faultactor: 'null' detail: org.kxml2.kdom.Node@107d094
W/System.err: at org.ksoap2.serialization.SoapSerializationEnvelope.parseBody(SoapSerializationEnvelope.java:147)
W/System.err: at org.ksoap2.SoapEnvelope.parse(SoapEnvelope.java:140)
W/System.err: at org.ksoap2.transport.Transport.parseResponse(Transport.java:118)
W/System.err: at org.ksoap2.transport.HttpTransportSE.call(HttpTransportSE.java:275)
W/System.err: at org.ksoap2.transport.HttpTransportSE.call(HttpTransportSE.java:118)
W/System.err: at org.ksoap2.transport.HttpTransportSE.call(HttpTransportSE.java:113)
W/System.err: at org.tempuri.WsCambioCotacaoSoap.validateCoupon(WsCambioCotacaoSoap.java:1004)
W/System.err: at com.example.home.onlineStore.payment.PurchaseDetailsActivity.run(PurchaseDetailsActivity.java:235)
W/System.err: at java.lang.Thread.run(Thread.java:761)
// 第 1004 行是: _ht.call("http://tempuri.org/ValidateCoupon", _envelope);
网络服务请求
POST /wscambiocotacao/WsCambioCotacao.asmx HTTP/1.1
Host: hom.cotacao.com.br
Content-Type: text/xml; charset=utf-8
Content-Length: length
SOAPAction: "http://tempuri.org/ValidateCoupon"
<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<ValidateCoupon xmlns="http://tempuri.org/">
<vOperID>long</vOperID>
<vUserName>string</vUserName>
<vToken>string</vToken>
<vCouponID>string</vCouponID>
</ValidateCoupon>
</soap:Body>
</soap:Envelope>
Web 服务响应
HTTP/1.1 200 OK
Content-Type: text/xml; charset=utf-8
Content-Length: length
<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<ValidateCouponResponse xmlns="http://tempuri.org/">
<ValidateCouponResult>
<Status>string</Status>
<Code>string</Code>
<Message>string</Message>
</ValidateCouponResult>
</ValidateCouponResponse>
</soap:Body>
</soap:Envelope>
WebService里面WebService的方法class(有所有webservices)
public org.tempuri.ValidateCoupon validateCoupon(long vOperID, java.lang.String vUserName, java.lang.String vToken, java.lang.String vCouponID) throws Exception {
SoapObject _client = new SoapObject(NAMESPACE, "validateCoupon");
_client.addProperty("vOperID", vOperID + "");
_client.addProperty("vUserName", vUserName);
_client.addProperty("vToken", vToken);
_client.addProperty("vCouponID", vCouponID);
SoapSerializationEnvelope _envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
_envelope.dotNet = true;
_envelope.setOutputSoapObject(_client);
WSHttpTransport _ht = new WSHttpTransport(Configuration.getWsUrl(), Configuration.getTimeout());
_ht.debug = Util.Debug();
_ht.call("http://tempuri.org/ValidateCoupon", _envelope);
if(Util.Debug()) {
Log.d(LOG_TAG, "ValidateCoupon request:"+_ht.requestDump);
Log.d(LOG_TAG, "ValidateCoupon response:"+_ht.responseDump);
}
SoapObject _ret = (SoapObject) _envelope.getResponse();
int _len = _ret.getPropertyCount();
org.tempuri.ValidateCoupon _returned = new org.tempuri.ValidateCoupon();
for(int _i = 0; _i < _len; _i++) {
_returned.setProperty(_i, _ret.getProperty(_i));
}
return _returned;
}
tempuri 包中的 ValidacteCoupon 对象
public final class ValidateCoupon extends SoapObject {
private java.lang.String status;
private java.lang.String code;
private java.lang.String message;
public ValidateCoupon() {super("", ""); }
public String getMessage() { return message; }
public void setMessage(String message) { this.message = message; }
public String getCode() { return code; }
public void setCode(String code) { this.code = code; }
public String getStatus() { return status; }
public void setStatus(String status) { this.status = status; }
public int getPropertyCount() { return 3; }
public Object getProperty(int __index) {
switch(__index) {
case 0: return status;
case 1: return code;
case 2: return message;
}
return null;
}
public void setProperty(int __index, Object __obj) {
switch(__index) {
case 0: status = (java.lang.String) __obj.toString(); break;
case 1: code = (java.lang.String) __obj.toString(); break;
case 2: message = (java.lang.String) __obj.toString(); break;
}
}
public void getPropertyInfo(int __index, Hashtable __table, PropertyInfo __info) {
switch(__index) {
case 0:
__info.name = "status";
__info.type = java.lang.String.class; break;
case 1:
__info.name = "code";
__info.type = java.lang.String.class; break;
case 2:
__info.name = "message";
__info.type = java.lang.String.class; break;
}
}
}
创建主要使用的方法class
private void validateCoupon() {
showLoading();
new Thread(new Runnable() {
@Override
public void run() {
final Resources res = getResources();
WsCambioCotacaoSoap ws = new WsCambioCotacaoSoap();
try {
HashMap<String, Object> session = session();
long operationID = 0;
if(session.containsKey("OpeID"))
operationID = (Long) session.get("OpeID");
org.tempuri.ValidateCoupon data = ws.validateCoupon(operationID,
Util.getDataWithKey(PurchaseDetailsActivity.this, Util.USER_DEFAULT_LOGIN_BK),
Util.getDataWithKey(PurchaseDetailsActivity.this, Util.USER_DEFAULT_KEY_TOKEN),
editTextCupom.getText().toString());
if(data.getStatus().compareTo("OK") == 0) // sucess
{
// // TODO: 12/2/16
Toast.makeText(PurchaseDetailsActivity.this, "Test compared to OK sucess", Toast.LENGTH_LONG);
} else {
Toast.makeText(PurchaseDetailsActivity.this, "Test compared to OK failed", Toast.LENGTH_LONG);
if(data.getCode().compareTo("INVALID_TOKEN") == 0 || data.getCode().compareTo("EXPIRED_TOKEN") == 0) {
Util.alert(PurchaseDetailsActivity.this, new AlertDialog.Builder(PurchaseDetailsActivity.this)
.setTitle(res.getString(R.string.ALERT_OK_BUTTON))
.setMessage(data.getMessage())
.setNeutralButton(res.getString(R.string.ALERT_OK_BUTTON), new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
PurchaseDetailsActivity.this.onLogoutClick();
}
}));
}
}
} catch (Exception e) {
e.printStackTrace();
}
hideLoading();
}
}).start();
}
这就是我得到的所有信息。对于这个非常非常长的问题,我深感抱歉,但是 我真的需要一些帮助。我是初学者,我什至不知道从哪里开始,我根据另一个已经创建的 web 服务方法创建了该方法(我的主管告诉我这样做)我不能向他寻求帮助,因为他说他知道与 android 无关,所以我必须 google 一些。谁能帮帮我?
创建 SoapObject
时,"method name" 应该是 ValidateCoupon
而不是 validateCoupon
。
改变,
SoapObject _client = new SoapObject(NAMESPACE, "validateCoupon");
到,
SoapObject _client = new SoapObject(NAMESPACE, "ValidateCoupon");