将安卓连接到网络服务

本文关键字:网络服务 连接 | 更新日期: 2023-09-27 18:34:16

我正在开发一个安卓应用程序,我想把它连接到一个网络服务,关于如何发出网络请求并接收响应的任何想法?注意:我想将XML文件发送到Web服务并从中接收。提前致谢

将安卓连接到网络服务

要连接网站,请使用以下代码

             URL url = new URL("http://www.myweb.com");
                HttpURLConnection conn = (HttpURLConnection) url.openConnection();
                conn.setConnectTimeout(5000);    // 5 seconds
                conn.setRequestMethod("GET");       
                conn.connect();
                BufferedReader rd  = new BufferedReader(new InputStreamReader(conn.getInputStream()));
                String line;
                while ((line = rd.readLine()) != null) {
                  //data of the page will be returned in line, do whatever u want here
                }

我创立了这个工具来自动生成 WSDL 到 Android 代码,

http://www.wsdl2code.com/Example.aspx

public void callWebService(){
    SampleService srv1 = new SampleService();
    Request req = new Request();
    req.companyId = "1";
    req.userName = "userName";
    req.password = "pas";
    Response response =  srv1.ServiceSample(req);
}