使用KSOAP的空指针异常



我正在使用KSOAP和AsyncTask向web服务发送JSON数据。在某些情况下,我得到一个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)

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

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

if(result == null)

代替:

if(result.equals(null))

原因是因为在result为空的情况下,你不能调用它的任何方法(包括equals),如果你仍然这样做,那么NPE将被抛出。

参见相关问题:Java空检查为什么使用==而不是.equals()

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

看那一行。可能在你做的时候。把try{}catch{}放在你所做的所有代码周围,然后返回null或处理异常。

相关内容

  • 没有找到相关文章

最新更新