简体   繁体   English

在Java / Android中以UTF-8发布文本和文件

[英]Post text and files in Java/Android in UTF-8

I have the following code for posting a text and a file with http: 我有以下代码用于使用http发布文本和文件:

        HttpPost post = new HttpPost(url);
        MultipartEntity entity = new MultipartEntity();
        entity.addPart("field_1", new StringBody(textField));
        entity.addPart("field_2", new FileBody(new File(filepath)));
        post.setEntity(entity);
        HttpClient httpclient = new DefaultHttpClient();
        HttpResponse response = httpclient.execute(post);

It works nice the problem is that I need the textField value to be encoded in UTF-8.Any ideas? 问题很好,那就是我需要将textField值编码为UTF-8。有什么想法吗?

Editted: Found an error in the code, posting the working answer 编辑:发现代码中的错误,发布有效答案

解决方法是使用:

new StringBody(textField,"text/plain",Charset.forName("UTF-8")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM