2011-04-07 20 views
0

我在Android中遇到了一些問題,因爲我對它很陌生。我需要知道如何在Android中爲以下問題編寫語法。在android中編寫語法所需的幫助

問題: 我有一個.net網絡服務(www.somesite.com)。該Web服務器具有需要用戶名和密碼作爲參數進行身份驗證的身份驗證方法。一旦我使用身份驗證方法設置這些內容,它將允許我調用Web服務器中存在的其他功能。我有用ASP編寫的源代碼。我想在Android中編寫相同的代碼。

private MyServerAPI.Service _service; 
_service = new MyServerAPI.Service(); 

MyServerAPI.DTAuthHeader auth = new MyServerAPI.DTAuthHeader(); 
auth.Username = ConfigurationManager.AppSettings["MyServerAPI.user"]; 
auth.Password = ConfigurationManager.AppSettings["MyServerAPI.pass"]; 

_service.DTAuthHeaderValue = auth; 
_service.Url = ConfigurationManager.AppSettings["MyServerAPI.service"]; 

基本上,我想在Android中編寫與上面的代碼相同的東西。

回答

0

有一個用於處理Web服務的庫,名爲KSOAP2。找到它here

使用它並注意您的服務器如何requrires用戶名和密碼發送。

UPDATE
代碼段:

SoapObject soapRequest = new SoapObject(WSDL_TARGET_NAMESPACE, OPERATION_NAME) ; 
    soapRequest.addProperty("appName","MyCoolApp"); 
    soapRequest.addProperty("sEmail","[email protected]"); 
    soapRequest.addProperty("sPassword","test"); 
    SoapSerializationEnvelope soapEnvelope = new  SoapSerializationEnvelope(SoapEnvelope.VER11); 
    soapEnvelope.dotNet = true; 
    soapEnvelope.setOutputSoapObject(soapRequest); 

    HttpTransportSE httpTransport = new HttpTransportSE(SOAP_ADDRESS); 

    try{ 
    httpTransport.call(SOAP_ACTION, soapEnvelope); 
    Object response = soapEnvelope.getResponse(); 
    Log.d("-----RESPONSE",""+response); 
    }catch (Exception exception){ 
    Log.d("RESPONSE",""+exception.toString()); 
    } 

雖然我不鼓勵在這裏張貼整個代碼,但因爲你似乎陷入麻煩,我希望這多少可以幫助你。

或稍等一下我會詳細介紹它的相關內容。

+0

能否請您寫一個樣本片段和帖子,因爲它是非常迫切的我們 – Naruto 2011-04-07 13:03:04

+0

謝謝謝謝,當然,我會等待你的博客..謝謝很多:) ..我會看看你的代碼。 – Naruto 2011-04-08 09:18:20

+0

謝謝:)如果這個答案解決了你的問題,你可以把它標記爲已解決。 – 2011-04-08 09:29:06

0

如果你不想依賴第三方庫,那麼你也可以使用android本身的inbuild類。

這是一個函數,它用於在您的web服務器上發佈數據以及傳遞用戶名和密碼。

public void postDataToWeb() { 
    // Create a new HttpClient and Post Header 
    HttpClient httpclient = new DefaultHttpClient(); 
    HttpPost httppost = new HttpPost("www.yoursite.com"); 

    try { 
     // Add your data 
     List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2); 
     nameValuePairs.add(new BasicNameValuePair("Username", "Paresh")); 
     nameValuePairs.add(new BasicNameValuePair("Password", "[email protected]")); 
     httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs)); 

     // Execute HTTP Post Request 
     HttpResponse response = httpclient.execute(httppost); 
     HttpEntity entity = response.getEntity(); 
    InputStream is = entity.getContent(); 

    } catch (ClientProtocolException e) { 
     // TODO Auto-generated catch block 
    } catch (IOException e) { 
     // TODO Auto-generated catch block 
    } 
} 

現在,您有在InputStream的響應(在上面的例子中,「是」),並可以用InputStream的播放。

+0

嗨,帕雷希,感謝您的回覆,我想知道如何創建一個在Android中的類的實例,它存在於.net Web服務中。 – Naruto 2011-04-11 09:29:14

+0

你能幫忙嗎我請我試試這個代碼,所以你可以告訴我,我也應該傳遞參數的內容類型? – 2011-11-10 05:40:35