Android Question Problem to upload file EncodeFromImage

Calvin Yee

Member
Licensed User
I'm trying to implement a file upload from my device to a Web API and Encode from image.
And Error encounter is as below:-

java.lang.NullPointerException: Attempt to invoke virtual method 'void anywheresoftware.b4h.okhttp.OkHttpClientWrapper$OkHttpRequest.InitializePost2(java.lang.String, byte[])' on a null object reference
at anywheresoftware.b4a.samples.httputils2.httpjob._postbytes(httpjob.java:284)
at anywheresoftware.b4a.samples.httputils2.httpjob._poststring(httpjob.java:423)

B4X:
Private Base64Con As Base64Image
PicFileName=IS001.jpeg
Private B64Str As String = Base64Con.EncodeFromImage(File.DirDefaultExternal, "IS001.jpeg")
Dim json As HttpJob
Dim m As String  = $"?DocNo=${DocNo}&PicFileName=${PicFileName}&PicImage=${B64Str}"$

            json.PostString(Starter.URILink & "/api/Customer/UploadDocument" & m,"")
            json.GetRequest.SetHeader("Authorization","Bearer " & kvs.GetSimple("CurrUserTokenAccess"))
            json.GetRequest.SetContentType("application/json")
            Wait For (json) JobDone(json As HttpJob)
            If json.Success Then
                Log("dd:" & json.GetString)
                Dim jNDparser As JSONParser
                jNDparser.Initialize(json.GetString)
                Dim root As Map = jNDparser.NextObject
                Dim result As String = root.Get("result")
                Dim data As String = root.Get("data")
                Dim resultset As String = root.Get("resultset")
                Log("data:" & data.Trim)
                If data.Trim ="Successful" Then
                    str="UPDATE Photo SET ImageSync='Y' "
                    str=str & " WHERE Docno='" & DocNo & "' "
                    SQL1.ExecNonQuery(str)
                End If
            End If
            json.Release
 

Calvin Yee

Member
Licensed User
Hi,
Modified as per:-
1. You haven't initialized the HttpJob.
2. Don't use File.DirDefaultExternal. Watch the runtime permissions video tutorial for more information.

And Error encounter is as below:-
ResponseError. Reason: Request-URI Too Long, Response: <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN""http://www.w3.org/TR/html4/strict.dtd">
<HTML><HEAD><TITLE>Request URL Too Long</TITLE>
<META HTTP-EQUIV="Content-Type" Content="text/html; charset=us-ascii"></HEAD>
<BODY><h2>Request URL Too Long</h2>
<hr><p>HTTP Error 414. The request URL is too long.</p>
</BODY></HTML>


this is output from EncodeFromImage:-

DocNo=IN638456&PicFileName=IN638456_2.jpeg&PicImage=/9j/4AAQSkZJRgABAQAAAQABAAD/2wBDAAEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQH/2wBDAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQH/wAARCAJYAcIDASIAAhEBAxEB/8QAHwAAAAYCAwEAAAAAAAAAAAAABQYHCAkKAAQCAwsB/8QAWRAAAQQCAQMCBAMFAwgFCAITAwECBAUGBxEIEhMAIQkUIjEVQVEKFiMyYUJxgRckM1KRobHRGGLB0vAlQ1OCkqLC4Rk0cpOy4vEmVGPT4ycoNjhVV3eDlP/EAB4BAAEEAwEBAQAAAAAAAAAAAAYEBQcIAgMJAQAK/8QAVBEAAgECBAQEAwQHBQQHBgMJAQIDBBEFBhIhAAcxQRMiUWEIMnEUgZGhFSNCUrHR8AkWM2LBJHLh8UNTgpKissIXGCVUVWMZNJMmNkRWV2SUo9L/2gAMAwEAAhEDEQA/AKQ0OeOOwjO9eXIiJ2pzz9SL7c8J9kVffj/FeE9B9qVslWOG5z14ci93Ht9v6/n6BmmVHqndz2pz7fn9k9uXIn5/r65LJRFaiov1c8cq32445+zn8/7v8fy6HGtpLeBDOjh7d7EkWI6gX6D+rcAUdNMrglLAX7r3H19/yPpxxcEniREbyv1eyK1V93P45RFVU55T8vXf8g8hXL2sRisI5FLIZGYviE4nClI8aI/6FQY0d3nJ2hE15CNa4aqo0eVx5ZCBRVT2aLl/8yp7vV/Cr7cp9KcJwi+/px2s8QqMgvYEUjY1lIhTYnz1E056w0pJUWrLBrmWFjRygxJ93LnzINSw9ZIeYdc+e4YBlRI4tm7EaHD8FxCv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ScRgsPziVVY3MdBqM3x3M4wXC/EJ9FDyWqFEFwxxztjZhSY5amDGe5rHubWMcVO4gmOEiPXbSY9Bl6ulxqPDDDiYskCBlcshPnOpAwGnybGx3tx1vosv5ByrmyozziGA0wzHWw06S1NQVkCtTxKl4mRXJve76Cf2bnpaSXS/V5tDTM21mYFnFtCgG+fOegly6+Zi1pGs6ywZcRbTHsgcseSZsCIpBy4zWtjPYDtMp3gYtsvpOyXD+rET8kzmlgyaig1f055nnePXVU+0ZA2dZY/i+c6wq66jMJ5wWWATRLl+SVUeM2FVNyTGsfskkWVfbVFTSw6ebCg1bvDBI3UNByHCteTjRrnJiy6tCZFExeRGtQ1mVwcekx5MeeABlSXVyAyinhX8LyPiSw1UiBaWGM++Mt0k6r1bhOAdNtwXJB4tWWFbWOsH5Brts80O4bbUV7lxbeHT2mR3t1NaK52HPj1tOmWZE2XZ1MitqpkNrg/NWITZunjkqLUomYO0s7CIE9GFpChspBUHa/Vbi54gLn4KfPmIYfQYThtPS12IHVV4wZ4KaF1DWjstS0D3SJVj2vcofVb2McszSpLHrLKvkNaCbKtiSZUmBXxgri84MoUAsdsow40chjOjKoAv/ABBUR4iAa8jmK5bHpLbccuarDsKQ5PI2RCFDVU+YlKrmINXNKz5lZQ0cn0/w+5iuG9rlqWdDI+pr4nnUFX7O2Ey6qdK4qWRkrMorYsaroJE2lClHCxLDq5LGaxZUqZIcS0spcqwhpWx7kYAsmmilj296yLIixkjSfA0g3PejI7XNGxhnuejEY4plTh3f/wCeciqqr2s/OFua1JhNLRYVRUNZDUPTlvGWNlfTcRgXsSCb3B0sSN78VHz7lmmyrV4VQLjcG


B4X:
Private Base64Con As Base64Image
PicFileName=IS001.jpeg
Private B64Str As String = Base64Con.EncodeFromImage(DirFolder, "IS001.jpeg")
Dim json As HttpJob
Dim m As String  = $"?DocNo=${DocNo}&PicFileName=${PicFileName}&PicImage=${B64Str}"$
json.Initialize("MyJobPostPhoto",Me)
            json.PostString(Starter.URILink & "/api/Customer/UploadDocument" & m,"")
            json.GetRequest.SetHeader("Authorization","Bearer " & kvs.GetSimple("CurrUserTokenAccess"))
            json.GetRequest.SetContentType("application/json")
            Wait For (json) JobDone(json As HttpJob)
            If json.Success Then
                Log("dd:" & json.GetString)
                Dim jNDparser As JSONParser
                jNDparser.Initialize(json.GetString)
                Dim root As Map = jNDparser.NextObject
                Dim result As String = root.Get("result")
                Dim data As String = root.Get("data")
                Dim resultset As String = root.Get("resultset")
                Log("data:" & data.Trim)
                If data.Trim ="Successful" Then
                    str="UPDATE Photo SET ImageSync='Y' "
                    str=str & " WHERE Docno='" & DocNo & "' "
                    SQL1.ExecNonQuery(str)
                End If
            End If
            json.Release

And now example on my WS side:
B4X:
             private ResultJsonTemplate DoUpload_Document(string DocNo, string PicFileName, Byte PicImage)
 
Upvote 0
Top