Hi I am trying to login salesforce through a webservice on Android platform. But I am facing some problems. I am getting a "Socket Exception:Operation timed out" error. I am using the Ksoap2 soap cient ported for Android and WSDL details are from the Partner WSDL of my developer account at Salesforce.I have anabled the "api access".Also, username and password are valid. I am trying this through development enviornment(Eclipse) on my desktop emulator for Android.
Can anyone please help me figure out if I am missing something and resolve this problem.
Following is the source code:
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();
}
}
}
Edit: For reference of anyone who refers this thread, I was able to get above web service to run, problem was discrepancy between url and namespace. With following modifications the above code runs perfectly fine.
NAMESPACE = "urn:partner.soap.sforce.com";
URL = "https://login.salesforce.com/services/Soap/u/20.0";
SOAP_ACTION = "soap";