clacka 2015-06-07 01:21 采纳率: 0%
浏览 1523

android调用wsdl参数值取不到

用安卓调用 javawebservice,以wsdl的形式,但是服务器端取不到传递的参数,具体代码如下,请高手帮忙!多谢

public static void getRemoteInfo(String phoneSec) {

// 命名空间

    // 调用的方法名称  
    String methodName = "login";  
    // EndPoint  

    // SOAP Action  
    String soapAction = nameSpace+methodName;  

    // 指定WebService的命名空间和调用的方法名  
    SoapObject rpc = new SoapObject(nameSpace, methodName);  

    // 设置需调用WebService接口需要传入的两个参数mobileCode、userId  
    rpc.addProperty("userId", "lcq");  --这里的两个参数
    rpc.addProperty("passWord", "123123123");  --这里的两个参数

    // 生成调用WebService方法的SOAP请求信息,并指定SOAP的版本  
    SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER10);  

    envelope.bodyOut = rpc;  
    // 设置是否调用的是dotNet开发的WebService  
    envelope.dotNet = true;  
    // 等价于envelope.bodyOut = rpc;  
    envelope.setOutputSoapObject(rpc);  

    HttpTransportSE transport = new HttpTransportSE(endPoint);  
    try {  
        // 调用WebService  
        transport.call(soapAction, envelope);  
    } catch (Exception e) {  
        e.printStackTrace();  
    }  
    // 获取返回的数据  
    SoapObject object = (SoapObject) envelope.bodyIn;  
    // 获取返回的结果  
    String result = object.getProperty(0).toString();  
    // 将WebService返回的结果显示在TextView中  
    //resultView.setText(result);  
}
  • 写回答

1条回答 默认 最新

  • 你知我知皆知 2024-08-03 23:14
    关注

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

    你提供的代码似乎没有正确设置参数。请确保将rpc.addProperty中的userIdpassWord替换为正确的参数值。

    此外,你的代码缺少了处理HTTP响应错误的部分。如果网络连接出现问题或者WS服务不可达,你需要捕获异常,并提供一个默认结果。

    下面是一个修改后的示例:

    import android.os.AsyncTask;
    import android.util.Log;
    
    import java.io.IOException;
    import java.net.HttpURLConnection;
    import java.net.MalformedURLException;
    import java.net.URL;
    
    public class WSClient extends AsyncTask<Void, Void, String> {
        private final String endpointUrl = "http://your-web-service-url.com";
        private final String serviceName = "YourServiceName";
    
        @Override
        protected String doInBackground(Void... params) {
            try {
                URL url = new URL(endpointUrl + "/" + serviceName);
                HttpURLConnection connection = (HttpURLConnection) url.openConnection();
                connection.setRequestMethod("POST");
                connection.setDoOutput(true);
    
                if (!connection.getResponseCode() == HttpURLConnection.HTTP_OK) {
                    throw new RuntimeException("Error: HTTP error code : "
                            + connection.getResponseCode());
                }
    
                // Add the parameters to the request body as multipart/form-data
                DataOutputStream wr = new DataOutputStream(connection.getOutputStream());
                wr.writeBytes("userId=lcq&passWord=123123123");
                wr.flush();
                wr.close();
    
                int responseCode = connection.getResponseCode();
                Log.d("Response Code:", String.valueOf(responseCode));
    
                // Check for any errors in the response
                BufferedReader br = new BufferedReader(new InputStreamReader(
                        connection.getInputStream()));
                StringBuilder response = new StringBuilder();
                String line;
                while ((line = br.readLine()) != null) {
                    response.append(line);
                }
                br.close();
                return response.toString();
    
            } catch (MalformedURLException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
            return null;
        }
    
        @Override
        protected void onPostExecute(String result) {
            super.onPostExecute(result);
            // Handle the result here.
            Log.d("RESULT", "The result is: " + result);
        }
    }
    

    这个示例中,我们创建了一个新的AsyncTask来发送POST请求到我们的Web Service。然后我们在doInBackground方法中添加了参数到请求体作为multipart/form-data。最后,我们使用onPostExecute方法来处理结果。

    注意:这个例子假设你的Web Service URL是完整的URL,包括路径和查询字符串。如果你的URL包含任何非法字符或不安全的部分(例如用户名/密码),你可能需要进行一些额外的安全检查。

    评论

报告相同问题?