Hi,
I succesfully built an example describing how to connect to mysql server with php. After that I done my example app but it throws an http exception, in more details: org.apache.http.ProtocolException: The server failed to respond with a valid HTTP response.
Here is b4android code:
Here is php code:
Please help.
I succesfully built an example describing how to connect to mysql server with php. After that I done my example app but it throws an http exception, in more details: org.apache.http.ProtocolException: The server failed to respond with a valid HTTP response.
Here is b4android code:
B4X:
'Activity module
Sub Process_Globals
'These global variables will be declared once when the application starts.
'These variables can be accessed from all modules.
Dim hc As HttpClient
End Sub
Sub Globals
'These global variables will be redeclared each time the activity is created.
'These variables can only be accessed from this module.
Dim tabhoster As TabHost
Dim pnl1,pnl2,pnl3 As Panel
Dim clear As Button
Dim scan As Button
Dim tbl As table
Dim id As Label
Dim bcreader As ABZxing
End Sub
Sub Activity_Create(FirstTime As Boolean)
If FirstTime Then
hc.Initialize("hc")
End If
tabhoster.Initialize("tabhoster")
pnl1.Initialize("pnl1")
pnl2.Initialize("pnl2")
pnl3.Initialize("pnl3")
tbl.Initialize(pnl3,"tbl",2)
id.Initialize("id")
clear.Initialize("clear")
scan.Initialize("scan")
pnl1.AddView(clear,20%x,30%y,60%x,15%y)
pnl2.AddView(scan,20%x,30%y,60%x,15%y)
pnl2.AddView(id,scan.Left,scan.Top+scan.Height+5%y,10%x,10%y)
tbl.AddToPanel(pnl3,0,0,100%x,100%y)
tabhoster.AddTab2("Service",pnl1)
tabhoster.AddTab2("Scan",pnl2)
tabhoster.AddTab2("Table",pnl3)
Activity.AddView(tabhoster,0,0,100%x,100%y)
tbl.SetHeader(Array As String("ID","Barcode"))
clear.Text="Clear"
scan.Text="Scan"
End Sub
Sub Activity_Resume
End Sub
Sub Activity_Pause (UserClosed As Boolean)
End Sub
Sub scan_click
bcreader.ABGetBarcode("bcreader","")
End Sub
Sub BCREADER_BarcodeFound (barCode As String, formatName As String)
ExecuteRemoteQuery("INSERT INTO barcodes (id, barcode) VALUES (NULL, '"&barCode&"')",1)
End Sub
Sub ExecuteRemoteQuery(Query As String, TaskId As Int)
Dim req As HttpRequest
req.InitializePost2("http://192.168.20.120/b4atest/barcode1/b4aconnector.php", Query.GetBytes("UTF8"))
'req.InitializeGet("http://192.168.20.120/b4atest/barcode1/b4aconnector.php?"&Query)
hc.Execute(req, TaskId)
End Sub
Sub tabhoster_TabChanged
ExecuteRemoteQuery("SELECT * FROM barcodes",2)
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 2
Dim barcodes As List
barcodes = parser.NextArray 'returns a list with maps
If barcodes.Size=0 Then
Msgbox("No values","Table is empty")
Return
End If
For i = 0 To barcodes.Size - 1
Dim m As Map
m = barcodes.Get(i)
tbl.AddRow(Array As String(m.Get("id"),m.Get("barcode")))
Next
End Select
Response.Release
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
End Sub
Here is php code:
PHP:
<?
$databasehost = "localhost";
$databasename = "testdb";
$databaseusername ="user";
$databasepassword = "xxxx";
$con = mysql_connect($databasehost,$databaseusername,$databasepassword) or die(mysql_error());
mysql_select_db($databasename) or die(mysql_error());
$query = file_get_contents("php://input");
$sth = mysql_query($query);
if (mysql_errno()) {
header("HTTP/1.1 500 Internal Server Error");
echo $query.'\n';
echo mysql_error();
}
else
{
$rows = array();
while($r = mysql_fetch_assoc($sth)) {
$rows[] = $r;
}
print json_encode($rows);
}
?>
Please help.