通过HTTP从服务器获取对象的最佳方式是什么?
问题描述:
我需要从HttpResponse中读取一个List(或一般对象),但是我得到一个没有消息的异常。通过HTTP从服务器获取对象的最佳方式是什么?
通过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;
}
答
我将序列化对象作为XML/JSON/...在服务器上,给他们一个HTTP响应,并最终反序列化他们的Android客户端上。有几个库(也可以在Android中工作)自动序列化/反序列化对象,例如XStream(http://code.google.com/p/xstream-for-android/),Simple(http://simple.sourceforge.net/),Jackson(http://jackson.codehaus.org/), ...
谢谢,这似乎解决了这个问题,但我现在将它打开,以防万一我有问题;-) – mw88