2013-06-29 26 views
2

我嘗試使用基本身份驗證中的KSOAP2。我要下載ksoap2-android-assembly-3.0.0-jar-with-dependencies.jarksoap2-extras-3.0.0.jarksoap2-extra-ntlm-3.0.0.jar。我試着用下面的代碼:Ksoap 3.0.0不支持基本身份驗證

ArrayList<HeaderProperty> headerProperty = new ArrayList<HeaderProperty>(); 
headerProperty.add(new HeaderProperty("Authorization", "Basic " + 
org.kobjects.base64.Base64.encode("user:password".getBytes()))); 

它產生錯誤:

java.io.IOException: HTTP request failed, HTTP status: 401 
ERROR:java.io.IOException:HTTP request failed, HTTP status: 401 

我試着用下面太代碼:

HttpTransportBasicAuth androidHttpTpAut = new HttpTransportBasicAuth(URL, "user", "password"); 
androidHttpTpAut.getServiceConnection().connect(); 

再次無法正常工作,生成錯誤:

Exception in thread "main" java.lang.NoClassDefFoundError: org/ksoap2/transport/HttpTransport 
    at java.lang.ClassLoader.defineClass1(Native Method) 

任何人都可以做這個工作fi ne?

回答

4

多次測試後,我發現下面的代碼需要改變:

ArrayList headerProperty = new ArrayList(); 
headerProperty.add(new HeaderProperty("Authorization", "Basic " + 
org.kobjects.base64.Base64.encode("user:password".getBytes()))); 

androidHttpTransport.call(SOAP_ACTION, envelope); 

更改上述androidHttpTransport.call()以下的多態方法:

androidHttpTransport.call(SOAP_ACTION, envelope, headerProperty); 

It's必要把參數headerProperty的方法androidHttpTransport.call。 而且,我在項目中只使用ksoap2-android-assembly-3.0.0-jar-with-dependencies.jar

謝謝

+0

接受此答案爲最佳答案。 – Tupac

+0

工程就像一個魅力! – CGR

1

您可以嘗試下面的代碼。它在我的情況下工作,希望它也能幫助你。

創建一個名爲HttpTransportBasicAuth

import org.ksoap2.transport.*; 
import org.ksoap2.transport.HttpTransportSE; 
import java.io.*; 

public class HttpTransportBasicAuth extends HttpTransportSE { 
    private String username; 
    private String password; 

    public HttpTransportBasicAuth(String url, String username, String password) { 
     super(url); 
     this.username = username; 
     this.password = password; 
    } 

    public ServiceConnection getServiceConnection() throws IOException { 
     ServiceConnectionSE midpConnection = new ServiceConnectionSE(url); 
     addBasicAuthentication(midpConnection); 
     return midpConnection; 
    } 

    protected void addBasicAuthentication(ServiceConnection midpConnection) throws IOException { 
     if (username != null && password != null) { 
      StringBuffer buf = new StringBuffer(username); 
      buf.append(':').append(password); 
      byte[] raw = buf.toString().getBytes(); 
      buf.setLength(0); 
      buf.append("Basic "); 
      org.kobjects.base64.Base64.encode(raw, 0, raw.length, buf); 
      midpConnection.setRequestProperty("Authorization", buf.toString()); 
     } 
    } 
} 

然後類,改變你的代理類以下(參見下面的代理類)

protected org.ksoap2.transport.Transport createTransport() 
{ 
    return new HttpTransportBasicAuth(url,username,password); 
} 

Proxy類

import java.util.List; 

import org.ksoap2.HeaderProperty; 
import org.ksoap2.SoapFault; 
import org.ksoap2.serialization.AttributeContainer; 
import org.ksoap2.serialization.PropertyInfo; 
import org.ksoap2.serialization.SoapObject; 

public class Z_WS_SCAN_REPLENISHMENT 
{ 
    interface IWcfMethod 
    { 
     ExtendedSoapSerializationEnvelope CreateSoapEnvelope() throws java.lang.Exception; 

     Object ProcessResult(ExtendedSoapSerializationEnvelope envelope,SoapObject result) throws java.lang.Exception; 
    } 

    String url="http://example.com/z_ws_scan_replenishment/200/z_ws_scan_replenishment/z_ws_scan_replenishment"; 
    String username = "username"; 
    String password = "password"; 

    int timeOut=60000; 
    public List< HeaderProperty> httpHeaders; 

    IServiceEvents callback; 
    public Z_WS_SCAN_REPLENISHMENT(){} 

    public Z_WS_SCAN_REPLENISHMENT (IServiceEvents callback) 
    { 
     this.callback = callback; 
    } 
    public Z_WS_SCAN_REPLENISHMENT(IServiceEvents callback,String url) 
    { 
     this.callback = callback; 
     this.url = url; 
    } 

    public Z_WS_SCAN_REPLENISHMENT(IServiceEvents callback,String url,int timeOut) 
    { 
     this.callback = callback; 
     this.url = url; 
     this.timeOut=timeOut; 
    } 

    protected org.ksoap2.transport.Transport createTransport() 
    { 
     return new HttpTransportBasicAuth(url,username,password); 
    } 

    protected ExtendedSoapSerializationEnvelope createEnvelope() 
    { 
     return new ExtendedSoapSerializationEnvelope(); 
    } 

    protected void sendRequest(String methodName,ExtendedSoapSerializationEnvelope envelope,org.ksoap2.transport.Transport transport) throws java.lang.Exception 
    { 
     transport.call(methodName, envelope,httpHeaders); 
    } 

    Object getResult(Class destObj,SoapObject source,String resultName,ExtendedSoapSerializationEnvelope __envelope) throws java.lang.Exception 
    { 
     if (source.hasProperty(resultName)) 
     { 
      Object j=source.getProperty(resultName); 
      if(j==null) 
      { 
       return null; 
      } 
      Object instance=__envelope.get((AttributeContainer)j,destObj); 
      return instance; 
     } 
     else if(source.getName().equals(resultName)) { 
      Object instance=__envelope.get(source,destObj); 
      return instance; 
     } 
     return null; 
    } 


    public Bapireturn1 ZScanReplenishment(final String ILgnum,final String IPernr,final String IScannedId,final String IScannedLgpl) throws java.lang.Exception 
    { 
     return (Bapireturn1)execute(new IWcfMethod() 
     { 
      @Override 
      public ExtendedSoapSerializationEnvelope CreateSoapEnvelope(){ 

       ExtendedSoapSerializationEnvelope __envelope = createEnvelope(); 
       SoapObject __soapReq = new SoapObject("urn:sap-com:document:sap:soap:functions:mc-style", "ZScanReplenishment"); 
       __envelope.setOutputSoapObject(__soapReq); 



       PropertyInfo __info=null; 
       __info = new PropertyInfo(); 
       __info.namespace=""; 
       __info.name="ILgnum"; 
       __info.type=PropertyInfo.STRING_CLASS; 
       __info.setValue(ILgnum); 
       __soapReq.addProperty(__info); 
       __info = new PropertyInfo(); 
       __info.namespace=""; 
       __info.name="IPernr"; 
       __info.type=PropertyInfo.STRING_CLASS; 
       __info.setValue(IPernr); 
       __soapReq.addProperty(__info); 
       __info = new PropertyInfo(); 
       __info.namespace=""; 
       __info.name="IScannedId"; 
       __info.type=PropertyInfo.STRING_CLASS; 
       __info.setValue(IScannedId); 
       __soapReq.addProperty(__info); 
       __info = new PropertyInfo(); 
       __info.namespace=""; 
       __info.name="IScannedLgpl"; 
       __info.type=PropertyInfo.STRING_CLASS; 
       __info.setValue(IScannedLgpl); 
       __soapReq.addProperty(__info); 
       return __envelope; 
      } 

      @Override 
      public Object ProcessResult(ExtendedSoapSerializationEnvelope __envelope,SoapObject __result)throws java.lang.Exception { 
       return (Bapireturn1)getResult(Bapireturn1.class,__result,"EReturn",__envelope); 
      } 
     },""); 
    } 



    protected Object execute(IWcfMethod wcfMethod,String methodName) throws java.lang.Exception 
    { 
     org.ksoap2.transport.Transport __httpTransport=createTransport(); 
     ExtendedSoapSerializationEnvelope __envelope=wcfMethod.CreateSoapEnvelope(); 
     sendRequest(methodName, __envelope, __httpTransport); 
     Object __retObj = __envelope.bodyIn; 
     if (__retObj instanceof SoapFault){ 
      SoapFault __fault = (SoapFault)__retObj; 
      throw convertToException(__fault,__envelope); 
     }else{ 
      SoapObject __result=(SoapObject)__retObj; 
      return wcfMethod.ProcessResult(__envelope,__result); 
     } 
    } 

    java.lang.Exception convertToException(SoapFault fault,ExtendedSoapSerializationEnvelope envelope) 
    { 
     return new java.lang.Exception(fault.faultstring); 
    } 
} 

這些變化對我來說是神奇的。希望它也能爲你工作。