B4J Question Only Post / Put requests support this method.

aaronk

Well-Known Member
Licensed User
Longtime User
Hi,

I am using GoogleOAuth2 with a nonUI app, and when I run my code I am getting the following error:

Error occurred on line: 84 (HttpJob)
java.lang.RuntimeException: Only Post / Put requests support this method.
at anywheresoftware.b4a.http.HttpClientWrapper$HttpUriRequestWrapper.SetContentType(HttpClientWrapper.java:426)
at b4j.example.httputils2service._submitjob(httputils2service.java:181)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:613)
at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:231)
at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:159)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:90)
at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:93)
at anywheresoftware.b4a.keywords.Common.CallSub4(Common.java:480)
at anywheresoftware.b4a.keywords.Common.access$0(Common.java:460)
at anywheresoftware.b4a.keywords.Common$CallSubDelayedHelper.run(Common.java:534)
at anywheresoftware.b4a.keywords.SimpleMessageLoop.runMessageLoop(SimpleMessageLoop.java:30)
at anywheresoftware.b4a.StandardBA.startMessageLoop(StandardBA.java:26)
at anywheresoftware.b4a.ShellBA.startMessageLoop(ShellBA.java:114)
at anywheresoftware.b4a.keywords.Common.StartMessageLoop(Common.java:146)
at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:303)
at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:159)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:90)
at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:93)
at anywheresoftware.b4a.BA.raiseEvent(BA.java:77)
at b4j.example.main.main(main.java:29)
{
"kind": "androidpublisher#productPurchase",
"purchaseTimeMillis": "1498957648684",
"purchaseState": 0,
"consumptionState": 1,
"developerPayload": ""
}
Done!!!

The code I am using is:

B4X:
Sub CheckGooglePlayPayment (PurchaseId As String, PurchaseToken As String)

    oauth2.GetAccessToken
    Wait For OAuth2_AccessTokenAvailable (Success As Boolean, Token As String)
    If Success = False Then
        Log("Error accessing account.")
        Return
    End If
    Dim j As HttpJob
    j.Initialize("", Me)
    j.Download($"https://www.googleapis.com/androidpublisher/v2/applications/my.app.name/purchases/products/"$ & PurchaseId & $"/tokens/"$ & PurchaseToken)
   
    j.GetRequest.SetHeader("Authorization", "Bearer " & Token)
    Wait For (j) JobDone(j As HttpJob)
    If j.Success Then
        Log("** j.GetString **")
        Log(j.GetString)
    Else
        Log("Online data not available.")
    End If
    j.Release
    Log("Done!!!")
   
End Sub

My HttpJob code looks like:

B4X:
'HttpUtils2 version 1.00
'Class module
Sub Class_Globals
    Public JobName As String
    Public Success As Boolean
    Public Username, Password As String
    Public ErrorMessage As String
    Private target As Object
    Private taskId As String
    Private req As HttpRequest
    Public Tag As Object
End Sub

'Initializes the Job.
'Name - The job's name. Note that the name doesn't need to be unique.
'TargetModule - The activity or service that will handle the JobDone event.
Public Sub Initialize (Name As String, TargetModule As Object)
    HttpUtils2Service.Initialize
    JobName = Name
    target = TargetModule
End Sub
'Sends a POST request with the given data as the post data.
Public Sub PostString(Link As String, Text As String)
    PostBytes(Link, Text.GetBytes("UTF8"))
End Sub

'Sends a POST request with the given string as the post data
Public Sub PostBytes(Link As String, Data() As Byte)
    req.InitializePost2(Link, Data)
    CallSubDelayed2(HttpUtils2Service, "SubmitJob", Me)
End Sub

'Sends a POST request with the given file as the post data.
'This method doesn't work with assets files.
Public Sub PostFile(Link As String, Dir As String, FileName As String)
    Dim length As Int
    If Dir = File.DirAssets Then
        Log("Cannot send files from the assets folder.")
        Return
    End If
    length = File.Size(Dir, FileName)
    Dim In As InputStream
    In = File.OpenInput(Dir, FileName)
    If length < 1000000 Then '1mb
        'There are advantages for sending the file as bytes array. It allows the Http library to resend the data
        'if it failed in the first time.
        Dim out As OutputStream
        out.InitializeToBytesArray(length)
        File.Copy2(In, out)
        PostBytes(Link, out.ToBytesArray)
    Else
        req.InitializePost(Link, In, length)
        CallSubDelayed2(HttpUtils2Service, "SubmitJob", Me)
    End If
End Sub
'Submits a HTTP GET request.
'Consider using Download2 if the parameters should be escaped.
Public Sub Download(Link As String)
    req.InitializeGet(Link)
    CallSubDelayed2(HttpUtils2Service, "SubmitJob", Me)
End Sub
'Submits a HTTP GET request.
'Encodes illegal parameter characters.
'<code>Example:
'job.Download2("http://www.example.com", _
'    Array As String("key1", "value1", "key2", "value2"))</code>
Public Sub Download2(Link As String, Parameters() As String)
    Dim sb As StringBuilder
    sb.Initialize
    sb.Append(Link)
    If Parameters.Length > 0 Then sb.Append("?")
    Dim su As StringUtils
    For i = 0 To Parameters.Length - 1 Step 2
        If i > 0 Then sb.Append("&")
        sb.Append(su.EncodeUrl(Parameters(i), "UTF8")).Append("=")
        sb.Append(su.EncodeUrl(Parameters(i + 1), "UTF8"))
    Next
    req.InitializeGet(sb.ToString)
    CallSubDelayed2(HttpUtils2Service, "SubmitJob", Me)       
End Sub

'Called by the service to get the request
Public Sub GetRequest As HttpRequest
    Return req
End Sub

'Called by the service when job completes
Public Sub Complete (id As Int)
    taskId = id
    CallSubDelayed2(target, "JobDone", Me)
End Sub

'Should be called to free resources held by this job.
Public Sub Release
    File.Delete(HttpUtils2Service.TempFolder, taskId)
End Sub

'Returns the response as a string encoded with UTF8.
Public Sub GetString As String
    Return GetString2("UTF8")
End Sub

'Returns the response as a string.
Public Sub GetString2(Encoding As String) As String
    Dim tr As TextReader
    tr.Initialize2(File.OpenInput(HttpUtils2Service.TempFolder, taskId), Encoding)
    Dim res As String
    res = tr.ReadAll
    tr.Close
    Return res
End Sub


Public Sub GetInputStream As InputStream
    Dim In As InputStream
    In = File.OpenInput(HttpUtils2Service.TempFolder, taskId)
    Return In
End Sub

Line 84 is: Return req

B4X:
Public Sub GetRequest As HttpRequest
    Return req
End Sub

Anyone know what I might of done wrong ?

When using it as a UI app it works, but since I am now using it as a nonUI app I can't work out why it's not.
 

aaronk

Well-Known Member
Licensed User
Longtime User
Why aren't you using OkHttpUtils2_NonUI library?
Got it to work now with OkHttpUtils2_NonUI. I had used HttpJob since I had something else already using HttpJob and didn't want to touch it since it was working already.

Moved to the questions forum.
Whops, I thought I posted it in the questions forum, but I guess not.
 
Upvote 0
Top