我需要从HttpResponse读取一个List(或一般的Object),但是我得到一个没有消息的异常。
通过http从服务器获取对象的最佳方法是什么?
我的实施不起作用:
List projectList = new ArrayList();
HttpClient client = new DefaultHttpClient();
HttpGet request = new HttpGet(url+"&action=getProjectList");
HttpResponse response = client.execute(request);
Object obj = null;
InputStream inputStream = response.getEntity().getContent();
// This is where the exception occurs
ObjectInputStream responseObject = new ObjectInputStream(inputStream);
if ((obj = responseObject.readObject()) != null)
{
projectList = (List) obj;
}
答案 0 :(得分:1)
我会在服务器上将对象序列化为XML / JSON / ...,在HTTP响应中发送它们,最后在Android客户端上反序列化它们。存在若干库(也在Android中工作)以自动序列化/反序列化对象,例如, XStream(http://code.google.com/p/xstream-for-android/),Simple(http://simple.sourceforge.net/),Jackson(http://jackson.codehaus.org/), ...