2

这是我构建我的 Kosap 请求 XML 的方式:

public static SoapSerializationEnvelope constructEnvelope (String METHOD_NAME, List<NameValuePair> properties) {
    SoapObject request = new SoapObject(Constants.NAMESPACE, METHOD_NAME);
    if (null != properties && !properties.isEmpty() && properties.size() > 0)   {
        for (NameValuePair prop : properties) {
            request.addProperty(prop.getName(), prop.getValue());
        }
    }

    SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
    envelope.dotNet = true;
    envelope.setOutputSoapObject(request);
    if (null != properties && !properties.isEmpty() && properties.size() > 0)   {
        envelope.addMapping(Constants.NAMESPACE, METHOD_NAME, request.getClass());
    }
    return envelope;
}

堆栈跟踪 :

04-08 16:46:42.292: WARN/System.err(19812): java.lang.NullPointerException
04-08 16:46:42.292: WARN/System.err(19812): at org.ksoap2.transport.ServiceConnectionSE.getResponseProperties(ServiceConnectionSE.java:85)
04-08 16:46:42.292: WARN/System.err(19812): at org.ksoap2.transport.HttpTransportSE.call(HttpTransportSE.java:189)
04-08 16:46:42.292: WARN/System.err(19812): at org.ksoap2.transport.HttpTransportSE.call(HttpTransportSE.java:114)
04-08 16:46:42.292: WARN/System.err(19812): at com.cantorfuturesexchange.CantorBinaryOptions.ui.LogInActivity$PracticeRegistration.doInBackground(LogInActivity.java:665)
04-08 16:46:42.292: WARN/System.err(19812): at com.cantorfuturesexchange.CantorBinaryOptions.ui.LogInActivity$PracticeRegistration.doInBackground(LogInActivity.java:623)
04-08 16:46:42.292: WARN/System.err(19812): at android.os.AsyncTask$2.call(AsyncTask.java:287)
04-08 16:46:42.292: WARN/System.err(19812): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
04-08 16:46:42.302: WARN/System.err(19812): at java.util.concurrent.FutureTask.run(FutureTask.java:137)
04-08 16:46:42.302: WARN/System.err(19812): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
04-08 16:46:42.302: WARN/System.err(19812): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
04-08 16:46:42.302: WARN/System.err(19812): at java.lang.Thread.run(Thread.java:856)
04-08 16:46:42.342: ERROR/MP-Decision(1576): UP Ld:55 Nw:1.990000 Tw:140 rq:2.800000 seq:147.000000
04-08 16:46:42.793: ERROR/MP-Decision(1576): DOWN Ld:18 Ns:1.100000 Ts:190 rq:0.000000 seq:197.000000
04-08 16:46:43.574: WARN/PowerManagerService(718): Timer 0x7->0x3|0x0

研究工作:

按照以下链接中的说明更改我的信封创建: HttpTransportSE.call 方法经常返回 NullPointerException

public static SoapSerializationEnvelope constructEnvelope (String METHOD_NAME, List<NameValuePair> properties) {
    SoapObject request = new SoapObject(Constants.NAMESPACE, METHOD_NAME);
    if (null != properties && !properties.isEmpty() && properties.size() > 0)   {
        for (NameValuePair prop : properties) {
            PropertyInfo property = new PropertyInfo();
            {
                property.name = prop.getName();
                property.setNamespace(Constants.NAMESPACE);
                property.setValue(prop.getValue());
            }
            request.addProperty(property);
        }
    }

    SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
    envelope.dotNet = true;
    envelope.setOutputSoapObject(request);
    if (null != properties && !properties.isEmpty() && properties.size() > 0)   {
        envelope.addMapping(Constants.NAMESPACE, METHOD_NAME, request.getClass());
    }
    return envelope;
}

我什至发现很少有其他链接,我已经尝试过使用 Ksoap 3.0.0 版而不是 3.0.0-rc4 版,但没有任何效果。我什至找到了一个链接,其中说明了有关检查基础知识的内容,但我无法理解如何解决该问题。下面是那个链接。

Android KSOAP2 ServiceConnectionSE NullPointerException

4

1 回答 1

0

我已经搜索了很多关于空指针异常的东西,有些人建议使用 Http keep-Alive,有些人建议对 Ksoap2 使用不同的版本,但都没有帮助。所以我开始弄乱消息来源。我已经通过一个简单的技巧解决了这个问题。这个 hack 帮助了我,因为我只在我进行的第一个 ksoap2 调用中得到空指针异常。第一次调用后,所有 ksoap2 调用均成功。所以我不建议将此作为答案,但如果有人也面临同样的问题,他们可以下载 ksoap2 源并编辑 HttpTransportSE 的调用函数,如下所示。

public List call(String soapAction, SoapEnvelope envelope, List headers)
        throws IOException, XmlPullParserException {
    return call(soapAction, envelope, headers, null);
}

public List callWithRetry(String soapAction, SoapEnvelope envelope, List headers, int numRetries)
        throws IOException, XmlPullParserException {
    for (int i=0; i<=numRetries; i++) {
        try {
            return call(soapAction, envelope, headers, null);
        } catch (IOException e) {
            if (i==numRetries) {
                throw e;
            }
        } catch (XmlPullParserException e) {
            if (i==numRetries) {
                throw e;
            }
        }
    }
    return null;
}

我只是添加了 numOfRetries 以防万一我需要多次尝试。我在得到空指针异常的地方调用了 callwithretry 函数。每件事都像一个魅力。注意:如果调用失败,我们会多次向服务器发出请求。如果在任何情况下您都不希望使用旧的调用功能。

于 2013-10-02T20:06:02.420 回答