首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 移动开发 > Android >

android调用c# web service实现把sd卡txt文件数据写下远程数据库

2013-12-19 
android调用c# web service实现把sd卡txt文件数据写入远程数据库c#web service代码:运行后android端代码:p

android调用c# web service实现把sd卡txt文件数据写入远程数据库
c#web service代码:
android调用c# web service实现把sd卡txt文件数据写下远程数据库
运行后android调用c# web service实现把sd卡txt文件数据写下远程数据库
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错误,试了各种方法都不行,非常着急,哪位大侠帮忙给看看,十分感谢。
[解决办法]

引用:
Quote: 引用:

你可以试试,把访问WEBSERVICE这部分代码,放到线程中调用;
有的时候android版本高了。访问INTENT都需要线程访问。
我web service返回的是dataset,android中那么解析对吗?这个问题都整了好几天了,头都大了,大神 帮我看看吧。


WS端不应该返回dataset,那样android端好像处理有问题;
你应该把WS端dataset中取出数据转换成JSON的数据格式或者XML数据格式,再返回到android端,这样解析相对简单的多。

热点排行