目录:
一. android使用ksoap2调用webservice
二. 异步调用
三. Android使用ksoap2调用C#中的webservice实现图像上传参考方法
四. 图像传输中Base64编码问题
一. android使用ksoap2调用webservice
这个话题很多文章中做过讨论,这里需要说明的一点,You can't do Network operations on the main thread. Checkout :http://developer.android.com/reference/android/os/AsyncTask.htmlfor painless background threading :) (参考:http://stackoverflow.com/questions/11969071/android-os-networkonmainthreadexception-for-webservice-ksoap),即调用webservice的操作必须是异步执行的(在2.2及之前版本可能不需要)。
android使用ksoap2调用webservice的基本代码如下:
package com.example.webserviceexample;
import java.io.IOException;
import org.ksoap2.SoapEnvelope;
import org.ksoap2.SoapFault;
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 android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends Activity {
final static String NAMESPACE = "http://tempuri.org/";
final static String METHOD_NAME = "CelsiusToFahrenheit";
final static String SOAP_ACTION = "http://tempuri.org/CelsiusToFahrenheit";
final static String URL = "http://www.w3schools.com/webservices/tempconvert.asmx";
TextView sonuc;
EditText deger;
Button hesapla;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
deger = (EditText) findViewById(R.id.deger);
sonuc = (TextView) findViewById(R.id.flag);
hesapla = (Button) findViewById(R.id.hesapla);
hesapla.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
//request info
SoapObject Request = new SoapObject(NAMESPACE, METHOD_NAME);
Request.addProperty("Celcius",deger.getText().toString());
//envelope
SoapSerializationEnvelope soapEnvelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
soapEnvelope.dotNet = true; //.NET = true, php = false
//putting request to the envelope
soapEnvelope.setOutputSoapObject(Request);
//transferring data
HttpTransportSE aht = new HttpTransportSE(URL); //prepare
//start
try {
aht.call(SOAP_ACTION, soapEnvelope);
}
catch (IOException e) {
e.printStackTrace();
}
catch (XmlPullParserException e)
{
e.printStackTrace();
}
//waiting and getting response.
String result;
try {
// we are creating SoapPrimitive Object as waiting for simple variable.
result = "Fahrenheit:" + soapEnvelope.getResponse();
//writing the result to the textView
sonuc.setText(result);
}
catch (SoapFault e) {
e.printStackTrace();
}
}
});
}
@Overrid