简体   繁体   中英

Error with WebService in Android. Object reference not set to an instance of an object

This question may be a little long. I tried to create a method to use this WebService, however I get an error. I can use others webservice fine, but I did not creat them(the method on android) I'm pretty new so I would like some help.

Event Log

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.rendimento.cotacao.home.onlineStore.payment.PurchaseDetailsActivity$1.run(PurchaseDetailsActivity.java:235)
W/System.err:     at java.lang.Thread.run(Thread.java:761)

// line 1004 is : _ht.call(" http://tempuri.org/ValidateCoupon ", _envelope);

WebService Request

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>

WebService Response

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>

Method of the WebService inside the WebService class(has all 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;
}

Object of the ValidacteCoupon inside the tempuri package

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;
        }

    }

}

Creating the method to use in the main 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();

}

Thats all the info I got. I'm deeply sorry for the very very long question, but I really need some help here. I'm a beginner I don't even know where to start, I created that method based on another webservice method that was already created(my supervisor told me to do) I can't ask him for help, because he said he knows nothing about android so I would have to google some. Can anyone help me please?

When creating the SoapObject the "method name" should be ValidateCoupon and not validateCoupon .

Change,

SoapObject _client = new SoapObject(NAMESPACE, "validateCoupon");

to,

SoapObject _client = new SoapObject(NAMESPACE, "ValidateCoupon");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM