2011-09-26 78 views
2

我正在開發一個android應用程序,我必須導入一些web服務引用;目前我正在使用Eclipse Indigo,我沒有找到任何導入Web參考選項,所以任何人都可以幫助我如何做到這一點?如何使用Eclipse Indigo在Android中導入Web服務引用?

+0

Web服務引用? –

+0

ya服務參考。 – nag

+0

在這個問題上需要更多的信息,我甚至不知道你的意思是「網絡服務引用」? –

回答

1

據我所知,在Android中沒有任何自動創建WSDL服務引用的方法。

不幸的是,您需要定義自己訪問WSDL服務的類和方法。

如果您的Web服務使用SOAP,那麼您可能需要調查http://code.google.com/p/ksoap2-android/作爲庫來協助您進行服務調用。

+0

不是WCF使用網絡服務 – nag

+0

好吧,也許您正在使用支持WSDL(Web服務描述語言)的舊版.net技術之一。無論哪種方式,我不相信有任何Eclipse擴展可用,它將讀取WSDL併爲您創建服務引用/類。 – Rob

+0

雅目前我已經使用axis2java jar文件,我得到像「導入網站引用」的選項,但它不會工作我在這個靛藍版本給出錯誤?那麼有沒有其他方法可以做到這一點? – nag

1

import java.io.BufferedReader; import java.io.InputStream; import java.io.InputStreamReader; import java.util.ArrayList;

import org.apache.http.HttpEntity; import org.apache.http.HttpResponse; import org.apache.http.NameValuePair; import org.apache.http.client.HttpClient; import org.apache.http.client.entity.UrlEncodedFormEntity; import org.apache.http.client.methods.HttpPost; import org.apache.http.impl.client.DefaultHttpClient;

公共類DbRequest {

public DbRequest() { 
} 
public String sendDBRequest(ArrayList<NameValuePair> httpPost) { 
    String result = ""; 

    String url = "http://www.YourURL.com/android/dbservice.php";//For Online Server 
    //String url = "http://10.0.2.2/android/dbservice.php"; 
    //String url = "http://192.168.1.4/android/dbservice.php";//For Local Server 
    InputStream is = null; 

    try { 
     HttpClient httpclient = new DefaultHttpClient(); 
     HttpPost httppost = new HttpPost(url); 
     httppost.setEntity(new UrlEncodedFormEntity(httpPost)); 
     HttpResponse response = httpclient.execute(httppost); 
     HttpEntity entity = response.getEntity(); 
     is = entity.getContent(); 
    } catch (Exception e) { 
     result = e.toString(); 
    } 

    try { 
     BufferedReader reader = new BufferedReader(new InputStreamReader(
       is, "iso-8859-1"), 8); 
     StringBuilder sb = new StringBuilder(); 
     String line = null; 
     while ((line = reader.readLine()) != null) { 
      sb.append(line + "\n"); 
     } 
     is.close(); 
     result = sb.toString(); 
    } catch (Exception e) { 

     result = e.toString(); 
    } 
    return (result); 
} 

}

更換你的數據庫服務的URL地址。它會調用它,並將收到字符串作爲結果...

+0

在我的服務網址中,我有很多類和方法,所以如何在Android應用程序中使用它們? – nag

+0

在您的活動中創建上述類的實例,並通過傳遞httpPost對列表來調用其功能,如果您想發佈一些可以在web服務中讀取的數據。這樣做: DBService dbservice = new DBService dbsesrvice.sendDBRequest(httpPost); –