java - Android 上的 HTTP Post Multipart 不发布图像。

标签 java android ruby-on-rails http-post multipart

整个过程完美无缺,只是图像无法显示,没有错误,使用 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

    1. 右键单击您的项目并单击属性
    2. 选择java构建路径
    3. 选择名为“订购和导出”的标签
    4. 应用它
    5. 由于现有 apk 不适合新库,因此使用 adb 卸载完全卸载您的 apk 文件
    6. 再次安装你的apk
    7. 运行它

谢谢,

珍妮

关于java - Android 上的 HTTP Post Multipart 不发布图像。,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9934609/

相关文章:

android - 线程安全 RNG Android NDK

ruby-on-rails - 包括带有 :cache => true into a rails app 的大型 JS 库

java - 删除在第一个索引处停止的索引方法(java字符串数组)

android - NullPointerException imageView.setImageBitmap(位图)

java - 如何使用 volley 发布 boolean 值

ruby-on-rails - 每当 => cron 作业不访问我的模型/数据库时

ruby-on-rails - 在表单标签中包含链接吗?

java - 奇怪的java jvm内存不足

java - 除了命令行,我在哪里可以设置用于运行 Netbeans 的 JRE/JDK?

java - 同步方法是否会阻止对象字段被更新?