Help with connecting to a database

isk2837

Member
Licensed User
Longtime User
I'm trying to connect to a database, but keep getting the error message Error: java.lang.IllegalArgumentException: Host name may not be null, StatusCode: -1, which I suspect has something to do with the data from the database not appearing in my ListView item. The code in my php page has been tested and the page itself seems to work as far as connecting to the MySQL database and accessing the data in the table, but when I try to bring that data into the app...nothing. So, what does that error message mean could be wrong, and how do I fix it (please bear in mind that I'm still very new to this program and try to keep it simple)
 

JonPM

Well-Known Member
Licensed User
Longtime User
Try posting your code here, then we can help you out more

Sent from my DROIDX
 
Upvote 0

isk2837

Member
Licensed User
Longtime User
Try posting your code here, then we can help you out more

Sent from my DROIDX

Sub Process_Globals
Dim hc As HttpClient
Dim PLAYER_LIST As Int
PLAYER_LIST = 1
End Sub

Sub Globals
Type TwoLines (First As String, Second As String)
Dim ListView1 As ListView
End Sub

Sub Activity_Create(FirstTime As Boolean)
If FirstTime Then
hc.Initialize("hc")
End If
Activity.LoadLayout("HighScores")
FetchCountriesList
End Sub
Sub FetchCountriesList
ProgressDialogShow("Fetching list of countries")
'Gets all the available countries
ExecuteRemoteQuery("SELECT PlayerName, HighScore FROM HighScores ORDER BY HighScore", PLAYER_LIST)
End Sub
Sub ExecuteRemoteQuery(Query As String, TaskId As Int)
Dim req As HttpRequest
req.InitializePost2("http://localhost/HighScores.php", Query.GetBytes("UTF8"))
hc.Execute(req, TaskId)
End Sub
Sub hc_ResponseError (Response As HttpResponse, Reason As String, StatusCode As Int, TaskId As Int)
Log("Error: " & Reason & ", StatusCode: " & StatusCode)
If Response <> Null Then
Log(Response.GetString("UTF8"))
Response.Release
End If
ProgressDialogHide
End Sub
Sub hc_ResponseSuccess (Response As HttpResponse, TaskId As Int)
Dim res As String
res = Response.GetString("UTF8")
Log("Response from server: " & res)
Dim parser As JSONParser
parser.Initialize(res)
Select TaskId
Case PLAYER_LIST
Dim players As List
players = parser.NextArray 'returns a list with maps
For i = 0 To 4
Dim m As Map
m = players.Get(i)
'We are using a custom type named TwoLines (declared in Sub Globals).
'It allows us to later get the two values when the user presses on an item.
Dim tl As TwoLines
tl.First = m.Get("PlayerName")
tl.Second = m.Get("HighScore")
ListView1.AddTwoLines2(tl.First, tl.Second, tl)
Next
ProgressDialogHide
End Select
Response.Release
End Sub


Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub
 
Upvote 0

lagore

Active Member
Licensed User
Longtime User
Hi try putting your code between 'code' tags as it makes reading the code much easier.
 
Upvote 0

isk2837

Member
Licensed User
Longtime User
Hi try putting your code between 'code' tags as it makes reading the code much easier.
B4X:
Sub Process_Globals
 Dim hc As HttpClient
 Dim PLAYER_LIST As Int
 PLAYER_LIST = 1
 End Sub
 
Sub Globals
 Type TwoLines (First As String, Second As String)
 Dim ListView1 As ListView
 End Sub
 
Sub Activity_Create(FirstTime As Boolean)
 If FirstTime Then
 hc.Initialize("hc")
 End If
 Activity.LoadLayout("HighScores")
 FetchCountriesList
 End Sub
 Sub FetchCountriesList
 ProgressDialogShow("Fetching list of countries")
 'Gets all the available countries
 ExecuteRemoteQuery("SELECT PlayerName, HighScore FROM HighScores ORDER BY HighScore", PLAYER_LIST)
 End Sub
 Sub ExecuteRemoteQuery(Query As String, TaskId As Int)
 Dim req As HttpRequest
 req.InitializePost2("http://localhost/HighScores.php", Query.GetBytes("UTF8"))
 hc.Execute(req, TaskId)
 End Sub
 Sub hc_ResponseError (Response As HttpResponse, Reason As String, StatusCode As Int, TaskId As Int)
 Log("Error: " & Reason & ", StatusCode: " & StatusCode)
 If Response <> Null Then
 Log(Response.GetString("UTF8"))
 Response.Release
 End If
 ProgressDialogHide
 End Sub
 Sub hc_ResponseSuccess (Response As HttpResponse, TaskId As Int)
 Dim res As String
 res = Response.GetString("UTF8")
 Log("Response from server: " & res)
 Dim parser As JSONParser
 parser.Initialize(res)
 Select TaskId
 Case PLAYER_LIST
 Dim players As List
 players = parser.NextArray 'returns a list with maps
 For i = 0 To 4
 Dim m As Map
 m = players.Get(i)
 'We are using a custom type named TwoLines (declared in Sub Globals).
 'It allows us to later get the two values when the user presses on an item.
 Dim tl As TwoLines
 tl.First = m.Get("PlayerName")
 tl.Second = m.Get("HighScore")
 ListView1.AddTwoLines2(tl.First, tl.Second, tl)
 Next
 ProgressDialogHide
 End Select
 Response.Release
 End Sub
 

Sub Activity_Resume
 
End Sub
 
Sub Activity_Pause (UserClosed As Boolean)
 
End Sub
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
http://localhost points to the current device / emulator.
I guess that you want to connect to your desktop computer. You should use its IP instead.
You can find the IP by calling IPConfig from the command line.

Firewalls usually block incoming connections. Make sure that your firewall allows incoming connections on port 80.
 
Upvote 0

isk2837

Member
Licensed User
Longtime User
http://localhost points to the current device / emulator.
I guess that you want to connect to your desktop computer. You should use its IP instead.
You can find the IP by calling IPConfig from the command line.

Firewalls usually block incoming connections. Make sure that your firewall allows incoming connections on port 80.

Changed the line with the URL from
B4X:
req.InitializePost2("http://localhost/HighScores.php", Query.GetBytes("UTF8"))
to
B4X:
req.InitializePost2("http://xxx.xxx.x.xxx/HighScores.php", Query.GetBytes("UTF8"))

I put xxx.xxx.x.xxx here instead of what I actually put - the numbers under IPv4 Address because I'm not sure if anyone could do anything with my actual IP address. But the point is that I got the message -

Forbidden
You don't have permission to access /HighScores.php on this server.

when I tried running the page http://xxx.xxx.x.xxx/HighScores.php in my browser. So how do i give permission to access this server? I know you said something about allowing incoming connections on port 80, but I don't know how to do that.
 
Upvote 0

isk2837

Member
Licensed User
Longtime User
It should be done in your server configuration.

This might be a stupid question, but where in Windows 7 is the server configuration? (I'm still very new both to using Windows 7 and doing this kind of computer work with ports and stuff.)
 
Upvote 0

isk2837

Member
Licensed User
Longtime User
This is a valid question. The answer is simple. Currently you don't have any HTTP (web) server running.

You should first install a web server. Google for "wamp".

I already have Uniserver on my computer. I still don't really understand why I can't use "http://localhost" to point Basic4Android to the php file - for me, "http://localhost" points to the Uniserver on my computer, and from there it's easy to point to and run the Highscores.php file. If i type in "http://localhost.HighScores.php" into my browser while Uniserver is running it works fine. But if I try to connect to that file with Basic4Android using the code I originally showed you at the start of this discussion, I now get Error: org.apache.http.conn.HttpHostConnectException: Connection to http://localhost refused, StatusCode: -1. I can still connect to http://localhost/HighScores.php through a web browser, but now Basic4Android can't connect to localhost at all. And I still don't have a clue how to get "http://xxx.xxx.x.xxx/HighScores.php to show anything other than "You do not have permission to access HighScores.php on this server" (xxx.xxx.x.xxx represent the numbers of my IPv4 address)
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
As I previously wrote, http://localhost points to the current device. I guess that you are using the emulator. In that case you are trying to call to a server hosted in the emulator (which doesn't exist).
See this link for more information about the emulator: Using the Android Emulator | Android Developers

There is an IP address which you can use to access the desktop localhost.
 
Upvote 0

isk2837

Member
Licensed User
Longtime User
As I previously wrote, http://localhost points to the current device. I guess that you are using the emulator. In that case you are trying to call to a server hosted in the emulator (which doesn't exist).
See this link for more information about the emulator: Using the Android Emulator | Android Developers

There is an IP address which you can use to access the desktop localhost.

Changed the line to
B4X:
req.InitializePost2("http://127.0.0.1/HighScores.php", Query.GetBytes("UTF8"))
Now I'm getting this message in the Log - Error: org.apache.http.conn.HttpHostConnectException: Connection to http://127.0.0.1 refused, StatusCode: -1

If I type in http://127.0.0.1/HighScores.php into the browser it runs the page, so why would it refuse the connection for Basic4android?
 
Upvote 0

isk2837

Member
Licensed User
Longtime User
Changed the line to
B4X:
req.InitializePost2("94.170.229.51/HighScores.php", Query.GetBytes("UTF8"))
That's my IP address according to various sources. Now I'm getting java.lang.IllegalArgumentException: Illegal character in path at index 13: 94.170.229.51\HighScores.php So what's the illegal character in that?

Also, I know you keep saying "localhost/HighScores.php" won't work, so where should I be putting the php file if localhost is out? Because that location is the place where all php files are. For me, localhost seems to be both where all php files are run and where the Uniserver database is.
 
Last edited:
Upvote 0

roarnold

Active Member
Licensed User
Longtime User
http://localhost points to the current device / emulator.
I guess that you want to connect to your desktop computer. You should use its IP instead.
You can find the IP by calling IPConfig from the command line.

Firewalls usually block incoming connections. Make sure that your firewall allows incoming connections on port 80.

Technically, you don't have to use port 80. I have a back end db and use a non-nondescript port that one would have to scan to find or at least know about. Then I have other things in place also. However, Erel is right, if HTTP the port 80 is the bet.

R
 
Upvote 0
Top