duong khang
duong khang

Reputation: 352

Asp Web Service always responds with null value when service is called in Android Ksoap

I am making an android app that can check information returned from ASP Web service (using Ksoap to interact with Web Service). I don't know why I can retrieve values with another Operation on WService but not with this one. This is the value (json format) returned from my service:

<string>
{"Code":"nxbtrebk000001","Title":"VI DU","Description":"VI DU test","Author":"Tac Gia","Publisher":"NXB Tre","Price":99900,"Salt":"1"}
</string>

When I try to get it from my Android app it returns a whole null json array like this:

{"Code":null,"Title":null,"Description":null,"Author":null,"Publisher":null,"Price":0,"Salt":null}

This is the code I'm using to connect and get values from the web service.

public final String SOAP_ACTION2 = "http://tempuri.org/GetBookItemByBarCode";
public final String OPERATION_NAME2 = "GetBookItemByBarCode";
public final String WSDL_PARAMETER_NAME2 = "Barcode";
public final String WSDL_TARGET_NAMESPACE2 = "http://tempuri.org/";
public final String SOAP_ADDRESS2 = "http://192.168.10.211/secudi";

public String CallSOAP_GetBookInfor(String paramBarcodeScannedData)
{
    // SOAP works on request response pair.

    SoapObject soapObjectRequest2 = new SoapObject(WSDL_TARGET_NAMESPACE2, OPERATION_NAME2);


    // Using setValue() method, set the value to the property.
    PropertyInfo bookItemBarcodeData2 = new PropertyInfo();
    bookItemBarcodeData2.setName(WSDL_PARAMETER_NAME2);
    bookItemBarcodeData2.setValue(paramBarcodeScannedData);
    bookItemBarcodeData2.setType(String.class);
    soapObjectRequest2.addProperty(bookItemBarcodeData2);

    // Create a serialized envelope which will be used to carry the parameters for SOAP body
    // and call the method through HttpTransportSE method.
    SoapSerializationEnvelope soapEnvelop2 = new SoapSerializationEnvelope(SoapEnvelope.VER11);
    soapEnvelop2.dotNet = true;
    soapEnvelop2.setOutputSoapObject(soapObjectRequest2);

    HttpTransportSE httpTransport = new HttpTransportSE(SOAP_ADDRESS2);
    Object soapResponse2 = null;

    try
    {
        httpTransport.call(SOAP_ACTION2, soapEnvelop2);

        soapResponse2 = soapEnvelop2.getResponse();
    }
    catch (Exception ex)
    {
        //soapResponse = ex.toString();
        ex.printStackTrace();
    }
    return  soapResponse2.toString();
    //return (String)soapResponse;

}

This is my Code Snippet which i using in my Activity to show the value in array.

      soapResponseBookItem = secuBookSOAPWS.CallSOAP_GetBookInfor(bBarcodeScannedData);
      JSONObject bookItemJSON;
      bookItemJSON = new JSONObject(soapResponseBookItem);

      TextView BookName = (TextView) findViewById(R.id.Book_Title);
      BookName.setText("Book Title: " + bookItemJSON.getString("Title"));

Please help me to fix this. I'm already mad about it. I searched google and stackoverflow but I didn't find a method to work with this.

Upvotes: 0

Views: 387

Answers (2)

duong khang
duong khang

Reputation: 352

Thank you for helping me. I'm posting this answer to help everyone who has this problem like me. You just need to Call webservice inside AsyncTask and it work like charm. Each Activity you just can to call one service at a time, in the case you don't know that.

Upvotes: 0

Neal Ahluvalia
Neal Ahluvalia

Reputation: 1548

Make a class that extends AsyncTask like this

private class MyClassName extends AsyncTask<String, Void, String> {

    @Override
    protected String doInBackground(String... params) {
        try {
            SoapObject soapObjectRequest2 = new SoapObject(WSDL_TARGET_NAMESPACE2, OPERATION_NAME2);
            ...
            //Your SOAP Calling Methods Here
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }       
}

And you can call this class by using this

new MyClassName().execute();

Upvotes: 1

Related Questions