Android调用WCF服务,返回DataTable

时间:2011-08-04 07:48:05

标签: android wcf

如何处理android中的数据表&gt;我的服务返回dataTable.How来处理它?<​​/ p>

public static final String APPURL = "http://192.168.1.213:6969/MySalesServices";    
private static final String METHOD_NAME = "SalesList";
private static final String NAMESPACE = "http://tempuri.org/"; 
private static String SOAP_ACTION = "http://tempuri.org/IMySalesServices/SalesList";

 SoapPrimitive responsePrimitive = null;
ArrayList<String> tablesName = new ArrayList<String>();
 public void onCreate(Bundle savedInstanceState) {
      ..................

  }

  public SoapPrimitive soapPrimitive(String METHOD_NAME, String SOAP_ACTION,String NAMESPACE, String URL) throws IOException,   XmlPullParserException {
    SoapPrimitive responses = null;
    SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME); // set up
    request.addProperty("strExec", strExecutive);
    request.addProperty("strBusinessUnit", strBusinessUnit);
    SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11); // put all required data into a soap
    envelope.dotNet = true;
    envelope.setOutputSoapObject(request);
    AndroidHttpTransport httpTransport = new AndroidHttpTransport(URL);
    httpTransport.debug = true;

    try {
        httpTransport.call(SOAP_ACTION, envelope);
        responses = (SoapPrimitive) envelope.getResponse();

        }catch(SocketException ex){
            ex.printStackTrace();
        } catch (Exception e) {
           e.printStackTrace();
        }

    return responses;
}

我收到了以下错误:

   AnyType{element=anyType{complexType=anyType{choice=anyType{element=anyType{complexType=anyType{sequence=anyType{element=anyType{}; element=anyType{}; element=anyType{}; element=anyType{}; element=anyType{}; }; }; }; }; }; }; }

请帮帮我

2 个答案:

答案 0 :(得分:1)

将Method类型返回为String&amp;演唱会将数据表格式化为Json作为字符串,这是一种很好的方法。易

  //Converting table to json
    public String ConverTableToJson(DataTable dtDownloadJson)
    {
        string[] StrDc = new string[dtDownloadJson.Columns.Count];
        string HeadStr = string.Empty;

        if (dtDownloadJson.Rows.Count > 0)
          {

            for (int i = 0; i < dtDownloadJson.Columns.Count; i++)
            {

                StrDc[i] = dtDownloadJson.Columns[i].Caption;
                HeadStr += "\"" + StrDc[i] + "\" : \"" + StrDc[i] + i.ToString() + "¾" + "\",";
            }
         if (HeadStr.Length > 0)
            {
                HeadStr = HeadStr.Substring(0, HeadStr.Length - 1);
                StringBuilder Sb = new StringBuilder();
                Sb.Append("{\"" + dtDownloadJson.TableName + "\" : [");

                for (int i = 0; i < dtDownloadJson.Rows.Count; i++)
                {

                    string TempStr = HeadStr;
                    Sb.Append("{");

                    for (int j = 0; j < dtDownloadJson.Columns.Count; j++)
                    {
                        TempStr = TempStr.Replace(dtDownloadJson.Columns[j] + j.ToString() + "¾", dtDownloadJson.Rows[i][j].ToString());
                    }

                    Sb.Append(TempStr + "},");
                }

                Sb = new StringBuilder(Sb.ToString().Substring(0, Sb.ToString().Length - 1));
                Sb.Append("]}");
                return Sb.ToString();
            }else
            {
             return "0";
            }
          }
           else
           {
               return "0";
           }
    }

答案 1 :(得分:0)

您完全没有错误的错误。至少,这就是我在自己的应用程序中看到的行为,使用的设置非常类似于那里的设置。您是否尝试将响应转换为SoapObject?然后,您可以在该SoapObject上调用.getPropertyCount()方法,以开始循环响应内容。快速举例:

//Create a Transport object makes the webservice call
HttpTransportSE httpTrans = new HttpTransportSE(URL);
httpTrans.call(SOAP_ACTION, env);

//Cast the object to SoapObject
SoapObject storages = (SoapObject)env.getResponse();

//Loop trough the result
for(int i = 0; i < storages.getPropertyCount(); i++) {
    //Get a SoapObject for each storage
    SoapObject storage = (SoapObject)storages.getProperty(i);
}

看到你得到的回应,你可能需要深入挖掘几个级别以获得你需要的数据。深度为6或7级。如果是一个选项,我会更改您从Web服务获得的响应,以便更容易解析。