using php in android (SOS)

alim2012

Member
Licensed User
Longtime User
hi all
i have website and some user.
i want login user in android appliction and send username and password with post php metod
and recive json file of website in android appliction
please help me
thanks a lot
:BangHead: :BangHead: :BangHead: :BangHead:
 

susu

Well-Known Member
Licensed User
Longtime User
You just use Httputils2 to send post data and read json with Json lib.
 
Upvote 0

warwound

Expert
Licensed User
Longtime User
As susu posted you can login to the website using HttpUtils - passing the username and password as POST data.

Once logged in you need to extract the logged in session cookie from the HttpResponse, this cookie will then need to be added to all future HttpRequests so that the web site recognises your application is logged in.
Without that cookie all requests will be refused by your web site - your application will not be recognised as logged in.

There's various threads on the forum dealing with logging in to a web site and handling the session cookie.

Martin.
 
Upvote 0

alim2012

Member
Licensed User
Longtime User
hi
thanks fo your answer
i have Httputils2
are you can send example or more explanation for this
please please please please please please please:BangHead::BangHead::BangHead:
thanks a lot
 
Upvote 0

warwound

Expert
Licensed User
Longtime User
I don't have a complete working example that uses HttpUtils2 and also gets the cookie.

So first have a look at this Service module code:

B4X:
Sub Process_Globals
   Dim AuthCookie As String=""
   Dim HttpClient1 As HttpClient
   Dim TaskId As Int=0
End Sub
Sub Service_Create
   HttpClient1.Initialize("HttpClient1")
   
End Sub

Sub Service_Start (StartingIntent As Intent)
   If AuthCookie="" Then
      Dim HttpRequest1 As HttpRequest
      Dim LoginUrl As String="set your web site url here"
      Dim PostData As String="username=???&password=???"
      
      HttpRequest1.InitializePost2(LoginUrl, PostData.GetBytes("UTF8"))
      HttpClient1.Execute(HttpRequest1, TaskId)
      TaskId=TaskId+1
   End If
End Sub

Sub Service_Destroy

End Sub

Sub HttpClient1_ResponseError (Response As HttpResponse, Reason As String, StatusCode As Int, TaskId1 As Int)
   Log("HttpClient1_ResponseError")
   Log("TaskId: "&TaskId1&", Reason: "&Reason&", Code: "&StatusCode)
   
    If Response<>Null Then
        Log(Response.GetString("UTF8"))
        Response.Release
    End If
End Sub

Sub HttpClient1_ResponseSuccess (Response As HttpResponse, TaskId1 As Int)
   Log("HttpClient1_ResponseSuccess")
   Log("TaskId: "&TaskId1)
   Log("** Headers **")
   Dim Headers As Map=Response.GetHeaders
   Dim i As Int
   Dim Key, Value As String
   For i=0 To Headers.Size-1
      '   one of these key/value pairs should be the logged in session cookie
      Key=Headers.GetKeyAt(i)
      Value=Headers.GetValueAt(i)
      Log(Key&" : "&Value)
   Next
   
'   ** watch out - if these next lines are uncommented a NetworkOnMainThread exception will occur **
'   set SDK target to API 8 as temporary workaround
   
'   Log("** Response **")
'    If Response<>Null Then
'        Log(Response.GetString("UTF8"))
'        Response.Release
'    End If
End Sub

The code in the Sub HttpClient1_ResponseSuccess is what you want to look at, after a successful login the HttpResponse object should contain a header that is the logged in session cookie.
You need to extract this cookie once successfully logged in and add it to future HttpRequest objects.

Now this example code is far from perfect, it was just written to test which headers are set once logged in.
You ought to get the HttpUtils2 code module and use HttpUtils2 - just look at my example code as describing the steps you want to take using HttpUtils2:
  • Login to the web site by passing the username and password as POST data.
  • Find and extract the logged in session cookie in the HttpResponse.
  • Pass the session cookie with future requests.

One thing to watch out for, if you're trying to log in to a web site over HTTPS (not HTTP) then the session cookie cannot be found using the above methods.
Have a read of my thread here: http://www.b4x.com/forum/libraries-...5383-extend-httpclientwrapper.html#post147081.

Martin.
 

Attachments

  • GetCookieExample.zip
    6.7 KB · Views: 331
Upvote 0

alim2012

Member
Licensed User
Longtime User
hi
thanks a lot for your answer
this is a best answer for my qustion but show me lenght of json
i can not give json text
please please please please please please please help again
tahanks a lot agian
ali
 
Upvote 0

warwound

Expert
Licensed User
Longtime User
Hi.

I don't understand what you mean by the json text length.

Have you managed to succssfully log in tothe website and now have to parse a json response?

Martin.
 
Upvote 0

alim2012

Member
Licensed User
Longtime User
hi
thanks a lot for your flowing my problem
android application show me number of length of web site json

i can not give json and save in the phone

do i give joson of subject httpclinet of your example?
thanks a lot
thanks a lot
thanks a lot
thanks a lot
thanks a lot
thanks a lot
thanks a lot
thanks a lot
thanks a lot
thanks a lot
thanks a lot
thanks a lot
Ali
 
Upvote 0
Top