【发布时间】:2011-07-14 15:50:28
【问题描述】:
我对使用流进行编码还很陌生,但现在我必须这样做才能更有效地进行 Http 编码。
这是我为获取 HttpClient 的 ContentProducer 而编写的(不工作)代码:
public static ContentProducer getContentProducer(final Context context, final UUID gId)
{
return new ContentProducer()
{
public void writeTo(OutputStream outputStream) throws IOException
{
outputStream = new Base64.OutputStream(new FileOutputStream(StorageManager.getFileFromName(context, gId.toString())));
outputStream.flush();
}
};
}
我在这里使用 Base64 流编码器:http://iharder.sourceforge.net/current/java/base64/ 我的目标是使用此函数将我从二进制文件中读取的数据作为 base64 编码流提供给 HttpClient。
这就是我消费内容制作者的方式:
private MyHttpResponse processPOST(String url, ContentProducer requestData)
{
MyHttpResponse response = new MyHttpResponse();
try
{
HttpPost request = new HttpPost(serviceURL + url);
HttpEntity entity = new EntityTemplate(requestData);
request.setEntity(entity);
ResponseHandler<String> handler = new BasicResponseHandler();
response.Body = mHttpClient.execute(request, handler);
}
catch (HttpResponseException e)
{
}
catch (Throwable e)
{
}
return response;
}
我有另一个与 GSON 流媒体一起使用的 ContentProducer(它正在工作):
public ContentProducer getContentProducer(final Context context)
{
return new ContentProducer()
{
public void writeTo(OutputStream outputStream) throws IOException
{
Gson myGson = MyGsonWrapper.getMyGson();
JsonWriter writer = new JsonWriter(new OutputStreamWriter(outputStream, "UTF-8"));
writer.beginObject();
// stuff
writer.endObject();
writer.flush();
}
};
}
我的问题是:如何使我的第一个示例工作。我做得对吗?现在我在服务器端收到空帖子,所以似乎没有数据通过。
【问题讨论】: