Android Question HttpJob Download File Error

Tilesoft

Member
Hi.
I use this code to download a file
B4X:
Sub Button1_Click
    Dim job As HttpJob
    job.Initialize("j", Me)
    job.Download("https://b4x-4c17.kxcdn.com/android/forum/data/avatars/m/0/1.jpg")
End Sub

Sub JobDone(job As HttpJob)
    If job.Success Then
        Dim out As OutputStream = File.OpenOutput(File.DirInternal, "number.jpg", False)
        File.Copy2(job.GetInputStream, out)
        out.Close '<------ very important
        Log("number download done")
        Dim number As Int
        number = File.ReadString(File.DirInternal, "number.txt")
        number = number+1
        File.WriteString(File.DirInternal,"number.txt", number)
        StartActivity(Post_Publish)
    Else
        Log("Error: " & job.ErrorMessage)
    End If
    job.Release
End Sub

But when run it, i get some errors. I attached errors photo.
 

Attachments

  • Capture.PNG
    41.3 KB · Views: 206

Tilesoft

Member
Thanks i changed my code
B4X:
Sub Activity_Create(FirstTime As Boolean)
    Activity.LoadLayout("Number_Download_Layout")
    
    Dim  Explore_Show_Trailerjob As HttpJob
    Explore_Show_Trailerjob.Initialize("Explore_Show_Trailerjob", Me)
    Explore_Show_Trailerjob.Download("https://b4x-4c17.kxcdn.com/android/forum/data/avatars/m/0/1.jpg")
    Wait For (Explore_Show_Trailerjob) JobDone(Explore_Show_Trailerjob As HttpJob)
    If Explore_Show_Trailerjob.Success Then
        Dim out1 As OutputStream = File.OpenOutput(File.DirInternal, "Explore_Show_Trailer.jpg", False)
        File.Copy2(Explore_Show_Trailerjob.GetInputStream, out1)
        out1.Close '<------ very important
        Log("Game1trailer_Show")
    End If
    Explore_Show_Trailerjob.Release
End Sub

---------------
My log is

B4X:
** Activity (number_download) Create, isFirst = true **
number_download$ResumableSub_Activity_Createresume (java line: 387)
java.lang.NoClassDefFoundError: Failed resolution of: Lorg/apache/http/client/methods/HttpGet;
    at anywheresoftware.b4a.http.HttpClientWrapper$HttpUriRequestWrapper.InitializeGet(HttpClientWrapper.java:342)
    at anywheresoftware.b4a.samples.httputils2.httpjob._download(httpjob.java:71)
    at lila.project.number_download$ResumableSub_Activity_Create.resume(number_download.java:387)
    at lila.project.number_download._activity_create(number_download.java:357)
    at java.lang.reflect.Method.invoke(Native Method)
    at anywheresoftware.b4a.BA.raiseEvent2(BA.java:213)
    at lila.project.number_download.afterFirstLayout(number_download.java:105)
    at lila.project.number_download.access$000(number_download.java:17)
    at lila.project.number_download$WaitForLayout.run(number_download.java:83)
    at android.os.Handler.handleCallback(Handler.java:883)
    at android.os.Handler.dispatchMessage(Handler.java:100)
    at android.os.Looper.loop(Looper.java:224)
    at android.app.ActivityThread.main(ActivityThread.java:7561)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:539)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:995)
Caused by: java.lang.ClassNotFoundException: org.apache.http.client.methods.HttpGet
    ... 16 more
 
Upvote 0

Mahares

Expert
Licensed User
Longtime User
i changed my code
Try something like this. Adapt it if you are are using a classic project:
B4X:
Private Sub B4XPage_Created (Root1 As B4XView)
    Root = Root1
    Link= "https://b4x-4c17.kxcdn.com/android/forum/data/avatars/m/0/1.jpg"  'declared in Class_Globals
    DownloadImage(Link)
End Sub

B4X:
Sub DownloadImage(myLink As String)
    Dim j As HttpJob   'need OkhttpUtils2 lib
    j.Initialize("", Me)
    j.Download(myLink)
    Wait For (j) JobDone(j As HttpJob)
    If j.Success Then
        Dim out1 As OutputStream = File.OpenOutput(File.DirInternal, "Explore_Show_Trailer.jpg", False)
        File.Copy2(j.GetInputStream, out1)
        out1.Close
        Log("done")
    Else
        Log("Error: " & j.ErrorMessage)
        ToastMessageShow("Error: " & j.ErrorMessage, True)
    End If
    j.Release
End Sub
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…