我很新的流编码,但现在我必须做更有效的Http编码。处理Streams和ContentProducer - Java
下面是代码,我写的(不工作),以获得ContentProducer为HttpClient的:
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/ 我的目标是使用这个功能来提供数据我从二进制文件读取到HttpClient作为base64编码流。
这是我如何消费内容制作:
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;
}
我有另一个ContentProducer与GSON流光作品(它的工作):
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();
}
};
}
我的问题是:如何让我的第一次示例工作。我做得对吗?现在我在服务器端获得空邮件,所以似乎没有数据通过。
我应该如何修改我的第一个示例使其工作?我需要将ContentProducer与文件 – katit
中的Base64流进行合并,以便更清楚地说明 - 第一个示例是我的代码。我试图从文件中建立base64编码数据的生产者。 – katit
那么,你的制片人有问题吗?如果是这样,那么问题是什么?问题很少清晰时,很难提供帮助。我鼓励你编辑这个问题,试图给我们更多的细节,以了解每个代码段的含义,你的问题是什么,以及你尝试过什么...... –