【问题标题】:Android with WCF web service using ksoap2 - error SoapFault - faultcode: 'a:InternalServiceFault'使用 ksoap2 的带有 WCF Web 服务的 Android - 错误 SoapFault - 故障代码:'a:InternalServiceFault'
【发布时间】:2012-10-17 06:56:13
【问题描述】:

我做了一个简单的项目,使用 ksoap2 调用 wcf web 服务。但是当它调用信封.getResponse();它给出了错误提示

错误: SoapFault - faultcode: 'a:InternalServiceFault' faultstring: '由于内部错误,服务器无法处理请求。有关错误的更多信息,请在服务器上打开 IncludeExceptionDetailInFaults(来自 ServiceBehaviorAttribute 或来自配置行为)以便将异常信息发送回客户端,或根据 Microsoft .NET Framework 3.0 SDK 文档打开跟踪并检查服务器跟踪日志。 faultactor: 'null' 细节: null

package testing.wcf;
import java.io.IOException;
import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapPrimitive;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;
import org.xmlpull.v1.XmlPullParserException;
import org.xmlpull.v1.XmlSerializer;

import android.os.Bundle;
import android.os.StrictMode;
import android.widget.TextView;
import android.annotation.SuppressLint;
import android.app.Activity;

public class MainActivity extends Activity 
{
    private static final String strNAMESPACE = "http://www.testing.co.in/TestingService/";
    private static final String strURL = "http://www.testing.co.in/TestingService/UserDetails.svc";
    private static final String strSOAP_ACTION = "http://testing.co.in/TestingService/UserDetails/LoginDetails";
    private static final String strMETHOD_NAME = "LoginDetails";
    TextView tv;
    StringBuilder sb;
    String strInputXML;

      @Override
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        tv = (TextView)findViewById(R.id.testing);
        sb = new StringBuilder();       
            Call();
        tv.setText(sb.toString());
        //setContentView(tv);

    }
    public void Call()
    {
         try 
         {
             SoapObject request = new SoapObject(strNAMESPACE, strMETHOD_NAME);

            String inputxml = "<?xml version="+"\""+"1.0"+"\""+" encoding="+"\""+"utf-8"+"\""+" ?>" +"<MOB> \n  <PIN>0000</PIN> \n  <LOGINID>TEST</LOGINID> \n  <PNUMBER>112233</pNUMBER> \n  <REQUESTID>LoginVal</REQUESTID> \n </MOB>";

            request.addAttribute("strInputXML", inputxml);
            request.addAttribute("strOutputXML","");

                   SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
             envelope.dotNet = true;
             envelope.setOutputSoapObject(request);

             HttpTransportSE androidHttpTransport = new HttpTransportSE(strURL);
             androidHttpTransport.call(strSOAP_ACTION, envelope);
             SoapPrimitive result = (SoapPrimitive)envelope.getResponse();

             String resultData = result.toString();
             sb.append(resultData + "\n");
         }
         catch(Exception e)
         {
             sb.append("Error:\n" + e.getMessage() + "\n");
         }       
    }
}

这里我想这样发送请求

<?xml version="1.0" encoding="utf-8" ?> 
<PhoneData>
<PINNO>0000</PINNO>
<LOGINID>HELLO</LOGINID>
<PASSWORD>1234</PASSWORD>
<REQID>0</REQID>
</PhoneData>

我的响应 XML 应该是

<?xml version="1.0" encoding="utf-8" ?> 
<PhoneData>
<OTA>1234</OTA>
</PhoneData>

【问题讨论】:

  • 可以看源码吗?还有来自网络服务的 wsdl 文件,这可能对我们有帮助。
  • @TobiasMoeThorstensen 您好,我在这里发布了我的源代码

标签: android .net wcf web-services ksoap2


【解决方案1】:

我发布了使用 WCF 的工作代码(WCF 的绑定必须是 basicHttpBinding!):

private static final String NAMESPACE = "http://tempuri.org/";
private static String URL="your url";

private static final String SOAP_ACTION_VALIDATION = "IValidateUser_wcf/ValidateUser";
private static final String VALIDATION_METHOD = "ValidateUser";

public boolean validateUser_WCF(String username, String password){

    SoapSerializationEnvelope envelope = null;
    SoapObject request = null;
    HttpTransportSE httpTransportSE = null;

    try {
        request = new SoapObject(NAMESPACE, VALIDATION_METHOD);
        request.addProperty("username", username);
        request.addProperty("password", password);

        envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
        envelope.dotNet = true; 
        envelope.setOutputSoapObject(request);

            //////////////////////////////                               
            // here you can add a HEADER element if you want
        Element[] header = new Element[1];  

        header[0] = new Element().createElement(NAMESPACE_INFOCAD, "a1");                
        header[0].addChild(Node.TEXT, "HeaderTextContent");

        envelope.headerOut = header;
            //////////////////////////////                               

        httpTransportSE = new HttpTransportSE(URL+VALIDATION_URI, 10*10000); // second parameter is timeout
        httpTransportSE.debug = true;
        httpTransportSE.setXmlVersionTag("<?xml version=\"1.0\" encoding=\"utf-8\"?>");
        httpTransportSE.call(NAMESPACE+SOAP_ACTION_VALIDATION, envelope);

            // if response is a simple text result, you can call SoapPrimitive, if not, you have to call SoapObject result and navigate in response's tree like an xml file
        SoapPrimitive result = (SoapPrimitive)envelope.getResponse();

        //To get the data.
        String textResult = result.toString();
        Log.i("textResult", textResult); 

                    return true;

    } catch (Exception e) {
        // TODO: handle exception
        e.printStackTrace();
    }finally{
                 // here you can see in LOG what is request you send and what is response received
                Log.i(getClass().getSimpleName(),"requestDump : "+httpTransportSE.requestDump);
                Log.i(getClass().getSimpleName(),"responseDump : "+httpTransportSE.responseDump);
    }

    return false;
}

希望我的代码可以帮助你:)。它工作在 100%

【讨论】:

  • 只是想知道,上面代码中VALIDATION_URI的值是什么?它不包含在文件顶部的字符串常量之一中。
  • 只是 httpTrasport 尝试连接的 URL 的另一部分。详细的是“/WebServices/validateuser.svc”,但一般来说是SVC路径。 URI + 验证 URI = "www.yoursite.com/WebServices/validateuser.svc"。我不得不拆分它,因为 SVC 并不总是本地化在同一个 uri 位置
【解决方案2】:

我认为你应该研究一下名为 FaultContract 的东西。如果服务抛出异常,错误消息将永远不会到达客户端。所以这就是FaultContract 的来源。比如说,调用服务会导致服务异常,客户端应该如何知道捕获的错误?

这是一个关于如何实现 FaulContract 的示例:

//An interface for your service
[ServiceContract()]
    public interface IMultiply
    {
        [OperationContract()]
        int Multiply(int number1, int number2);
    }

你的“服务类”中的方法应该是implement这个interface

//Service implementation

public  class DoSomeMath: IMultiply
{
   public int Multiply(int number1, int number2)
   {
     try 
     {
        int k = number1 * number2;
     } 
     catch (Exception e)
     {
       MyFaultException theFault = new MyFaultException();
       theFault.Reason = "Some Error " + e.Message.ToString();
       throw new FaultException<MyFaultException>(theFault);
     }      
     return k;     
   }
}

在客户端,您现在可以捕获异常。如需进一步阅读,我建议您阅读:

Exception Handling in WCF using Fault Contract

我希望这将为您指明正确的方向。

【讨论】:

    【解决方案3】:

    对于 android Emulator 使用这个 ip:10.0.2.2 否则它将无法工作!! 私有最终字符串 SOAP_URL="http://10.0.2.2:15398/Service1.svc";

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-04-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-07-16
      • 1970-01-01
      相关资源
      最近更新 更多