使用KSOAP的空指针异常

时间:2014-03-17 20:10:52

标签: android android-asynctask ksoap2 ksoap

我使用KSOAP和AsyncTask将JSON数据发送到Web服务。在某些情况下,我得到一个NullPointerException错误,当我做代码调试时,作为服务的响应,我得到一个NULL值。在AsyncTask的onPostExecute中,我试图捕获此NULL值,但无法执行此操作。我使用if(result.equals(null))和if(result.equals(“null”))但似乎没有用。与nullPointerException一起,LogCat向我显示SSL上的其他警告,连接超时。我无法弄清楚是什么导致了这个问题,如果你能指出我正确的方向,我会出错,那会有很大的帮助。

AsyncTask类

public class IncidentDataToServer extends AsyncTask<JSONObject, Void, String>{
        @Override
        protected String doInBackground(JSONObject... params) {
            incidentJSON = params[0];
            ServerAuthentication sa = new ServerAuthentication();
            String res = sa.incidentDataToServer(incidentJSON);         
            return res;
        }

        @Override
        protected void onPostExecute(String result) {
           //This is where I am trying to catch the null value.
           //tried using result.equals("null") but does not get into the if statement
            if(result.equals(null)){
    //if error from service write the data to local DB                  
                DBStaffList dbObj = new DBStaffList(IncidentsActivity.this);
                dbObj.open();
                dbObj.createIncidentDataJSONEntry( loggedInUserId, incidentJSON.toString());
                dbObj.close();
            }                    
                DBStaffList dbObj = new DBStaffList(IncidentsActivity.this);
                dbObj.open();
                String JsonDatafromDB = dbObj.getIncidentJSONData().toString();             
                dbObj.getIncidentJSONData().size(); 
   //check if there is data in local DB to be sent to service           
            if(dbObj.getIncidentJSONData().size() >0){                   
                    IncidentDataToServer incidentToServerObj = new IncidentDataToServer();
                    try {
                        jsonIncidentObject = new JSONObject(dbObj.getIncidentJSONData().get(0));

                    } catch (JSONException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }                   dbObj.deleteIncidentRow(dbObj.getIncidentRowIds().get(0));
                    incidentToServerObj.execute(jsonIncidentObject);

            }               
                dbObj.close();      
            super.onPostExecute(result);
        }       
    }

将数据发送到服务的方法

public String incidentDataToServer(JSONObject incidentJSON){
        // package the request
        SoapObject Request = new SoapObject(INCIDENT_NAMESPACE, INCIDENT_METHOD_NAME);
        String incidentData = incidentJSON.toString();
        PropertyInfo pi = new PropertyInfo();
        pi.setName("IncidentJSonString");
        pi.setValue(incidentData);
        pi.setType(String.class);
        Request.addProperty(pi);    
        SoapSerializationEnvelope soapEnvelope = new SoapSerializationEnvelope(
                SoapEnvelope.VER11);
        soapEnvelope.dotNet = true;
        soapEnvelope.setOutputSoapObject(Request);      
        HttpTransportSE aht = new HttpTransportSE(INCIDENT_URL);
        try {           
            aht.call(INCIDENT_SOAP_ACTION, soapEnvelope);
            SoapPrimitive resultString = (SoapPrimitive) soapEnvelope
             .getResponse();
              resIncident = resultString.toString();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return resIncident;

    }
来自LogCat的

错误

03-17 19:15:36.758: W/System.err(8237): javax.net.ssl.SSLException: Write error: ssl=0x4e0bfc28: I/O error during system call, Connection timed out
03-17 19:15:36.798: W/System.err(8237):     at org.apache.harmony.xnet.provider.jsse.NativeCrypto.SSL_write(Native Method)
03-17 19:15:36.798: W/System.err(8237):     at org.apache.harmony.xnet.provider.jsse.OpenSSLSocketImpl$SSLOutputStream.write(OpenSSLSocketImpl.java:693)
03-17 19:15:36.808: W/System.err(8237):     at java.io.BufferedOutputStream.flushInternal(BufferedOutputStream.java:185)
03-17 19:15:36.808: W/System.err(8237):     at java.io.BufferedOutputStream.flush(BufferedOutputStream.java:85)
03-17 19:15:36.808: W/System.err(8237):     at libcore.net.http.FixedLengthOutputStream.flush(FixedLengthOutputStream.java:49)
03-17 19:15:36.808: W/System.err(8237):     at org.ksoap2.transport.HttpTransportSE.call(HttpTransportSE.java:188)
03-17 19:15:36.818: W/System.err(8237):     at org.ksoap2.transport.HttpTransportSE.call(HttpTransportSE.java:118)
03-17 19:15:36.818: W/System.err(8237):     at org.ksoap2.transport.HttpTransportSE.call(HttpTransportSE.java:113)
03-17 19:15:36.818: W/System.err(8237):     at com.dimensions.dimensionsapp.ServerAuthentication.incidentDataToServer(ServerAuthentication.java:130)
03-17 19:15:36.818: W/System.err(8237):     at com.dimensions.dimensionsapp.IncidentsActivity$IncidentDataToServer.doInBackground(IncidentsActivity.java:3207)
03-17 19:15:36.818: W/System.err(8237):     at com.dimensions.dimensionsapp.IncidentsActivity$IncidentDataToServer.doInBackground(IncidentsActivity.java:1)
03-17 19:15:36.818: W/System.err(8237):     at android.os.AsyncTask$2.call(AsyncTask.java:287)
03-17 19:15:36.818: W/System.err(8237):     at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
03-17 19:15:36.818: W/System.err(8237):     at java.util.concurrent.FutureTask.run(FutureTask.java:137)
03-17 19:15:36.838: W/System.err(8237):     at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
03-17 19:15:36.838: W/System.err(8237):     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
03-17 19:15:36.838: W/System.err(8237):     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
03-17 19:15:36.838: W/System.err(8237):     at java.lang.Thread.run(Thread.java:856)
03-17 19:15:36.838: D/AndroidRuntime(8237): Shutting down VM
03-17 19:15:36.838: W/dalvikvm(8237): threadid=1: thread exiting with uncaught exception (group=0x40dd52a0)
03-17 19:15:36.838: E/AndroidRuntime(8237): FATAL EXCEPTION: main
03-17 19:15:36.838: E/AndroidRuntime(8237): java.lang.NullPointerException
03-17 19:15:36.838: E/AndroidRuntime(8237):     at com.dimensions.dimensionsapp.IncidentsActivity$IncidentDataToServer.onPostExecute(IncidentsActivity.java:3215)
03-17 19:15:36.838: E/AndroidRuntime(8237):     at com.dimensions.dimensionsapp.IncidentsActivity$IncidentDataToServer.onPostExecute(IncidentsActivity.java:1)
03-17 19:15:36.838: E/AndroidRuntime(8237):     at android.os.AsyncTask.finish(AsyncTask.java:631)
03-17 19:15:36.838: E/AndroidRuntime(8237):     at android.os.AsyncTask.access$600(AsyncTask.java:177)
03-17 19:15:36.838: E/AndroidRuntime(8237):     at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:644)
03-17 19:15:36.838: E/AndroidRuntime(8237):     at android.os.Handler.dispatchMessage(Handler.java:99)
03-17 19:15:36.838: E/AndroidRuntime(8237):     at android.os.Looper.loop(Looper.java:137)
03-17 19:15:36.838: E/AndroidRuntime(8237):     at android.app.ActivityThread.main(ActivityThread.java:4944)
03-17 19:15:36.838: E/AndroidRuntime(8237):     at java.lang.reflect.Method.invokeNative(Native Method)
03-17 19:15:36.838: E/AndroidRuntime(8237):     at java.lang.reflect.Method.invoke(Method.java:511)
03-17 19:15:36.838: E/AndroidRuntime(8237):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1038)
03-17 19:15:36.838: E/AndroidRuntime(8237):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:805)
03-17 19:15:36.838: E/AndroidRuntime(8237):     at dalvik.system.NativeStart.main(Native Method)

帮助/建议非常感谢。 谢谢

2 个答案:

答案 0 :(得分:1)

检查结果是否为null,您应该使用:

if(result == null)

而不是:

if(result.equals(null))

原因是因为result的情况为null,那么你不能调用它上面的任何方法(包括equals),如果你仍然这样做,那么就会抛出NPE。

另见相关问题:Java null check why use == instead of .equals()

答案 1 :(得分:0)

你去兄弟, System.err(8237):at com.dimensions.dimensionsapp.ServerAuthentication.incidentDataToServer(ServerAuthentication.java:130)

看那条线。可能在你的doInBg中。在doinbg中的所有代码周围放一个try {} catch {},然后返回null或以其他方式处理异常。