在Android中访问soap Web服务

时间:2015-01-05 13:43:44

标签: android web-services soap

您好我正在尝试从我机器中的Android模拟器访问我的局域网中另一台机器提供的soap Web服务。

以下代码我必须访问Web服务...

public class UserLoginTask extends AsyncTask<Void, Void, Boolean> {
        @Override
        protected Boolean doInBackground(Void... params) {




            String SOAP_ACTION = "http://xxx.xxx.xx.xx:7001/yantrawebservices/yantrawebservice";
            String URL = "http://xxx.xxx.xx.xx:7001/yantrawebservices/yantrawebservice?WSDL";
            String response = null;
            try {
                DefaultHttpClient httpClient = new DefaultHttpClient();
                   httpClient.getAuthSchemes().register("ntlm", new NTLMSchemeFactory());


                HttpPost httpPost = new HttpPost(URL);
                String bodyOut ="<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:yan=\"http://yantra.com/yantrawebservices\">\n"
                       + "<soapenv:Header/>\n"
                       + "<soapenv:Body>\n"
                        +"<yan:getOrderDetails>\n"
                        +"<yan:env><![CDATA[<YFSEnvironment userId=\"admin\" Password=\"password\"/>]]></yan:env>\n"
                        +"<yan:document1><![CDATA[<Order EnterpriseCode=\"DEFAULT\" DocumentType=\"0001\" OrderNo=\"Y100000000\"/>]]></yan:document1>"
                        +"</yan:getOrderDetails>\n"                             
                        +"</soapenv:Body>\n"
                        +"</soapenv:Envelope>";

                StringEntity se = new StringEntity(bodyOut, HTTP.UTF_8);
                se.setContentType("text/xml");
                httpPost.addHeader("SOAPAction", SOAP_ACTION);
                httpPost.setEntity(se);



                HttpResponse httpResponse = httpClient.execute(httpPost);

                System.out.println("status"+httpResponse.getStatusLine().getStatusCode());

                HttpEntity resEntity = httpResponse.getEntity();
                response = EntityUtils.toString(resEntity);
                System.out.println("status 2 this is sample status  "+response);
                DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
                DocumentBuilder builder = factory.newDocumentBuilder();
                StringReader sr = new StringReader(response);
                InputSource is = new InputSource(sr);
                Document XMLResponse = builder.parse(is);
                NodeList elements = XMLResponse.getElementsByTagName("ERRORS");
                Boolean error = Boolean.valueOf(elements.item(0)
                        .getAttributes().item(0).getNodeValue());
                System.out.println("the boolean"+error);
                HashMap<String, String> Data = new HashMap<String, String>();
                if (error) { // case of no error, value of error is returned
                                // true in case of no error.
                    elements = XMLResponse
                            .getElementsByTagName(
                                    "AUTH_OUTPUT").item(0)
                            .getChildNodes();
                    System.out.println("response" +response);
                    System.out.println("DAta"+Data.get(0).charAt(0));


            } else {// In case of error
                    String eCode = elements.item(0).getChildNodes().item(0)
                            .getAttributes().item(0).getNodeValue();
                    String eDesc = elements.item(0).getChildNodes().item(0)
                            .getAttributes().item(1).getNodeValue();
                    Exception e = new Exception("Server Response: Error Code- "
                            + eCode + " -" + eDesc);
                    System.out.println("Baradwaj Exception"+e);
                    throw e;
                }
            } catch (Exception e) {
                Log.e(LOG_TAG, e.getMessage());
            } finally {
                Log.v(LOG_TAG + " Response value", response);
            }
            return true;
        }

但我得到一个例外,说#34;需要身份验证&#34;。我在这里粘贴我的logcat。

01-05 02:29:47.377: V/TruitonMainActivity Response value(2653): <HEAD><TITLE>Proxy Authorization Required</TITLE></HEAD>
01-05 02:29:47.377: V/TruitonMainActivity Response value(2653): <BODY BGCOLOR="white" FGCOLOR="black"><H1>Proxy Authorization Required</H1><HR>
01-05 02:29:47.377: V/TruitonMainActivity Response value(2653): <FONT FACE="Helvetica,Arial"><B>
01-05 02:29:47.377: V/TruitonMainActivity Response value(2653): Description: Authorization is required for access to this proxy<BR><BR>
01-05 02:29:47.377: V/TruitonMainActivity Response value(2653): </B>Error Code: <BR>HTTP POST Body Is Drained!<BR><BR>
01-05 02:29:47.377: V/TruitonMainActivity Response value(2653): User Agent: <BR>Apache-HttpClient/UNAVAILABLE (java 1.4)<BR><BR>
01-05 02:29:47.377: V/TruitonMainActivity Response value(2653): Error Detail: <BR><PRE>(None).</PRE><BR><BR></B></FONT>
01-05 02:29:47.377: V/TruitonMainActivity Response value(2653): <HR>
01-05 02:29:47.377: V/TruitonMainActivity Response value(2653): <!-- default "Proxy Authorization Required" response (407) -->
01-05 02:29:47.377: V/TruitonMainActivity Response value(2653): </BODY>
01-05 02:29:47.377: V/TruitonMainActivity Response value(2653): ��
01-05 02:29:47.407: V/TruitonMainActivity AsyncTask(2653): Success

但我可以访问网址&#34; http://xxx.xxx.xx.xx:7001/yantrawebservices/yantrawebservice?WSDL&#34;来自模拟器中的浏览器。请告诉我哪里出错了。

3 个答案:

答案 0 :(得分:0)

答案 1 :(得分:0)

我用过ksoap2-android-assembly-2.5.8-jar-with-dependencies.jar

String NAMESPACE = "http://www.domain.com/";
String METHOD_NAME = "methodName";
String SOAP_ACTION = "http://www.domain.com/methodName";
String URL = "your_url";

      SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
          // Set all input params   
          request.addProperty("property", "value");   
          SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
         // if using .net
     //envelope.dotNet = true;
    // envelope.setOutputSoapObject(request);

     HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
     try 
     {
         List<HeaderProperty> headerList = new ArrayList<HeaderProperty>();
         headerList.add(new HeaderProperty("Authorization", "Basic " + org.kobjects.base64.Base64.encode("username:password".getBytes())));

         androidHttpTransport.call(SOAP_ACTION, envelope, headerList);
         SoapObject response = (SoapObject)envelope.getResponse();

     }
     catch(Exception e)
     {
         e.printStackTrace();
     }

答案 2 :(得分:0)

你应该试试这个: 创建一个简单的类,如下所示:

import java.net.Authenticator;  

class ProxyAuthenticator extends Authenticator {  

private String user, password;  
public ProxyAuthenticator(String user, String password) {  
    this.user = user;  
    this.password = password;  
}  

protected PasswordAuthentication getPasswordAuthentication() {  
    return new PasswordAuthentication(user, password.toCharArray());  
}  
}  

并在代码打开URLConnection之前放置这些代码行:

Authenticator.setDefault(new ProxyAuthenticator("user", "password"));
System.setProperty("http.proxyHost", "proxy host");
System.setProperty("http.proxyPort", "port");

希望这有帮助! 来源:http://blog.vinodsingh.com/2008/05/proxy-authentication-in-java.html

相关问题