整个过程完美无缺,只是图像无法显示,没有错误,使用 RoR。我错过了什么?顺便说一句,所有这些都由异步类调用。尝试了几种不同的方法都无济于事,如果有人能帮助我,那就太好了。如果需要,愿意发布更多内容。
谢谢!
public static void multiPart(Bitmap image, String topicid, String topost, Context c){
String responseString = "";
{
try {
String imageName = System.currentTimeMillis() + ".jpg";
HttpClient httpClient = new MyHttpClient(c);
HttpPost postRequest = new HttpPost("https://urlofmyapi");
if (image==null){
Log.d("TAG", "NULL IMAGE");
}
ByteArrayOutputStream bos = new ByteArrayOutputStream();
image.compress(CompressFormat.JPEG, 75, bos);
byte[] data = bos.toByteArray();
ByteArrayBody bab = new ByteArrayBody(data, imageName);
MultipartEntity reqEntity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
reqEntity.addPart("feed", new StringBody(topost));
reqEntity.addPart("post_to", new StringBody(topicid));
reqEntity.addPart("upload_file", bab);
postRequest.setEntity(reqEntity);
HttpResponse response = httpClient.execute(postRequest);
BufferedReader reader = new BufferedReader(
new InputStreamReader(
response.getEntity().getContent(), "UTF-8"));
String sResponse;
StringBuilder s = new StringBuilder();
while ((sResponse = reader.readLine()) != null) {
s = s.append(sResponse);
}
responseString = s.toString();
System.out.println("Response: " + responseString);
} catch (Exception e) {
Log.e(e.getClass().getName(), e.getMessage());
}
}
}
最佳答案
也许您可以按照以下步骤将库导入您的 Android。
需求库:
- apache-mime4j-0.6.jar
httpmime-4.0.1.jar
- 右键单击您的项目并单击属性
- 选择java构建路径
- 选择名为“订购和导出”的标签
- 应用它
- 由于现有 apk 不适合新库,因此使用 adb 卸载完全卸载您的 apk 文件
- 再次安装你的apk
- 运行它
谢谢,
珍妮
关于java - Android 上的 HTTP Post Multipart 不发布图像。,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9934609/