0

我有一个内置在 .NET 中的 Web 服务,它提供以下格式的数据表的输出...现在我需要从我的 android 设备调用它,但我无法访问它。客户是我的 SQL Server 中的一个表2008年数据库

<DataTable xmlns="http://tempuri.org/">
<xs:schema xmlns="" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata" id="NewDataSet">
 <xs:element name="NewDataSet" msdata:IsDataSet="true" msdata:MainDataTable="Customer" msdata:UseCurrentLocale="true">
 <xs:complexType>
 <xs:choice minOccurs="0" maxOccurs="unbounded">
  <xs:element name="Customer">
  <xs:complexType>
    <xs:sequence>
      <xs:element name="CustomerId" type="xs:int" minOccurs="0"/>
       <xs:element name="Code" type="xs:string" minOccurs="0"/>
         <xs:element name="Name" type="xs:string" minOccurs="0"/>
         <xs:element name="SAddress" type="xs:string" minOccurs="0"/>
        <xs:element name="BAddress" type="xs:string" minOccurs="0"/>
      <xs:element name="Phone" type="xs:string" minOccurs="0"/>
     <xs:element name="BArea" type="xs:string" minOccurs="0"/>
    <xs:element name="ContactPerson" type="xs:string" minOccurs="0"/>
 <xs:element name="Advance" type="xs:decimal" minOccurs="0"/>
 <xs:element name="Balance" type="xs:decimal" minOccurs="0"/>
  <xs:element name="Region" type="xs:string" minOccurs="0"/>
  <xs:element name="CellNo" type="xs:string" minOccurs="0"/>
 </xs:sequence>
     </xs:complexType>
   </xs:element>
   </xs:choice>
      </xs:complexType>
       </xs:element>
   </xs:schema>
   <diffgr:diffgram xmlns:msdata="urn:schemas-microsoft-com:xml-msdata"           xmlns:diffgr="urn:schemas-microsoft-com:xml-diffgram-v1">
     <DocumentElement xmlns="">
     <Customer diffgr:id="Customer1" msdata:rowOrder="0">
      <CustomerId>118</CustomerId>
      <Code>CN-000001</Code>
      <Name>A. T. FOODS</Name>
      <SAddress/>
       <BAddress/>
      <Phone/>
        <BArea/> 
       <ContactPerson/>
       <Advance>500</Advance>
        <Balance>999062</Balance>
        <Region>KARACHI</Region>
        <CellNo/>
     </Customer>
   <Customer diffgr:id="Customer2" msdata:rowOrder="1">
     <CustomerId>119</CustomerId>
     <Code>CN-000002</Code>
      <Name>A. A. AGENCIES</Name>
       <SAddress>1ST FLOOR SUPER MARKET</SAddress>
       <BAddress>1ST FLOOR SUPER MARKET</BAddress>
       <Phone>048-3722025</Phone>
         <BArea>KATCHEHRY BAZAR</BArea>
        <ContactPerson>SHEIKH AFTAB AHMED</ContactPerson>
        <Advance>0</Advance>
        <Balance>1035155</Balance>
        <Region>FAISALABAD</Region>
        <CellNo>0300-9607496</CellNo>
     </Customer>
    <Customer diffgr:id="Customer3" msdata:rowOrder="2">
     <CustomerId>120</CustomerId>
    <Code>CN-000003</Code>
     <Name>A. A. TRADERS</Name>
     <SAddress/>
      <BAddress/>
       <Phone/>
       <BArea>SHEERAZI MOHALLA</BArea>
       <ContactPerson/>
      <Advance>0</Advance>
       <Balance>1006780</Balance>
       <Region>REST OF SOUTH</Region>
       <CellNo>0321-2192502</CellNo>
       </Customer>
4

1 回答 1

0

检查下面的例子它应该工作

@Override
protected void onCreate(Bundle savedInstanceState) 
{
   super.onCreate(savedInstanceState);
   setContentView(R.layout.activity_main);
   init();
}
private void init()
{
    //your webservice envelope
    String envelope="<?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><GetContentTreeBySections xmlns=\"http://tempuri.org/\"><SectionsInclude>%s</SectionsInclude><DocumentCount>%s</DocumentCount><SortBy>%s</SortBy><SortOrder>%s</SortOrder><UserName>%s</UserName><Pass>%s</Pass></GetContentTreeBySections></soap:Body></soap:Envelope>";

String myEnvelope = String.format(envelope, "myParam1 value", "myParam2 value","myParam3 value","myParam4 value");
String url = "http://www.mywebserice.com/XMLGenerator/GenerateXML.asmx";
String soapAction = "http://tempuri.org/GetContentTreeBySections"; 
String response = CallWebService(url, soapAction, myEnvelope);
//Log.v("response", response);
Toast.makeText(getApplicationContext(), "this"+response,Toast.LENGTH_LONG).show();
String xml=response;    
}

@Override
public boolean onCreateOptionsMenu(Menu menu) 
{
   // Inflate the menu; this adds items to the action bar if it is present.
   getMenuInflater().inflate(R.menu.activity_main, menu);
   return true;
}
  String CallWebService(String url,String soapAction,String envelope) 
{

  final DefaultHttpClient httpClient=new DefaultHttpClient();
  // request parameters
  HttpParams params = httpClient.getParams();
     HttpConnectionParams.setConnectionTimeout(params, 10000);
     HttpConnectionParams.setSoTimeout(params, 15000);
     // set parameter
  HttpProtocolParams.setUseExpectContinue(httpClient.getParams(), true);

  // POST the envelope
  HttpPost httppost = new HttpPost(url);
  // add headers
     httppost.setHeader("soapaction", soapAction);
     httppost.setHeader("Content-Type", "text/xml; charset=utf-8");
     String responseString="";

     try 
     {
            // the entity holds the request
            HttpEntity entity = new StringEntity(envelope);
            httppost.setEntity(entity);

            // Response handler
            ResponseHandler<String> rh=new ResponseHandler<String>() {
    // invoked when client receives response
    public String handleResponse(HttpResponse response)
      throws ClientProtocolException, IOException {

     // get response entity
     HttpEntity entity = response.getEntity();

     // read the response as byte array
           StringBuffer out = new StringBuffer();
           byte[] b = EntityUtils.toByteArray(entity);

           // write the response byte array to a string buffer
           out.append(new String(b, 0, b.length));
           return out.toString();
    }
   };

   responseString=httpClient.execute(httppost, rh); 

  }
     catch (Exception e) {
      Log.v("exception", e.toString());
  }

     // close the connection
  httpClient.getConnectionManager().shutdown();
  return responseString;
}
于 2013-06-25T04:21:12.190 回答