Android Question HTTPServer questions

William Hunter

Active Member
Licensed User
Longtime User
I have been working with Erel’s HTTP Server example, as illustrated in the tutorial. I don’t fully understand the action of some of the code statements I have been using, and need the answers to a few questions.

1. Each of code examples below will return the path “mnt/sdcard/download/WIFItransfer.apk “, which is correct. My question is, are either of these two statements more correct in use than the other, and if so why?
Code: Copy
' example a
Dim fileToDelete AsString = DecodePath(Request.RequestURI.SubString("/delete/".Length))
' example b
'Dim fileToDelete As String = DecodePath(Request.RequestURI.SubString(7))

2. After deleting a file, I would like to refresh the list of files in the folder from which the file was deleted using the code below. Example a returns the path “mnt/sdcard/download/WIFItransfer.apk“, which is the path to the file just deleted. I would like to use the path to the folder from which that file was deleted (/mnt/sdcard/download). My question is, what changes would have to be made to example a, in order to return the folder path?
Code: Copy
' example a
Dim CurrentDirPath AsString = DecodePath(Request.RequestURI.SubString("/delete/".LastIndexOf("/")))
 ' example b
Response.SendRedirect("/list/" & EncodePath(CurrentDirPath))
' example c redirect to main list page
Response.SendRedirect("/list/")

I’ve searched the forum and done a Google search for info on Jetty, but can’t find the answers I need. Any help would be greatly appreciated.
 
1. "/delete/".Length = 8. The question is whether you need to delete the last slash as well. It depends on what you are doing with this string.

2. This is not related to Jetty. Do all three lines fail?

Thank you for your response Erel. No, all three lines do not fail. Perhaps I can explain my difficulty in a more clear way. I am using the code below to delete a file, and then redirect to the main list page. This works well. Note that the file I am deleting for testing is - mnt/sdcard/download/WIFItransfer.apk.

Code: Copy
Case Request.RequestURI.StartsWith("/delete/")
       SetContentType(Request.RequestURI, Response)
       'Dim CurrentDirPath As String = DecodePath(Request.RequestURI.SubString("/delete/".LastIndexOf("/")))
       Dim fileToDelete As String = DecodePath(Request.RequestURI.SubString("/delete/".Length))
       Log("fileToDelete: " & fileToDelete)
       File.Delete("", fileToDelete)
       Response.SendRedirect("/list/") 'redirect to the list page
       'Response.SendRedirect("/list/" & EncodePath(CurrentDirPath)) 'redirect to the list page

I would like to use the first line of Rem’d out code to find the path to the folder from which the file was deleted. Then use the second line of Rem’d out code to redirect to that folder, and refresh the list of files there. As it stands now, when using these two lines of code, I get this error - Error serving request: (IOException) java.io.IOException: /mnt/sdcard/download/WIFItransfer.apk is not a folder. I need to know how to modify that first line of Rem’d out code to give me the path to /mnt/sdcard/download.

I know B4A treats a string as an array of characters where the first character in the string is array element 0, and that SubString goes from BeginIndex to the end of the string. I’ve looked at string manipulation in the Beginners Guide, yet I still can’t find the syntax to find the needed path, for a redirect to the folder from which I have deleted a file. If this can be done, I would appreciate a code example showing how to do it.

Regards
 
Upvote 0
Maybe this sub will be helpful:

Thank you Erel. Your “Sub GetParent” was the answer to my needs.

For those interested, the following code additions in ServerService, along with Erel’s Sub GetParent, will add the ability to delete files, from the Android device, using Erel’s HTTP Server example.

Code: Copy
' in Sub Server_HandleRequest
Case Request.RequestURI.StartsWith("/delete/")
      SetContentType(Request.RequestURI, Response)
      Dim fileToDelete AsString = DecodePath(Request.RequestURI.SubString("/delete/".Length))
      Dim CurrentDir AsString = GetParent(fileToDelete)
      'Log("fileToDelete: " & fileToDelete)
      'Log("CurrentDir: " & CurrentDir)
      File.Delete("", fileToDelete)
      Response.SendRedirect("/list/" & EncodePath(CurrentDir)) 'redirect to the list page

Code: Copy
' in Sub HandleList
ForEach F AsStringIn files ' this code allows for either download or deletion
      listSb.Append("<li>")
      listSb.Append(F).Append(" (").Append("<a href='/download/").Append(EncodePath(File.Combine(CurrentPath, F))).Append("'>Download</a>)")
      ‘ ########### add the line below #############################
      listSb.Append(" - (").Append("<a href='/delete/").Append(EncodePath(File.Combine(CurrentPath, F))).Append("'>Delete</a>)") ' appends delete link
      ‘ ############################################################
      listSb.Append("</li>")
Next

Regards
 
Upvote 0
Back
Top