Android Question HttpJob Postmultipart with JSON and Image files

jobykpaul

Member
Licensed User
Hi

I want to upload data to the server using HttpJob Postmultipart method. My data include JSON string and Two image files. Could you help me how to send these items in single post request?

When i use JSON string instead of Map data , the IDE showing the warning Type mismatch..

<Code>
Dim Map1 As Map
Map1.Initialize
Dim JSONGenerator1 As JSONGenerator
Dim JSONstring As String
Map1.put("id","1")
Map1.put("Number","2")
JSONGenerator1.Initialize(Map1)
JSONstring=JSONGenerator1.ToString

j.Initialize("", Me)
JSONstring=JSONGenerator1.ToString'.ToString
j.GetRequest.SetContentType("application/json")

j.PostMultipart("http://154.15.225.11/api/add_dev",JSONstring,Null)

' Here Type doesnot match warning 22



<End Code>

Fisrt try with JSON string, after its success then i need to do with image uploading .

Thank You for your help.
 

Peter Simpson

Expert
Licensed User
Please use code tags when posting code.
Code_Tags.jpg
 
Upvote 0

nibbo

Active Member
Licensed User
PostMultipart NameValues expects a map but you have supplied a string.
Here is one I did:

B4X:
    Dim job As HttpJob
    job.Initialize("Wahtever", Me)
    
    '    add the NameValues to a map
    Dim m As Map
    m.Initialize
    m.Put("JSON", JSONString)

    '    add the files to a list
    Dim files As List
    files.Initialize
    Dim fd1 As MultipartFileData
    fd1.Initialize
    fd1.KeyName = "UploadedFile1"
    fd1.Dir = File.DirInternal
    fd1.FileName = "MyFile.jpg"
    fd1.ContentType = "image/jpg"
    files.Add(fd1)

    job.PostMultipart(Main.webSite, m, files)

I don't think you need to set the content type as you are posting the JSON as a string using this method.
 
Upvote 0

nibbo

Active Member
Licensed User
Thank You will test and report you.

Actually, it depends on the api you are trying to use a bit, do you have access to the code?
If you have access to the api code you could change it to look at the request item to retrieve the JSON.
If not then my guess is that api will try to get the JSON data from the input stream which will not be populated by PostMultipart.
I don't think there is a valid method that you can use for your api…?
 
Upvote 0

jobykpaul

Member
Licensed User
Actually, it depends on the api you are trying to use a bit, do you have access to the code?
If you have access to the api code you could change it to look at the request item to retrieve the JSON.
If not then my guess is that api will try to get the JSON data from the input stream which will not be populated by PostMultipart.
I don't think there is a valid method that you can use for your api…?


We are still having issues in postmulitpart code, i am trying to upload a text file to server, but there is no response from server my code

B4X:
Dim j As  HttpJob
Dim m As Map
    m.Initialize
    m.Put("file","Nil")
    
    Dim files As List
    files.Initialize
    Dim fd1 As MultipartFileData
    fd1.Initialize
    fd1.KeyName = "File"
    fd1.Dir = File.DirRootExternal
    fd1.FileName = file_upload
    fd1.ContentType = "text/plain"
    files.Add(fd1)   
    
    j.Initialize("", Me)
    
    j.PostMultipart("http://541.15.25.10/mobile/api/add_file",m,files)


The response data is
Status:200 (OK) message only, but when i upload file from the external POST tool, response message received

Thank You.
 
Upvote 0
Top