2

我需要在后台将文件上传到服务器,我在 asynctask 中进行此操作,但是,如果我没有互联网,我想稍后再上传。实际上,如果没有网络,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();

应该是什么?检查互联网连接以及何时打开文件的服务?其他想法?

请问有人有例子吗?谢谢

4

1 回答 1

2

只需使用此方法检查连接

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" />

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

于 2012-07-09T06:58:37.003 回答