在Android Ksoap中调用服务时,Asp Web Service始终以null值响应

时间:2015-06-22 09:50:54

标签: android asp.net json web-services

我正在创建一个Android应用程序,可以检查从ASP Web服务返回的信息(使用Ksoap与Web服务交互)。我不知道为什么我可以使用WService上的另一个Operation来检索值,但不能使用这个值。这是我的服务返回的值(json格式):

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

当我尝试从我的Android应用程序获取它时,它返回一个完整的空json数组,如下所示:

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

这是我用来连接并从Web服务获取值的代码。

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;

}

这是我的代码片段,我在我的Activity中使用它来显示数组中的值。

      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"));

请帮我解决这个问题。我已经生气了。我搜索了谷歌和stackoverflow,但我没有找到一种方法来处理这个问题。

2 个答案:

答案 0 :(得分:1)

制作一个像这样扩展AsyncTask的课程

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

您可以使用此

来调用此类
new MyClassName().execute();

答案 1 :(得分:0)

感谢您帮助我。 我发布这个答案,以帮助像我这样有问题的每个人。你只需要在AsyncTask中调用webservice,它就像魅力一样。在您不知道的情况下,您可以一次拨打一项服务。