2013-06-25 27 views
0

我有一個.NET服務,它提供了一個數據表的格式輸出... 現在我需要從我的Android設備調用它,但我不能訪問它..客戶是我的SQL Server 2008數據庫來自android的調用和XML網絡服務

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

回答

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

它的工作,但它在XML ..WITH標籤 –

+0

的形式返回的文本解析XML並提取數據 – blganesh101