0

I have to upload multiple images included in JSON from android.

{
       "topicId" : 1,
       "QuizTest": [
            {
                "question": "This first question for test",
                "Image" : "file:///D:/Images/fb_logo.png",
                "isTrue" : 3,
                "OptionFor":
                    {
                        "1" : "i am option 1",
                        "2" : "i am option 2",
                        "3" : "i am option 3",
                        "4" : "i am option 4"
                    }
                
            },
            {
                "question": "This second question for test",
                "Image" : "",
                "isTrue" : 2,
                "OptionFor": 
                    {
                        "1" : "i am  2 option 1",
                        "2" : "i am 2 option 2"
                     }
                
            },
            {
                "question": "This third question for test",
                "Image" : "file:///D:/Images/icon.png",
                "isTrue" : 1,
                "OptionFor": 
                    {
                        "1" : "i am 3 option 1",
                        "2" : "i am 3 option 2",
                        "3" : "i am 3 option 3",
                        "4" : "i am 3 option 4",
                        "5" : "i am 3 option 5"
                     }
                
            }
        ]
}

I have data like this to upload.

I have tried with a volley, android networking. But I am not sure if I can upload files like this.

I have uploaded with json in body. Doesn't work. Please help.

Sachin Harne
  • 175
  • 2
  • 14

1 Answers1

1

You can use Retrofit for API calls where you can send payload as @Body or however required by API provider. You can set Gson converter in Retrofit builder so that you don't have to write plain Json objects.

If you are using Kotlin, you can write data classes like below

data class Options(
  val 1: String,
  val 2: String,
  val 3: String,
  val 4: String,
  val 5: String 
)
data class Question(
  val question: String,
  val Image: String,
  val isTrue: Int,
  val OptionFor: Options
)
data class Test(
  val topicId:Long,
  val QuizTest: Questions
)

And then you can send them as payload in API call

@POST("{api_end_point}")
fun sendData(@Body test: Test)
  • I have my JSON data ready to upload as mentioned in the question above. Will this upload image files also? – Sachin Harne Jul 06 '20 at 08:02
  • No @SachinHarne, for that you need to use `(@Part file: MultipartBody.Part`) format. Basically `Retrofit` lets you upload files as multipart data, you can read this in their official documentation. – Chandra Pratap Singh Jul 06 '20 at 08:06
  • Consider uploading the files separately while selecting them from storage and finally send the image url in final payload. – Chandra Pratap Singh Jul 06 '20 at 08:09
  • Yes, I can upload files separately as a Multipart with the final payload. So you mean to say I can't upload this only with this JSON, I have to upload files separately and JSON separately right? – Sachin Harne Jul 06 '20 at 11:21
  • Yes @SachinHarne. You can upload files together along with Body params too but in that case your server needs to be smart to map these files to their respective objects. So, prefer the former solution only. – Chandra Pratap Singh Jul 07 '20 at 09:13