丶夏之星1 2014-10-13 02:04 采纳率: 0%
浏览 626

android 读取webservice

SoapObject object = (SoapObject) envelope.bodyIn;
我在测试方法里面能获取到值,但是运行android工程时就是null

  • 写回答

1条回答 默认 最新

  • 你知我知皆知 2024-08-02 20:52
    关注

    以下回答参考 皆我百晓生券券喵儿 等免费微信小程序相关内容作答,并由本人整理回复。

    要使用Android的SoapObject来访问Web Service,你需要遵循以下步骤:

    1. 首先,确保你的项目中包含了javax.xml.soap.*包。如果没有,请在项目的build.gradle文件中添加依赖。

    2. 然后,创建一个XmlHttpHandler类,用于处理XML请求和响应。

    3. XmlHttpHandler类中,定义一个方法来发送HTTP GET请求到指定的URL,并解析返回的XML数据。

    4. 使用SoapObject来构建SOAP请求。在这个例子中,我们假设你想要从Web服务接收一些参数并将其传递给你的方法。

    5. onResponseReceived()方法中,检查SoapObject对象是否为空或包含无效的数据。如果它为空或不正确,则抛出异常。

    6. 如果所有条件都满足,你可以将SoapObject中的属性转换为Java对象。

    下面是一个完整的示例代码:

    public class XmlHttpHandler {
        private static final String SOAP_ACTION = "http://example.com/soapaction";
        private static final String URL = "https://www.example.com/webservice";
    
        public void sendGetRequest() throws Exception {
            // 创建一个新的HTTP请求
            HttpPost request = new HttpPost(URL);
    
            // 设置HTTP请求头
            request.setHeader("Content-Type", "application/xml; charset=UTF-8");
            request.setHeader("SOAPAction", SOAP_ACTION);
    
            // 创建一个新的XMLHttpRequestHandler实例
            XMLHttpRequestHandler handler = new XMLHttpRequestHandler();
    
            // 发送GET请求
            HttpResponse response = executeHttpGet(request, handler);
            
            // 解析XML数据
            SoapObject soapObject = (SoapObject) handler.getResponse();
            if(soapObject == null || soapObject.getPropertyCount() <= 0){
                throw new RuntimeException("Invalid SOAP response received.");
            }
    
            // 提取参数
            String param1 = soapObject.getPropertyValue("param1").toString();
            String param2 = soapObject.getPropertyValue("param2").toString();
    
            // 打印参数
            System.out.println("Parameter 1: " + param1);
            System.out.println("Parameter 2: " + param2);
        }
    }
    
    class XMLHttpRequestHandler implements IXmlHttpHandler {
    
        @Override
        public Response executeHttpGet(HttpRequest request, IXmlHttpHandler handler) throws IOException, SAXException {
            // 将请求发送到服务器
            URL url = new URL(request.getURL());
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            connection.setRequestMethod("GET");
            connection.setDoOutput(true); // Enable sending of output data.
            connection.setRequestProperty("Content-Type", "text/xml");
    
            try (BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()))) {
                StringBuilder xmlStringBuilder = new StringBuilder();
                String line;
                while ((line = reader.readLine()) != null) {
                    xmlStringBuilder.append(line).append("\n");
                }
                return new SoapObject(SOAP_ACTION, xmlStringBuilder.toString());
            } finally {
                connection.disconnect();
            }
        }
    }
    

    这个示例代码首先创建了一个HTTP GET请求,然后执行了这个请求并通过XmlHttpHandler处理结果。注意,你需要根据实际的Web服务进行相应的修改,例如改变URL、参数名称等。

    评论

报告相同问题?