【问题标题】:upload file in background, service?在后台上传文件,服务?
【发布时间】:2012-07-09 06:54:13
【问题描述】:

我需要在后台将文件上传到服务器,我在异步任务中进行此操作,但是,如果我没有互联网,我想稍后再上传。 实际上,如果没有网络,asynctask 就会崩溃。

request.addProperty(file);

            envelope = new SoapSerializationEnvelope(SoapEnvelope.VER12);
            new MarshalBase64().register(envelope); // serialization

            envelope.encodingStyle = SoapEnvelope.ENC;
            envelope.bodyOut = request;

            envelope.dotNet = true;
            envelope.setOutputSoapObject(request);
            envelope.setAddAdornments(false);
            envelope.implicitTypes = true;

            HttpTransportSE transporte = new HttpTransportSE(URL);
            transporte.setXmlVersionTag("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
            transporte.debug = true;

            try {
                transporte.call(SOAP_ACTION, envelope);
                result = (SoapObject) envelope.getResponse();

应该是什么?一项检查互联网连接并在打开时上传文件的服务?其他想法?

谁有例子吗?谢谢

【问题讨论】:

    标签: android service android-asynctask


    【解决方案1】:

    用这个方法检查连接就行了

    public boolean isOnline() {
            ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
    
    
    
            if (cm.getActiveNetworkInfo() != null && cm.getActiveNetworkInfo().isConnectedOrConnecting())
            return true;
            return false;
    
            }
    

    喜欢

      internetconnection= isOnline();
        if(internetconnection==true){
        //performtask
        }
    

    然后您必须创建广播接收器,例如 创建一个广播接收器并使用它

    <action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
    

    在清单中 它会在互联网状态发生变化时运行。在此检查它是否在线然后上传图片

    【讨论】:

    • 是的,但是如果离线,我想稍后再上传文件..我怎么能这样做¿?
    猜你喜欢
    • 2014-06-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-01-29
    • 2015-05-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多