0

如何将序列化对象从 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( ????
}

我如何从这里继续?
有没有更好的办法?

4

1 回答 1

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));
于 2012-04-08T16:54:00.767 回答