java - 图片上传到服务器 android httppost

标签 java android

嗨,我在谷歌上搜索了很多有关从 Android 手机到服务器的图像上传的信息,并且我已经完成了以下代码

例如

HttpClient httpClient = new DefaultHttpClient();
   HttpContext localContext = new BasicHttpContext();
    HttpPost httpPost = new HttpPost(url);

    try {
        MultipartEntity entity = new MultipartEntity(
                HttpMultipartMode.BROWSER_COMPATIBLE);
        for (int index = 0; index < nameValuePairs.size(); index++) {
            if (nameValuePairs.get(index).getName()
                    .equalsIgnoreCase("image")) {
                entity.addPart(nameValuePairs.get(index).getName(),new FileBody(new File(
                                        nameValuePairs.get(
                                                index)
                                                .getValue()),
                                "image/jpg"));
         httpPost.setEntity(entity);

     HttpResponse httpResponse = httpClient.execute(httpPost,
                localContext);
        HttpEntity httpEntity = httpResponse.getEntity();
        is = httpEntity.getContent();

不知道为什么,但图像没有上传到服务器,我正在关注 logcat

      05-08 23:20:17.452: I/System.out(2501): Response from serveer <pre>Array
      05-08 23:20:17.454: I/System.out(2501): (
      05-08 23:20:17.455: I/System.out(2501):     [image] => Array
      05-08 23:20:17.455: I/System.out(2501):         (
      05-08 23:20:17.455: I/System.out(2501):             [name] => IMG_20130101_164850.jpg
      05-08 23:20:17.455: I/System.out(2501):             [type] => 
      05-08 23:20:17.455: I/System.out(2501):             [tmp_name] => C:\Windows\Temp\php5CD4.tmp
      05-08 23:20:17.456: I/System.out(2501):             [error] => 0
      05-08 23:20:17.456: I/System.out(2501):             [size] => 1988151
      05-08 23:20:17.456: I/System.out(2501):         )
      05-08 23:20:17.456: I/System.out(2501): )

这里来自服务器TYPE没有返回任何值,所以我无法提供更新

请任何人都可以帮助解决这个问题

最佳答案

我已经测试过,它完全可以工作

你只需要改变

这段代码

MultipartEntity 实体 = new MultipartEntity( HttpMultipartMode.BROWSER_COMPATIBLE);

MultipartEntity 实体 = new MultipartEntity( HttpMultipartMode.STRICT);

一切都会完成,所有其他代码都很好

关于java - 图片上传到服务器 android httppost,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23549222/

相关文章:

android - 使用 GStreamer 在 Android 上进行硬件视频解码

android - Phonegap 构建 facebook Connect 插件 (android)

java - 如何从数据库获取 spring-logback 的路径

java - 休息服务: select an appropriate method

Java/Mysql : Get all resulted lines from a stored procedure and not only the last

android - picasso 图书馆 - 内存不足

android - 访问调用者对象的 JNI 陈旧本地引用错误

android - 需要调用API以使用Retrofit更新android mvvm中的 token ,在哪里编写逻辑?

java - 生成给定长度的 ArrayList<String> 的所有排列

java - 使用什么来重播 ActiveMQ 中的消息以及为什么?