如何将序列化对象从Android客户端发送到Google App Engine Servlet 我有这个代码:
private static HttpClient getHttpClient()
{
if (mHttpClient == null)
{
mHttpClient = new DefaultHttpClient();
final HttpParams params = mHttpClient.getParams();
HttpConnectionParams.setConnectionTimeout(params, HTTP_TIMEOUT);
HttpConnectionParams.setSoTimeout(params, HTTP_TIMEOUT);
ConnManagerParams.setTimeout(params, HTTP_TIMEOUT);
}
return mHttpClient;
}
public static String executeHttpPost(String url, Object obj) throws Exception
{
HttpClient client = getHttpClient();
HttpPost request = new HttpPost(url);
request.set( ????
}
我如何从这里继续?
有更好的方法吗?
答案 0 :(得分:2)
你走了:
ByteArrayOutputStream bos = new ByteArrayOutputStream();
ObjectOutput out = new ObjectOutputStream(bos);
out.writeObject(obj);
byte[] yourObjectSerialized = bos.toByteArray();
HttpClient client = getHttpClient();
HttpPost request = new HttpPost(url);
request.setEntity(new ByteArrayEntity(yourObjectSerialized));