将空对象转换为字符串

我已经编写了一个android程序来将值从Web服务加载到表行。但是值变为null,因此我需要将其转换为字符串。有人可以告诉我这样做的方法吗?

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);

}

现在deleteuserDetails.setCreatedBy(result.getPropertyAsString(0));从Web服务获取空值,因此我需要将其转换为string

"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)

回答:

用这个,

try {

deleteuserDetails.setCreatedBy(result.getPropertyAsString(0).toString());

}

catch(Exception e) {

deleteuserDetails.setCreatedBy("null");

}

以上是 将空对象转换为字符串 的全部内容, 来源链接: utcz.com/qa/404122.html

回到顶部