2015-06-22 53 views
0

我正在制作一个可检查从ASP Web服务(使用Ksoap与Web Service交互)返回的信息的Android应用程序。我不知道为什么我可以用WService上的另一个操作检索值,但不能用这个操作。这是值(JSON格式)从我的服务返回:当在Android Ksoap中调用服务时,Asp Web Service始终以空值响应

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

} 

这是我的代码片段,我使用我的活动来显示数组中的值。

 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,但我没有找到一种方法来处理这个。

+0

使用异步任务,那么它会正常工作 – Mano

+0

http://stackoverflow.com/a/29444607/4447803尝试这样。它使用Ksoap的异步任务但给Web服务提供适当的请求。 – Mano

+0

你能告诉我我应该在哪里放AsyncTask,我不知道我会把它放在哪里。在我的活动(已经使用AsyncTask)或在我的secuBookSOAPWS类? @Mano –

回答

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(); 
+0

我已经尝试过,但不知道我的代码有什么问题 –

0

谢谢你帮我调用这个类。 我发布了这个答案,以帮助像我这样的问题的每个人。你只需要在AsyncTask中调用webservice,它就像魅力一样。每个活动你可以一次调用一个服务,如果你不知道的话。