I made a Family Tree app which updates the DB at several users using PARSE.
It works nice and I made an equivalent app with B4j but the PARSE lib is not compatible.
Agraham showed me their REST API https://parse.com/docs/rest#users-login
It looks like something that can be done using HTTP and HTTPUTILS but I have no experience. The examples are in PYTHON or CURL.
For example : login process
But got lost afterwards. I guess it should be job1.download2() with the username & password (which I have).
After login I need to get objects and send objects.
If someone could help – I’ll appreciate.
I will venture a guess, but here is what I think you need.
Use the HttpClient (atleast initially)
You need to set the headers:
B4X:
HttpC.SetHeader("X-Parse-Application-Id", you app id)
HttpC.SetHeader("X-Parse-REST-API-Key", you reset api key from parse)
HttpC.InitializeGet ("http://api.parse.com/1/login?username=" & UrlEncode(username) & "&password=" & UrlEncode(password))
If Successful Then
Log("connected")
HR.InitializeHead("api.parse.com/1/users")
HR.SetHeader("X-Parse-Application-Id", "xxxxxxx")
HR.SetHeader("X-Parse-REST-API-Key","xxxxx")
Dim username As String = su.EncodeUrl("xxxxxxx","UTF8")
Dim pass As String = su.EncodeUrl("xxxxx","UTF8")
HR.InitializeGet("http://api.parse.com/1/login?username=" & username & "&password=" & pass)
hc.Execute(HR,1)
End If
end sub
Sub hc_ResponseError (Reason As String, StatusCode As Int, TaskId As Int)
Log(Reason & " =error")
End Sub
Sub hc_ResponseSuccess (Response As HttpResponse, TaskId As Int)
Log(Response.GetString("UTF8") & " success")
End Sub
I get this:
Program started.
connected
An error occurred:
(Line: 40) End Sub
java.lang.Exception: Sub hc_responseerror signature does not match expected signature.
public static anywheresoftware.b4a.pc.RemoteObject b4j.example.main_subs_0._hc_responseerror(anywheresoftware.b4a.pc.RemoteObject,anywheresoftware.b4a.pc.RemoteObject,anywheresoftware.b4a.pc.RemoteObject) throws java.lang.Exception
didnt realize this was a b4j question.
Nevertheless try this sub signature:
Sub hc_ResponseError (Response As HttpResponse, Reason As String, StatusCode As Int, TaskId As Int)
Sub AppStart (Form1 As Form, Args() As String)
MainForm = Form1
'MainForm.RootPane.LoadLayout("1") 'Load the layout file.
MainForm.Show
Dim j As HttpJob
j.Initialize("log-in", Me)
Dim user As Map
user.Initialize
user.Put("username", "user1")
user.Put("password", "123456")
Dim gen As JSONGenerator
gen.Initialize(user)
j.PostString("https://api.parse.com/1/users", gen.ToString)
j.GetRequest.SetHeader("X-Parse-REST-API-Key", "xxx")
j.GetRequest.SetHeader("X-Parse-Application-Id", "yyy")
j.GetRequest.SetContentType("application/json")
End Sub
Sub JobDone(job As HttpJob)
Log(job)
If job.Success Then
Log(job.GetString)
End If
job.Release
End Sub
Wow that works - I got connected and logged in.
Please show me how to send and recieve objects (lines of the DB in parse), like this in B4A:
B4X:
....Dim CheckOne As ParseQuery
CheckOne.Initialize("Family")
Dim k As Int
k = strg(0)
CheckOne.WhereEqualTo("No",k)
CheckOne.Find("CheckOne", k) ....
Sub CheckOne_DoneFind (Success As Boolean, ListOfPO As List, TaskId As Int)
'Log("CheckOne_DoneFind ")
If Success = False Then
Log("Error: " & LastException.Message)
Else
Dim po As ParseObject
If ListOfPO.Size > 0 Then
po = ListOfPO.Get(0)
Else
parse_add_item(strg(0))
End If
End If
End Sub
Erel, I see what happened. you translated the sign-in process, once it was done -no need to do it again.
now I need the login process which is written in the first post.
I tried by changing to https://api.parse.com/1/login but get a 404 answer (when I go to this address in browser I get a form for input my username and password).
with download2, array contains the username and password, I get response of "unauthorized".
This is probably because the request does not include the
"X-Parse-REST-API-Key" and "X-Parse-Application-Id"
How do I add them to the request ?
Dim j As HttpJob
j.Initialize("log-in", Me)
j.GetRequest.SetHeader("X-Parse-REST-API-Key", "opBMoH2Qqguvx3eDhcwEAZ0Jdl4nuLUP5BgRZWZb")
j.GetRequest.SetHeader("X-Parse-Application-Id", "vWcbP5PvajXyGcF123E0Z9F4tSCIwvS9AtIZmwCz")
j.Download2("https://api.parse.com/1/login",Array As String( "rderez@013net.net","kimel111"))
Dim j As HttpJob
j.Initialize("log-in", Me)
j.Download2("https://api.parse.com/1/login",Array As String( "rderez@013net.net","kimel111"))
j.GetRequest.InitializeHead("https://api.parse.com/1/login")
j.GetRequest.SetHeader("X-Parse-REST-API-Key", "opBMoH2Qqguvx3eDhcwEAZ0Jdl4nuLUP5BgRZWZb")
j.GetRequest.SetHeader("X-Parse-Application-Id", "vWcbP5PvajXyGcF123E0Z9F4tSCIwvS9AtIZmwCz")
' j.GetRequest.SetContentType("application/json")
Program started.
java.lang.IllegalArgumentException: HTTP entity may not be null
at org.apache.http.util.EntityUtils.toByteArray(EntityUtils.java:68)
at anywheresoftware.b4a.http.HttpClientWrapper$ExecuteHelper.run(HttpClientWrapper.java:279)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:471)
at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:334)
at java.util.concurrent.FutureTask.run(FutureTask.java:166)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
at java.lang.Thread.run(Thread.java:722)
[jobname=log-in, success=false, username=
, password=, errormessage=Bad Request, target=class b4j.example.main
, taskid=1, req=anywheresoftware.b4a.http.HttpClientWrapper$HttpUriRequestWrapper@9e5e7e, tag=java.lang.Object@14dc149
, fx=anywheresoftware.b4j.objects.JFX@1ccc11d, httputils2service=null]
So I went to the url in the browser, put the user and password there and it also does not authorize me and wants to login again !
If I go to www.parse.com and there press login I get another screen and there the username and password are accepted and I log in.
Tried again now with this (users instead of login !):
B4X:
Dim j As HttpJob
j.Initialize("log-in", Me)
j.Download2("https://api.parse.com/1/users",Array As String( "rderez@013net.net","kimel111"))
j.GetRequest.SetHeader("X-Parse-REST-API-Key", "opBMoH2Qqguvx3eDhcwEAZ0Jdl4nuLUP5BgRZWZb")
j.GetRequest.SetHeader("X-Parse-Application-Id", "vWcbP5PvajXyGcF123E0Z9F4tSCIwvS9AtIZmwCz")