解决Web Service问题所需的帮助

时间:2010-10-22 03:26:37

标签: android soap salesforce

您好我正在尝试通过Android平台上的网络服务登录salesforce。但我面临一些问题。我收到 “套接字异常:操作超时” 错误。我正在使用为Android安装的Ksoap2 soap cient和WSDL详细信息来自Salesforce的开发人员帐户的Partner WSDL。我已经启用了“api access”。此外,用户名和密码都有效。我正在通过Android桌面模拟器上的开发环境(Eclipse)来尝试这一点 任何人都可以帮我弄清楚我是否遗漏了什么并解决了这个问题 以下是源代码:

import android.app.Activity;
//Soap imports
import org.ksoap2.SoapEnvelope;
import org.ksoap2.SoapFault;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.AndroidHttpTransport;

//Android Imports
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;

import org.xmlpull.v1.*;
import java.io.IOException;


import android.os.Bundle;

public class SalesForceLogin extends Activity 
{
    private static final String NAMESPACE = "urn:sobject.partner.soap.sforce.com";
    private static final String URL = "https://login.salesforce.com/services/Soap/c/20.0";
    private static final String SOAP_ACTION = "";
    private static final String TAG = "SALESFORCE-SERVICE";

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        //New
        SoapObject binding = new SoapObject(NAMESPACE, "login");
        binding.addProperty("username", "username"); //Valid username
        binding.addProperty("password", "password"); //Valid password appended by security token
        SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);

        envelope.setOutputSoapObject(binding);
        AndroidHttpTransport androidHttpTransport = new AndroidHttpTransport(URL);
        androidHttpTransport.debug = true; 
        SoapObject result=null;
        try 
        {
            androidHttpTransport.call(SOAP_ACTION, envelope);
            result = (SoapObject)envelope.getResponse();
            // At this point, Soap login success.
            Log.v(TAG, result.toString());
            Object serverUrl=result.getProperty("serverUrl");
            Log.v(TAG,"serverUrl="+serverUrl);
            Object passwordExpired=result.getProperty("passwordExpired");
            Log.v(TAG,"passwordExpired="+passwordExpired); 
            Log.v(TAG, "Ending Login with Success.");

        } 
        catch (SoapFault aSoapFault) 
        {
            Log.d(TAG, "SoapFault actor="+aSoapFault.faultactor);
            Log.d(TAG, "SoapFault code="+aSoapFault.faultcode);
            Log.d(TAG, "SoapFault message="+aSoapFault.faultstring);
            // do something – Soap Fault occurred
        }
        catch (XmlPullParserException reso)
        {
            Log.v(TAG, "XmlPullParserException");
            reso.printStackTrace();
        }
        catch (IOException reso) //Socket Exception caught here!
        {
            Log.v(TAG, "IOException");
            reso.printStackTrace();
        }

    }
}

编辑:为了引用这个线程的任何人的参考,我能够运行上面的web服务,问题是url和命名空间之间的差异。通过以下修改,上面的代码运行得非常好。

NAMESPACE = "urn:partner.soap.sforce.com";
URL = "https://login.salesforce.com/services/Soap/u/20.0";
SOAP_ACTION = "soap";

2 个答案:

答案 0 :(得分:2)

路由问题可能吗? (即端口转发,代理,防火墙等)

答案 1 :(得分:1)

我有一些使用Ksoap2-Android for .NET webservices的经验,我可以告诉你的第一件事就是它们最多是困难的。我祈祷Google将来会成为一名本地肥皂客户......

至于你的问题,在不了解你的网络服务细节的情况下很难说出错。在套接字创建或设置选项期间可以抛出SocketException(请参阅google api)。您可能正在使用错误的URL或命名空间。

我还建议使用数据包嗅探器(wireshark对此有好处),这样你就可以看到你的soap请求实际上是什么样的了。然后,您可以将其与服务期望的内容进行比较,并从那里进行微调。

祝你好运!让我知道我是否可以提供更多帮助