本文介绍了空对象转换为字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我已经写了一个机器人程序加载值从Web服务为tablerow。但价值来自空,所以我需要做转换成字符串。可有一个人告诉我的方法去做。
尝试{
SoapObject请求= service.getRequest();
SoapSerializationEnvelope信封= service.getEnvelope(要求);
SoapObject响应= service.getResponse(信封);
Log.i(服务大师,response.toString());
诠释计数= response.getPropertyCount();
的for(int i = 0; I<计数;我++){
SoapObject结果=(SoapObject)response.getProperty(ⅰ);
DeleteuserDetails deleteuserDetails =新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服务的空值,所以我需要将其转换成字符串空
。
LogCat中:
12-20 18:48:52.608:W / System.err的(2174):显示java.lang.NullPointerException
12-20 18:48:52.608:W / System.err的(2174):在org.ksoap2.serialization.SoapObject.getPropertyAsString(SoapObject.java:165)
12-20 18:48:52.608:W / System.err的(2174):在com.mvss.admin.Deleteuser $ deteUserIdLoad.doInBackground(Deleteuser.java:81)
12-20 18:48:52.608:W / System.err的(2174):在com.mvss.admin.Deleteuser $ deteUserIdLoad.doInBackground(Deleteuser.java:1)
12-20 18:48:52.608:W / System.err的(2174):在android.os.AsyncTask $ 2.call(AsyncTask.java:185)
12-20 18:48:52.608:W / System.err的(2174):在java.util.concurrent.FutureTask中$ Sync.innerRun(FutureTask.java:305)
12-20 18:48:52.608:W / System.err的(2174):在java.util.concurrent.FutureTask.run(FutureTask.java:137)
12-20 18:48:52.608:W / System.err的(2174):在java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1068)
12-20 18:48:52.608:W / System.err的(2174):在java.util.concurrent.ThreadPoolExecutor中的$ Worker.run(ThreadPoolExecutor.java:561)
12-20 18:48:52.608:W / System.err的(2174):在java.lang.Thread.run(Thread.java:1096)
解决方案
使用此,
尝试{
deleteuserDetails.setCreatedBy(result.getPropertyAsString(0)的ToString());
}
赶上(例外五){
deleteuserDetails.setCreatedBy(空);
}
I have written an android program to load values to tablerow from web service. But a value comes null so I need do convert it into string. Can some one tell me the method to do it.
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);
}
Now deleteuserDetails.setCreatedBy(result.getPropertyAsString(0));
gets null value from webservice, so I need to convert it into string "null"
.
LogCat:
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)
解决方案
Use this,
try {
deleteuserDetails.setCreatedBy(result.getPropertyAsString(0).toString());
}
catch(Exception e) {
deleteuserDetails.setCreatedBy("null");
}
这篇关于空对象转换为字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!