android调用c# web service实现把sd卡txt文件数据写入远程数据库
c#web service代码:
运行后
android端代码:
public class Upload extends Activity {
private static String requestURL = "http://192.168.120.163/WebServiceLearn/Service1.asmx";
private static final String methodName ="UploadAndroidFile";
private String srcUrl = Environment.getExternalStorageDirectory().getAbsolutePath();//获取SDCard目录;
private String fileName ="firstcheckinfo.txt";
private String picPath= srcUrl + "/"+fileName;
private List<String> checkinfos=new ArrayList<String>();
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//setContentView(R.layout.upload);
try{
FileInputStream fis = new FileInputStream(picPath);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int count = 0;
while((count = fis.read(buffer)) >= 0){
baos.write(buffer, 0, count);
}
String uploadBuffer = new String(Base64.encode(baos.toByteArray(), count)); //进行Base64编码
List<String> connectstring=connectWebService(fileName); //调用webservice
fis.close();
}catch(Exception e){
e.printStackTrace();
}
}
private List<String> connectWebService(String fileName2) {
// TODO Auto-generated method stub
//使用 ksoap2 调用webservice
String namespace = "http://localhost/WebServiceLearn ";
// 命名空间,即服务器端得接口,注:后缀没加 .wsdl,
//服务器端我是用c#实现webservice接口的
String url = "http://localhost/WebServiceLearn/Service1.asmx?wsdl ";
// SOAP Action
String soapAction="http://localhost/WebServiceLearn/UploadAndroidFile";
//指定WebService的命名空间和调用的方法名
SoapObject soapObject = new SoapObject(namespace, methodName);
soapObject.addProperty("FileName", fileName2); //参数1 文件名
//生成调用WebService方法的SOAP请求信息。该信息由SoapSerializationEnvelope对象描述
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER10);
envelope.bodyOut=soapObject;
(new MarshalBase64()).register(envelope);
envelope.dotNet = true; //这个属性是对dotnetwebservice属性的支持,如果 dotnetwebservice不指定rpc方式,则为true,否则是false
envelope.setOutputSoapObject(soapObject);
//实例化一个HttpTransportSE对象来调用web service,并获得xml字符串数据
HttpTransportSE httpTranstation = new HttpTransportSE(url);
httpTranstation.debug=true;//是否是调试模式
try
{
httpTranstation.call(soapAction, envelope);
SoapObject result=(SoapObject)envelope.getResponse();
//下面对结果进行解析,结构类似json对象
int count=result.getPropertyCount();
for(int index=0;index<count;index++){
checkinfos.add(result.getProperty(index).toString());
}
}catch (IOException e) {
e.printStackTrace();
} catch (XmlPullParserException e)
{
e.printStackTrace();
}
return checkinfos;
}
} 每次运行到SoapObject result=(SoapObject)envelope.getResponse();就跑到IOException e处, 报SoapFault - faultcode: 'soap:Client' faultstring: '服务器未能识别 HTTP 头 SOAPAction 的值: 。' faultactor: 'null' detail: org.kxml2.kdom.Node@4056e118错误,试了各种方法都不行,非常着急,哪位大侠帮忙给看看,十分感谢。
[解决办法]