Android Question [SOLVED] POST map and image (PostMultipart) to API

ThePuiu

Active Member
Licensed User
Longtime User
I want to make a POST call to an API. In Postman I can make the call with the parameters:

form-data:
jsonKey = {"id":"1", "location":"home address"}
file = homepicture.jpg

I try with code:
B4X:
Dim jobSend As HttpJob
   
Dim fd As MultipartFileData
fd.Initialize
fd.KeyName = "file"
fd.Dir = PathPHOTO
fd.FileName = NamePHOTO
fd.ContentType = "image/jpeg"
        
Dim flist As List
flist.Initialize
flist.Add(fd)

Dim NameValues As Map
NameValues.Initialize
NameValues.Put("id", "1")
NameValues.Put("location", "home address")

jobSend.Initialize("JobSend", Me)
jobSend.PostMultipart("http://api.xxxxxxxxx.eu/api/uploadfile",NameValues,flist)

something is wrong because the message received in JobDone is:
ResponseError. Reason: Not Acceptable, Response: "Object reference not set to an instance of an object."
I assume NameValues is not formatted correctly...

Can anyone help me with an idea?
Thank you!
 

ThePuiu

Active Member
Licensed User
Longtime User
there seems to be another error. I will post the full code, maybe it will be easier for you to find the answer ...
B4X:
Sub CC_Result(Success As Boolean, Dir As String, FileName As String)
    If Success Then
        File.Copy(Dir, FileName, File.DirInternal, "sesizare.jpg")  
        ImageViewPoza.Bitmap = LoadBitmap(Dir.Internal, "sesizare.jpg")
    End If  
End Sub

Sub ButtonTrimite_Click
    Dim jobSend As HttpJob
       
    Dim fd As MultipartFileData
    fd.Initialize
    fd.KeyName = "file"
    fd.Dir = File.DirInternal
    fd.FileName = "sesizare.jpg"
    fd.ContentType = "image/jpeg"
           
    Dim flist As List
    flist.Initialize
    flist.Add(fd)
           
    Dim NameValues As Map
    NameValues.Initialize
   
    NameValues.Put("id", "1")
    NameValues.Put("location", "home address")

           
    jobSend.Initialize("JobSendSesizare", Me)
    ProgressDialogShow2("Asteptati va rog...", False)

    jobSend.PostMultipart("http://api.xxxxxxxx.eu/api/uploadfile",NameValues,flist)  
    jobSend.GetRequest.Timeout = 60000
   
    Wait For (jobSend) JobDone(jobSend As HttpJob)
    Dim data As String = jobSend.GetString
    jobSend.release
    Log(data)
    ProgressDialogHide
End Sub

error is:
B4X:
ResponseError. Reason: Not Acceptable, Response: "Object reference not set to an instance of an object."
Error occurred on line: 234 (HttpJob)
java.io.FileNotFoundException: /data/user/0/xxi.smartcity/cache/2 (No such file or directory)
    at java.io.FileInputStream.open0(Native Method)
    at java.io.FileInputStream.open(FileInputStream.java:231)
    at java.io.FileInputStream.<init>(FileInputStream.java:165)
    at anywheresoftware.b4a.objects.streams.File.OpenInput(File.java:214)
    at xxi.smartcityhall.httpjob._getstring2(httpjob.java:529)
    at xxi.smartcityhall.httpjob._getstring(httpjob.java:110)
    at java.lang.reflect.Method.invoke(Native Method)
    at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:732)
    at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:348)
    at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:255)
    at java.lang.reflect.Method.invoke(Native Method)
    at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:144)
    at anywheresoftware.b4a.BA.raiseEvent(BA.java:193)
    at anywheresoftware.b4a.shell.DebugResumableSub$RemoteResumableSub.resume(DebugResumableSub.java:22)
    at anywheresoftware.b4a.BA.checkAndRunWaitForEvent(BA.java:267)
    at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:137)
    at anywheresoftware.b4a.BA$2.run(BA.java:387)
    at android.os.Handler.handleCallback(Handler.java:873)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loop(Looper.java:216)
    at android.app.ActivityThread.main(ActivityThread.java:7266)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:494)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:975)
 
Upvote 0
Top