【问题标题】:JSON with text and image带有文本和图像的 JSON
【发布时间】:2015-09-18 14:19:18
【问题描述】:

我需要将有关用户的信息(包括头像)上传到服务器。到目前为止它工作正常,我使用 FTP 传输来上传图像,这意味着 JSON 中只有用户的文本信息。

但我想将两者结合在一个 JSON 中。像这样的:

{
    name: 'Jason Manson',
    age: 45,
    gender: 0,
    avatar: [IMG element]
}

这可能吗?如果是,如何在 Xcode、Android 和 php 中使用它?任何可用的样本。

这适用于两者,从应用程序发送到服务器并从服务器返回到应用程序。

【问题讨论】:

    标签: android ios json post


    【解决方案1】:

    您应该使用 Json 上传图片的多部分系统,您可以使用以下方法将带有 JSON(Text) 的单个或多个图像上传到服务器...! mImagePath 是图片路径的arraylist

    // Method for sending files using multiparting......
    public static String sendJsonWithFile(Activity mActivity, ArrayList<String> mImagePaths, String jsonString, String URL)
    {
        Log.e("json", jsonString);
        String res = "";
        try
        {
            HttpClient httpclient = new DefaultHttpClient();
            HttpPost httppost = new HttpPost(URL);
            String boundary = "*****" + Long.toString(System.currentTimeMillis()) + "*****";
            boundary = "--" + boundary;
            httppost.addHeader("Content-Type", "multipart/form-data; boundary=" + boundary);
            MultipartEntity reqEntity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
    
        StringBody stringBody = new StringBody(jsonString);
    
        reqEntity.addPart("formstring", stringBody);
    
        for (int i = 0; i < mImagePaths.size(); i++)
        {
            String imagePath = mImagePaths.get(i);
            if (mImagePaths != null && mImagePaths.size() > 0)
            {
    
                byte[] filebytes = FileUtils.readFileToByteArray(new File(imagePath));
    
                ByteArrayBody filebodyImage = new ByteArrayBody(filebytes, "image");
                Log.e("file path=", filebodyImage.toString());
    
                reqEntity.addPart("image", filebodyImage);
    
            }
    
        }
    
        httppost.setEntity(reqEntity);
        HttpResponse response = httpclient.execute(httppost);
        HttpEntity resEntity = response.getEntity();
        if (resEntity != null)
        {
            res = EntityUtils.toString(resEntity);
            System.out.println(res);
        }
    
        if (resEntity != null)
        {
            resEntity.consumeContent();
        }
        httpclient.getConnectionManager().shutdown();
    }
    catch (UnsupportedEncodingException e)
    {
        res = "UnsupportedEncodingException";
        e.printStackTrace();
    }
    catch (ClientProtocolException e)
    {
        res = "ClientProtocolException";
        e.printStackTrace();
    }
    catch (FileNotFoundException e)
    {
        res = "FileNotFoundException";
        e.printStackTrace();
    }
    catch (IOException e)
    {
        res = "IOException";
        e.printStackTrace();
    }
    catch (Exception e)
    {
        res = "Exception";
        e.printStackTrace();
    }
    return res;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-03-14
      • 1970-01-01
      • 1970-01-01
      • 2012-09-03
      相关资源
      最近更新 更多