Android Question okHttpUtils2: can't download an image

Bossolo

Member
Hi everyone,
i create a serve on InfinityFree just to host some pic that i need do download and show in a image view.
The webserver has a certificate (a free one that offer the site) and has the TLS Protocol active (1.1,1.2 n 1.3)
When i try to download the job, the complete event doesn't raise. I've tried all the solutions that i found in the forum but whithout success.

Probably is a problem of the server, but i can't figure what can be or how to solve it.
The link is: https://testwebserver.42web.io/files/immagini


Certificati.png


B4A App:
Sub Activity_Create(FirstTime As Boolean)
    Dim Pic1 As String = "https://testwebserver.42web.io/files/immagini/20898.jpg"
    Dim j As HttpJob
    Log("Start job")
    
    j.Initialize("", Me) 'name is empty as it is no longer needed
    j.Download(Pic1)
    Wait For (j) JobDone(j As HttpJob)
    If j.Success Then
        Dim img As Bitmap
        img.Initialize3(j.GetBitmapresize(Activity.Width-10dip,Activity.Height-10dip,True))
        Activity.SetBackgroundImage(img)
    End If
    j.Release
End Sub
 

drgottjr

Expert
Licensed User
Longtime User
so, you've got a couple issues.
to get past the cert validity problem, you go into your b4a ide project tab, go into buildconfigurations and add: HU2_ACCEPTALL to the input box labeled conditional symbols. obviously this is a security risk, but in this case, it will at least allow the job done event to raise.

after you've done that and run the job again, you should see the app crash (at least that's what happens on my device). the problem is the jpg you're downloading is not a jpg. it's html with a javascript source file. if you access the url (of your job) with chrome, it will first tell you the site's cert is bad. if you ignore that and force the load, you will see the image which, presumably, you were looking for. chrome simply follows the instructions which lead to the image. httputils can't do that. you told httputils to download an image (understandable). it tries and fails. httputils is not a browser.

if you log j.getstring instead of j.getbitmap, you'll see the actual html returned by the server. if you were to capture that string and feed it to a webview, in theory you could see your image file. but it's not going to work in this case, because the javascript file is not local to your device. webview won't know where to go to get it. i hope all is clear.

edit: attached is actual "image" sent by the server. open it with a text editor. if you open it with a browser, you won't see much.
 
Upvote 0

Bossolo

Member
so, you've got a couple issues.
to get past the cert validity problem, you go into your b4a ide project tab, go into buildconfigurations and add: HU2_ACCEPTALL to the input box labeled conditional symbols. obviously this is a security risk, but in this case, it will at least allow the job done event to raise.

First of all, thank for the reply.
I know the conditional symbol setting, i've already try it, and as u say it don't function. Crash confirmed

after you've done that and run the job again, you should see the app crash (at least that's what happens on my device). the problem is the jpg you're downloading is not a jpg. it's html with a javascript source file. if you access the url (of your job) with chrome, it will first tell you the site's cert is bad. if you ignore that and force the load, you will see the image which, presumably, you were looking for. chrome simply follows the instructions which lead to the image. httputils can't do that. you told httputils to download an image (understandable). it tries and fails. httputils is not a browser.

Yeah i notice this, is the cert's fault. For test i use a free one, but i use to think that the file is always reachable but i was wrong

if you log j.getstring instead of j.getbitmap, you'll see the actual html returned by the server. if you were to capture that string and feed it to a webview, in theory you could see your image file. but it's not going to work in this case, because the javascript file is not local to your device. webview won't know where to go to get it. i hope all is clear.

edit: attached is actual "image" sent by the server. open it with a text editor. if you open it with a browser, you won't see much.

I'm trying to do this just to understand better what to do.
Thanks a lot for the explanation, now it's more clear.

In the end, what u suggest me to do? change the certificate of the webserver with a good one; or maybe change the entire solution?
I just need a secure space online where stored image/audio to show/stream
 
Upvote 0

Bossolo

Member
Don't waste your time with activities. Switch to B4XPages.

I'll try to convert it in these days. I don't know so much yet how to manage B4XPages, that's because i used an activity

OkHttpUtils2 will never crash because of an invalid signature.
Please post the full logs.

Here the log filtered:

Logger connesso a: samsung SM-G965F
--------- beginning of main
*** Service (starter) Create ***
** Service (starter) Start **
** Activity (main) Create, isFirst = true **
Start job
** Activity (main) Resume **
*** Service (httputils2service) Create ***
(Http client initialized with accept all option.)
** Service (httputils2service) Start **
Error occurred on line: 333 (HttpJob)
java.lang.RuntimeException: Error loading bitmap.
at anywheresoftware.b4a.objects.drawable.CanvasWrapper$BitmapWrapper.initializeSampleImpl(CanvasWrapper.java:637)
at anywheresoftware.b4a.objects.drawable.CanvasWrapper$BitmapWrapper.InitializeResize(CanvasWrapper.java:548)
at anywheresoftware.b4a.keywords.Common.LoadBitmapResize(Common.java:1370)
at b4a.example.httpjob._getbitmapresize(httpjob.java:124)
at b4a.example.main$ResumableSub_Activity_Create.resume(main.java:458)
at anywheresoftware.b4a.shell.DebugResumableSub$DelegatableResumableSub.resumeAsUserSub(DebugResumableSub.java:48)
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$DelegatableResumableSub.resume(DebugResumableSub.java:43)
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:883)
at android.os.Handler.dispatchMessage(Handler.java:100)
at android.os.Looper.loop(Looper.java:237)
at android.app.ActivityThread.main(ActivityThread.java:8167)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:496)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1100)
** Activity (main) Pause, UserClosed = true **
** Service (starter) Destroy (ignored)**
** Service (httputils2service) Destroy **
 
Upvote 0

drgottjr

Expert
Licensed User
Longtime User
Don't waste your time with activities. Switch to B4XPages.


OkHttpUtils2 will never crash because of an invalid signature.
Please post the full logs.
it doesn't crash because of the cert. it crashes because the "bitmap" isn't a bitmap. j.getbitmap(resize) causes the exception, not the cert. the resource is actually text.
 
Upvote 0

drgottjr

Expert
Licensed User
Longtime User
First of all, thank for the reply.
I know the conditional symbol setting, i've already try it, and as u say it don't function. Crash confirmed



Yeah i notice this, is the cert's fault. For test i use a free one, but i use to think that the file is always reachable but i was wrong



I'm trying to do this just to understand better what to do.
Thanks a lot for the explanation, now it's more clear.

In the end, what u suggest me to do? change the certificate of the webserver with a good one; or maybe change the entire solution?
I just need a secure space online where stored image/audio to show/stream
even if the cert had been valid, you still would have had the crash. i suggested the conditional symbol simply to get you past problem #1. the real problem is that the resource you requested is not a bitmap. the resource you requested leads to something else. a browser can handle that. the only way for you to download that image in an app is to use webview and to capture it as a bitmap (one of webview's methods).

httputils did exactly what you told it to do. there was an exception because you told it to do the wrong thing (obviously, you didn't know it was the wrong thing). httputils is not the tool to use in this case.
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
As stated by @drgottjr the result is not a bitmap.

B4X:
    Dim Pic1 As String = "https://testwebserver.42web.io/files/immagini/20898.jpg"
    Dim j As HttpJob
    
    Log("Start job")
    
    j.Initialize("", Me) 'name is empty as it is no longer needed
    j.Download(Pic1)
    Wait For (j) JobDone(j As HttpJob)
    If j.Success Then
        Dim res As String = j.GetString
        Log(res)
    End If
    j.Release

this is the log
B4X:
Start job
Call B4XPages.GetManager.LogEvents = True to enable logging B4XPages events.
(Http client initialized with accept all option.)
<html><body><script type="text/javascript" src="/aes.js" ></script><script>function toNumbers(d){var e=[];d.replace(/(..)/g,function(d){e.push(parseInt(d,16))});return e}function toHex(){for(var d=[],d=1==arguments.length&&arguments[0].constructor==Array?arguments[0]:arguments,e="",f=0;f<d.length;f++)e+=(16>d[f]?"0":"")+d[f].toString(16);return e.toLowerCase()}var a=toNumbers("f655ba9d09a112d4968c63579db590b4"),b=toNumbers("98344c2eee86c3994890592585b49f80"),c=toNumbers("fba680bddd05e956856a66276beb09cb");document.cookie="__test="+toHex(slowAES.decrypt(c,2,a,b))+"; expires=Thu, 31-Dec-37 23:55:55 GMT; path=/"; location.href="https://testwebserver.42web.io/files/immagini/20898.jpg?i=1";</script><noscript>This site requires Javascript to work, please enable Javascript in your browser or use a browser with Javascript support</noscript></body></html>
The last line is what you get from the Server. As you can see it is not a Bitmap!
 
Upvote 0

Bossolo

Member
even if the cert had been valid, you still would have had the crash. i suggested the conditional symbol simply to get you past problem #1. the real problem is that the resource you requested is not a bitmap. the resource you requested leads to something else. a browser can handle that. the only way for you to download that image in an app is to use webview and to capture it as a bitmap (one of webview's methods).

httputils did exactly what you told it to do. there was an exception because you told it to do the wrong thing (obviously, you didn't know it was the wrong thing). httputils is not the tool to use in this case.
Yes know i understand how it work and special how this webserver work. Now i'm evaluing other option to manage this project

Thanks a lot for the help, it was really helpfull
 
Upvote 0
Top