0

我的 android 应用程序有一个BroadcastReceiver. 在开始时,我启动了Service一个AlarmManager用于每 10 分钟定期调用我的接收器的一个,并且接收器尝试建立一个 http 连接。当接收者试图建立一个 httpConnection 时,有时需要排队httpConnection.getResponseCode();androidHttpTransport.call(soapAction, envelope);在这些情况下,android UI(活动)挂起并等待接收者的操作完成。但我认为广播接收器的操作是在一个单独的线程中,它不应该暂停 UI 线程。

为什么会发生这种情况,我该如何纠正?

我在接收器中的 httpconnection:

private Object callWebServiceMethodPublic(String url,
            String namespace, String methodName,
            HashMap<String, Object> parameters, String soapAction)
            throws Exception {

        //System.setProperty("http.keepAlive", "false");

        Log.i("WebService", "URL: " + url);
        Log.i("WebService", "MethodName: " + methodName);

        Log.i("SendMapMovements", "1.2.3.1");

        URL myurl = new URL(url);
        URLConnection connection = myurl.openConnection();
        connection.setConnectTimeout(20 * 1000);
        //connection.setRequestProperty("Connection", "close");

        HttpURLConnection httpConnection = (HttpURLConnection) connection;

        Log.i("SendMapMovements", "1.2.3.2");

        int responseCode = -1;
        try {
            responseCode = httpConnection.getResponseCode();

            Log.i("SendMapMovements", "1.2.3.3");

        } catch (Exception e1) {
            if (e1.toString().contains("Network is unreachable")) {

            } else if (e1.toString().contains("SocketTimeoutException")) {

            } else {
                throw e1;
            }
        }
        if (responseCode == HttpURLConnection.HTTP_OK) {
            httpConnection.disconnect();

            Log.i("SendMapMovements", "1.2.3.4");

            SoapObject request = new SoapObject(namespace, methodName);

            if (parameters != null) {
                String[] keys = new String[0];
                keys = (String[]) parameters.keySet().toArray(keys);
                Object[] vals = (Object[]) parameters.values().toArray();

                for (int i = 0; i < parameters.size(); i++) {
                    request.addProperty(keys[i], vals[i]);
                    Log.i("WebService", keys[i] + ": " + vals[i]);
                }
            }

            Log.i("SendMapMovements", "1.2.3.5");

            SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
                    SoapEnvelope.VER11);
            envelope.dotNet = true;

            envelope.setOutputSoapObject(request);
            HttpTransportSE androidHttpTransport = new HttpTransportSE(url,
                    TimeOutInSeconds * 1000);

            Log.i("SendMapMovements", "1.2.3.6");

            try {
                androidHttpTransport.call(soapAction, envelope);

                Log.i("SendMapMovements", "1.2.3.7");

            } catch (Exception e) {
                if (e.toString().contains("XmlPullParserException")) {
                    throw new Exception(...);
                }
            }

            Object so = envelope.getResponse();
            System.gc();

            return so;

        } else {
            httpConnection.disconnect();
            Log.i("SendMapMovements", "1.2.3.8");
            String strErrorDescription = getHttpErrorDescription(responseCode);
            throw new Exception(strErrorDescription);
        }
    }
4

1 回答 1

1

您必须在 new 中运行您的连接代码Thread,而不是 on UI Thread (Main Thread),因为 theBroadcastReceiver和您Activity在同一线程上运行。

于 2012-11-11T07:20:35.213 回答