I am writing an Android application that will use the getlist() method of the lists.amx service in sharepoint 2010. I am using ksoap2-android to handle my soap messages. When I try to authenticate I get an xmlpullparser exception expected START_TAG... Why will the following code not authenticate to the sharepoint server?
Here is my code:
public class SharepointList extends Activity {
private static final String SOAP_ACTION = "http://schemas.microsoft.com/sharepoint/soap/GetList";
private static final String METHOD_NAME = "GetList";
private static final String NAMESPACE = "http://schemas.microsoft.com/sharepoint/soap/" ;
private static final String URL = "http://<ip of sharepoint server>/_vti_bin/lists.asmx";
private TextView result;
private Button btnSubmit;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
result = (TextView)findViewById(R.id.textView1);
btnSubmit = (Button)findViewById(R.id.button1);
btnSubmit.setOnClickListener(new OnClickListener(){
public void onClick(View v) {
if(v.getId() == R.id.button1)
{
String list = getMobileTestList();
result.setText(list);
}
}
});
}
private String getMobileTestList()
{
PropertyInfo pi = new PropertyInfo();
pi.setName("listName");
pi.setValue("Mobile Test List");
SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
request.addProperty(pi);
String authentication = android.util.Base64.encodeToString("username:password".getBytes(), android.util.Base64.DEFAULT);
List<HeaderProperty> headers = new ArrayList<HeaderProperty>();
headers.add(new HeaderProperty("Authorization","Basic " +authentication));
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.dotNet = true;
envelope.setOutputSoapObject(request);
HttpTransportSE transport = new HttpTransportSE(URL);
try
{
transport.debug = true;
transport.call(SOAP_ACTION, envelope, headers);
//transport.call(SOAP_ACTION, envelope);
Object result = envelope.getResponse();
return result.toString();
}
catch(Exception e)
{
return e.toString();
}
}
}
Here is the transport.requestdump (preceeding '<' removed):
- v:Envelope xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns:d="http://www.w3.org/2001/XMLSchema" xmlns:c="http://schemas.xmlsoap.org/soap/encoding/" xmlns:v="http://schemas.xmlsoap.org/soap/envelope/">
- v:Header />
- v:Body>
- GetList xmlns="http://schemas.microsoft.com/sharepoint/soap/" id="o0" c:root="1">
- listName i:type="d:string">Mobile Test List
- GetList xmlns="http://schemas.microsoft.com/sharepoint/soap/" id="o0" c:root="1">
- /GetList>
- /v:Body>
- /v:Envelope>
Here is the transport.responsedump (preceeding '<' removed):
- !DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN""http://www.w3.org/TR/html4/strict.dtd">
- HTML>
- HEAD>
- TITLE>Bad Request
- META HTTP-EQUIV="Content-Type" Content="text/html; charset=us-ascii">
- /HEAD>
- BODY>
- h2>Bad Request - Invalid Hostname
- hr> p>HTTP Error 400. The request hostname is invalid.
- /BODY>
- /HTML>
Maybe try the following:
By default the Android Base64 util adds a newline character to the end of the encoded string. This invalidates the HTTP headers and causes the "Bad request".
The
Base64.NO_WRAP
flag prevents this and keeps the headers in tact.