【问题标题】:Upload image to the server using multipartentity and json in android在android中使用多方和json将图像上传到服务器
【发布时间】:2014-03-30 10:45:38
【问题描述】:

我已经阅读了很多帖子来完成任务,比如在 android 中使用 multipartentity 和 josn 上传图像以及参数,但我的问题是当我尝试上传图像和参数而不将字符串转换为 JSONobject 然后图像有上传时没有错误,但是当我尝试将响应字符串添加到 jsonobject 时,logcat 中会出现错误,例如):

error: Value`<form of type java.lang.String cannot be converted to JSONObject.

有人请帮忙解决这个问题吗?我想用MultipartEntity 将图像和JsonObject 发送到PHP 服务器。我正在开发一个允许用户使用 HttpPost 方法上传图像的应用程序。我使用MultipartEntity,因此我将库 apache-mime4j-0.6.1.jar、httpclient-4.3.1.jar、httpcore-4.3.1.jar 和 httpmime-4.2.1.jar 添加到我的应用程序中。

这是我的代码:

    public JSONObject doFileUpload(String _fname, String _lname, String _email,
                String _password, String _country, String _countrycode,
                String _phone) {

            File file1 = new File(selectedPath);
            String urlString = "http://capstonehostingservices.com/fetch_new/app/index.php/fetch/register";
            try {
                HttpClient client = new DefaultHttpClient();
                HttpPost post = new HttpPost(urlString);
                FileBody bin1 = new FileBody(file1);
                MultipartEntity reqEntity = new MultipartEntity();
                reqEntity.addPart("photo", bin1);
                reqEntity.addPart("first_name", new StringBody(_fname));
                reqEntity.addPart("last_name", new StringBody(_lname));
                reqEntity.addPart("email", new StringBody(_email));
                reqEntity.addPart("password", new StringBody(_password));
                reqEntity.addPart("country", new StringBody(_country));
                reqEntity.addPart("country_code", new StringBody(_countrycode));
                reqEntity.addPart("phone", new StringBody(_phone));

                post.setEntity(reqEntity);
                HttpResponse response = client.execute(post);
                resEntity = response.getEntity();
                 String response_str = EntityUtils.toString(resEntity);



                    json = new JSONObject(response_str);


            } catch (Exception ex) {
                Log.e("Debug", "error: " + ex.getMessage(), ex);
            }
            return json;
        }

在上面的代码中,我试图将响应字符串转换为 jsonobject,我该如何实现呢? 我使用 selectedpath 参数从图库中获取图像路径,我想将图像和JsonObject 发送到带有MultipartEntity 的 PHP 服务器。我正在开发一个允许用户使用HttpPost 方法上传图像的应用程序。我使用MultipartEntity,因此我将库 apache-mime4j-0.6.1.jar、httpclient-4.3.1.jar、httpcore-4.3.1.jar 和 httpmime-4.2.1.jar 添加到我的应用程序中。

【问题讨论】:

    标签: android json


    【解决方案1】:

    这可能不是最好的答案,但它是一个很好的入门示例。

    使用以下函数将文件发送到服务器:

    public static void postFile(String fileName) throws Exception {//fileName is path+filename of picture
            String url_upload_image = "http://url-to-api/upload_photo.php";
            HttpClient client = new DefaultHttpClient();
            HttpPost post = new HttpPost(url_upload_image);
            MultipartEntityBuilder multipartEntity = MultipartEntityBuilder
                    .create();
            multipartEntity.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
            multipartEntity.addPart("file", new FileBody(new File(fileName)));
            post.addHeader("id", id);//id is anything as you may need
            post.setEntity(multipartEntity.build());
            HttpResponse response = client.execute(post);
            HttpEntity entity = response.getEntity();
    
            entity.consumeContent();
            client.getConnectionManager().shutdown();
        }
    

    位于http://url-to-api/upload_photo.php的PHP文件

    <?php
        $name = $_POST['id'];
    
        $file_path = "images/";
    
        $file_path = $file_path . basename( $_FILES['file']['name']);
    
        if(move_uploaded_file($_FILES['file']['tmp_name'], $file_path)) {
            echo "success";
            echo $name;
        } else{
            echo "fail";
        }
     ?>
    

    并确保您在服务器中的目录或文件夹是可执行、可写和可读的。我认为这是主要问题。这就是称为 777 权限。相信我,这与其他要考虑的事情一样重要。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-04-19
      • 2013-12-17
      • 2014-03-21
      • 2014-03-24
      • 1970-01-01
      • 2015-11-25
      • 2011-11-18
      相关资源
      最近更新 更多