package com.example.ksoap_android_authentication_demo;
import java.util.ArrayList;
import java.util.List;
import org.ksoap2.HeaderProperty;
import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.PropertyInfo;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;
public class MainActivity extends Activity implements OnClickListener {
private static final String SOAP_ACTION = "http://schemas.microsoft.com/sharepoint/soap/GetList";
// List Web Service:
// http://www.runmont.com:1093/test/_vti_bin/lists.asmx?op=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://www.runmont.com:1093/test/_vti_bin/lists.asmx";
private static final String TAG = MainActivity.class.getName();
private TextView responseTextView;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
responseTextView = (TextView) findViewById(R.id.response);
findViewById(R.id.action).setOnClickListener(this);
}
@Override
public void onClick(View v) {
PropertyInfo pi = new PropertyInfo();
pi.setName("listName");
pi.setValue("Calendar");
pi.setType(String.class);
SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
request.addProperty(pi);
String authentication = android.util.Base64.encodeToString(
"myuse...@runmont.com:mypassword".getBytes(),
android.util.Base64.NO_WRAP);
List<HeaderProperty> headers = new ArrayList<HeaderProperty>();
headers.add(new HeaderProperty("Authorization", "Basic "
+ authentication));
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
SoapEnvelope.VER11);
envelope.dotNet = true;
envelope.bodyOut = request;
envelope.setOutputSoapObject(request);
HttpTransportSE transport = new HttpTransportSE(URL);
try {
transport.debug = true;
transport.call(SOAP_ACTION, envelope, headers);
Object result = envelope.getResponse();
Log.i(TAG, "result = " + result);
responseTextView
.setText(result == null ? null : request.toString());
} catch (Exception e) {
Log.e(TAG, e.getMessage(), e);
responseTextView.setText(e.getMessage());
}
}
}
i found it!
in HttpTransportSE.call(String soapAction, SoapEnvelope envelope, List headers, File outputFile), reponseCode is not checked.
in my case, reponseCode == 401, so contentLength == 0, exception thrown when new BufferedInputStream.
i add getResponseCode() and getResponseMessage() in interface ServiceConnection add check reponseCode before instance BufferedInputStream:
int reponseCode = connection.getResponseCode();
if (reponseCode != 200) {
throw new RuntimeException("reponse code:" + reponseCode + "; "+ connection.getResponseMessage());
}