Android Question Non-HTTP get request?

Jeffrey Cameron

Well-Known Member
Licensed User
Longtime User
I'm working on interfacing a Star TSP100IIIBI printer for a customer which is a raster-only bluetooth printer. Star has an App called "Star PassPRNT" which is supposed to create a URI that you can send HTML to and have it print. The examples they have show it being done with javascript and simply linking it to an anchor link in the document:

JavaScript:
var passprnt_uri;

function createURI() {
      passprnt_uri = "starpassprnt://v1/print/nopreview?";

      passprnt_uri = passprnt_uri + "back=" + encodeURIComponent(window.location.href);

      passprnt_uri = passprnt_uri + "&html=" + encodeURIComponent("<html><head>...</body></html>");

      var target = document.getElementById("print");
      target.href = passprnt_uri;
}
<html>
      <head></head>
      <body>
         <a href="#" id="print">PRINT</a>
      </body>
</html>

When I attempt to submit to "starpassprnt://v1/print/nopreview?size=2..." via job.download2 I get an "Invalid-url" response with no address associated with host name. And yet, on Star's test page they have javascript that is setting the location.uri value to the same value that I am using and the printer prints their test page.

Is there something I need to set to navigate to a non-"http://" address using OkHttp or am I supposed to be using an intent somehow?
 
Solution
See how the custom scheme is implemented in the Google OAuth class.

B4X:
AddActivityText(Main,
  <intent-filter>
  <action android:name="android.intent.action.VIEW" />
  <category android:name="android.intent.category.DEFAULT" />
  <category android:name="android.intent.category.BROWSABLE" />
  <data android:scheme="custom.scheme.here" />
  </intent-filter>
   )

That was the secret sauce I was missing, thank you very much! For anyone else banging their head against this particular wall, and for completeness sake here's the test code that will produce output:
Star PassPRNT example call:
        Dim HtmlText As String = "<html><head /><body><span style='font-size:30px;'>Test Line 1<br />Test Line 2<br />.........1.........2.........3.........4<br...

Jeffrey Cameron

Well-Known Member
Licensed User
Longtime User
Something like:
B4X:
Dim html = ...
Dim su As StringUtils
html = su.EncodeUrl(html, "utf8")
Dim in As Intent
in.Initialize(in.ACTION_VIEW", $"starpassprnt://starpassprnt://v1/print/nopreview?html=${html}"$)
StartActivity(in)
Thank you, that does indeed invoke the printing app. Forgive my Android ignorance, is this what Chrome is doing in the background when it does not see "HTTP" as the header for the request?

Also, again please forgive my extreme lack of knowledge of Android mechanics, but the above method requires a "back" parameter that is a URI to the app making the request. How may I build a URI for my app with an extra that can indicate "Hey, I'm coming back from printing something" that may be encoded and appended to the initialization string?
 
Last edited:
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
Forgive my Android ignorance, is this what Chrome is doing in the background when it does not see "HTTP" as the header for the request?
Yes.

I'm not sure what the back parameter does so can't say much. In some cases, for example with OAuth, we need to create a small http server to handle the result.
[class][B4X] Google OAuth2
 
Upvote 0

Jeffrey Cameron

Well-Known Member
Licensed User
Longtime User
Yes.

I'm not sure what the back parameter does so can't say much. In some cases, for example with OAuth, we need to create a small http server to handle the result.
[class][B4X] Google OAuth2
In the scant few examples I've found the back parameter is the URL the app was invoked from (e.g. "back=http://webprnt.com/demofiles/PassPRNT/PassPRNT.html" URL encoded, obviously) or they're using some java function to build some type of app URI to "me" or "this" (and I can't find that one example of that I did see again, I think it was objective C anyway not Java ... /sigh).

I'm assuming that the parameter needs to be of the same format we created for "starpassprnt://" just using "whatever_my_app_URI_is://" instead? Again, my Android ignorance is showing and I apologize if this is something I should already know/understand. From what I can tell from the rather nebulous documentation the PassPRNT app will pass back some type of extras that indicate printer status and job result.
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
See how the custom scheme is implemented in the Google OAuth class.

B4X:
AddActivityText(Main,
  <intent-filter>
  <action android:name="android.intent.action.VIEW" />
  <category android:name="android.intent.category.DEFAULT" />
  <category android:name="android.intent.category.BROWSABLE" />
  <data android:scheme="custom.scheme.here" />
  </intent-filter>
   )
 
Upvote 1

Jeffrey Cameron

Well-Known Member
Licensed User
Longtime User
See how the custom scheme is implemented in the Google OAuth class.

B4X:
AddActivityText(Main,
  <intent-filter>
  <action android:name="android.intent.action.VIEW" />
  <category android:name="android.intent.category.DEFAULT" />
  <category android:name="android.intent.category.BROWSABLE" />
  <data android:scheme="custom.scheme.here" />
  </intent-filter>
   )

That was the secret sauce I was missing, thank you very much! For anyone else banging their head against this particular wall, and for completeness sake here's the test code that will produce output:
Star PassPRNT example call:
        Dim HtmlText As String = "<html><head /><body><span style='font-size:30px;'>Test Line 1<br />Test Line 2<br />.........1.........2.........3.........4<br /><br /></span></body></html>"
        Dim poSu As StringUtils
        Dim psEnc As String = poSu.EncodeUrl(HtmlText, "UTF8")
        Dim psBack As String = poSu.EncodeUrl("custom.scheme.here://", "UTF8")
        Dim psAppURI As String = "starpassprnt://v1/print/nopreview"
        Dim poIn As Intent
        poIn.Initialize(poIn.ACTION_VIEW, $"${psAppURI}?html=${psEnc}&drawer=ahead&size=3&back=${psBack}"$)
        StartActivity(poIn)
When the app resumes, you can look at the intent data and see the result from the printing program, here's some log output showing that:
After resume log:
** Activity (main) Resume **
Activity resumed
Intent ACTION: android.intent.action.VIEW
Intent Extras: no extras
Intent Data: custom.scheme.here://?passprnt_code=0&passprnt_message=SUCCESS
Thanks again everyone for their help! ?
 
Upvote 1
Solution
Top