将空对象转换为字符串

我编写了一个Android程序来将值从Web服务加载到表行。但是值为空,所以我需要将其转换为字符串。有人能告诉我这样做的方法吗?

try{                    

    SoapObject request = service.getRequest();
    SoapSerializationEnvelope envelope = service.getEnvelope(request);
    SoapObject response = service.getResponse(envelope);
    Log.i("Service Master", response.toString());
    int count = response.getPropertyCount();
    for (int i = 0; i < count; i++) {
        SoapObject result = (SoapObject) response.getProperty(i);
        DeleteuserDetails deleteuserDetails=new DeleteuserDetails();
        deleteuserDetails.setUserId(result.getPropertyAsString(4));
        deleteuserDetails.setUserName(result.getPropertyAsString(2));
        deleteuserDetails.setUserRole(result.getPropertyAsString(3));
        deleteuserDetails.setCreatedDate(result.getPropertyAsString(1));
        deleteuserDetails.setCreatedBy(result.getPropertyAsString(0));
        userdetail.add(deleteuserDetails);
    }

现在从Webservice获取空值,所以我需要将其转换为字符串。deleteuserDetails.setCreatedBy(result.getPropertyAsString(0));"null"

日志:

12-20 18:48:52.608: W/System.err(2174): java.lang.NullPointerException
12-20 18:48:52.608: W/System.err(2174):     at org.ksoap2.serialization.SoapObject.getPropertyAsString(SoapObject.java:165)
12-20 18:48:52.608: W/System.err(2174):     at com.mvss.admin.Deleteuser$deteUserIdLoad.doInBackground(Deleteuser.java:81)
12-20 18:48:52.608: W/System.err(2174):     at com.mvss.admin.Deleteuser$deteUserIdLoad.doInBackground(Deleteuser.java:1)
12-20 18:48:52.608: W/System.err(2174):     at android.os.AsyncTask$2.call(AsyncTask.java:185)
12-20 18:48:52.608: W/System.err(2174):     at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
12-20 18:48:52.608: W/System.err(2174):     at java.util.concurrent.FutureTask.run(FutureTask.java:137)
12-20 18:48:52.608: W/System.err(2174):     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1068)
12-20 18:48:52.608: W/System.err(2174):     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:561)
12-20 18:48:52.608: W/System.err(2174):     at java.lang.Thread.run(Thread.java:1096)

答案 1

不要捕获异常或放置条件,而是使用String.valueOf(result.getPropertyAsString(0));

它将调用参数的 toString() 方法,并将其转换为 String,如果 result.getPropertyAsString(0) 为 null,则将其更改为 “null”。


答案 2

result.getPropertyAsString(0)当属性内部为 null 时,单独一个 NPE 本身就会导致 NPE。堆栈跟踪点应指向哪个SoapObject.getPropertyAsString(SoapObject.java:165)

public String getPropertyAsString(int index) {
    PropertyInfo propertyInfo = (PropertyInfo) properties.elementAt(index);
    return propertyInfo.getValue().toString();
}

- 当 或 为 时,它将崩溃。propertyInfopropertyInfo.getValue()null

为了防止这种情况发生,您需要获取属性,而不是通过,而是通过,并将其手动转换为.getPropertyAsStringgetPropertyString

你可以把它封装到一些实用工具方法中。

public static String getPropertyAsString(SoapObject object, int index) {
    Object prop = object.getProperty(index);
    if(prop instanceof PropertyInfo) {
        prop = ((PropertyInfo)prop).getValue();
    }
    return String.valueOf(prop); // will make it "null" if it is null
}

然后做

deleteuserDetails.setUserId(getPropertyAsString(result, getPropertyAsString(4)));
deleteuserDetails.setUserName(getPropertyAsString(result, getPropertyAsString(2)));
deleteuserDetails.setUserRole(getPropertyAsString(result, getPropertyAsString(3)));