Android Question insert into mysql remote whith Okhttp

Kahapdos

Member
Licensed User
Longtime User
Good morning, I need your help, please.
I am trying to make an insert to a remote mysql database but I have difficulties.
Previously if I could do it with the http library but lately I mark apache error etc ...

Check the forum and comment that you can with the library okhttp that is almost the same but with the OK before example OkHttpClient before used the HttpClient but I have not worked.

Could you help me with the structure to make the remote mysql insert with the new libraries please? I only find examples to download files.
 
If it worked with http lib in the past then it should work with okhttp too.

I personally dont believe that the http lib is able to connect to a mysql db and insert things there. So i guess you are missing giving us enough informations.

What code did you used in the past???? Please post the relevant code, the jobdone sub and the error you got.
 
Last edited:
Upvote 0
https://www.b4x.com/android/forum/threads/okhttp-replaces-the-http-library.54723/

sample code

Code:
Dim job1 As HttpJob
    job1.Initialize("Insert", Me)
    job1.Download2("https://sample.net/insertdata.php", _
    Array As String("user_id",username))

Sub JobDone (Job As HttpJob)

    ProgressDialogHide
    Log("JobName = " & Job.JobName & ", Success = " & Job.Success)
    If Job.Success = True Then

Select Job.JobName
 
    Case "Insert"
        Dim str As String

       str=Job.Getstring     

       msgbox(str,"title")



     End Select
End if
Job.Release

End Sub

PHP:
<?php
$db_host        = '*****';
$db_user        = '***';
$db_pass        = '***';
$db_database    = '****';


$db = new PDO('mysql:host='.$db_host.';dbname='.$db_database, $db_user, $db_pass,array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8") );
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING);

$name = $_GET["user_id"];


$sql = "INSERT INTO user (name) VALUES (:name)";

$q = $db->prepare($sql);

$q->execute(array(':name'=>$name));


echo "data inserted!";


?>
 
Last edited:
Upvote 0
In the past I used this code and it worked for me, But with okhttp I could not

Code:
Sub Process_Globals
Dim hc As HttpClient
End Sub

Sub Globals
 
End Sub

Sub Activity_Create(FirstTime As Boolean)
If FirstTime Then
hc.Initialize("hc")
End If
Activity.LoadLayout("formulario") 
End Sub

Sub Sub Button4_Click
    Dim Query1  As String
    Dim req As HttpRequest
Query1="INSERT INTO Brain (Nombre,Contraseña) VALUES ('" & edittext1.text & "' ,'" & edittext2.text & "')"
req.InitializePost2("http://kahapdos.com/mapas.php",Query1.GetBytes("UTF8"))
hc.Execute(req, 1)
end sub


Sub hc_ResponseSuccess (Response As HttpResponse, tarea As Int)
End Sub

Sub hc_ResponseError (Response As HttpResponse, Reason As String, StatusCode As Int, tarea As Int)
End Sub

and php file

Code:
<?

$databasehost = "localhost";
$databasename = "****";
$databaseusername ="****";
$password="*****";

$con = mysql_connect($databasehost,$databaseusername,$databasepassword) or die(mysql_error());
mysql_select_db($databasename) or die(mysql_error());
mysql_query("SET CHARACTER SET utf8");
$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);
}
?>
 
Last edited:
Upvote 0
If it worked with http lib in the past then it should work with okhttp too.

I personally dont believe that the http lib is able to connect to a mysql db and insert things there. So i guess you are missing giving us enough informations.

What code did you used in the past???? Please post the relevant code, the jobdone sub and the error you got.
https://www.b4x.com/android/forum/threads/okhttp-replaces-the-http-library.54723/

sample code

Code:
Dim job1 As HttpJob
    job1.Initialize("Insert", Me)
    job1.Download2("https://sample.net/insertdata.php", _
    Array As String("user_id",username))

Sub JobDone (Job As HttpJob)

    ProgressDialogHide
    Log("JobName = " & Job.JobName & ", Success = " & Job.Success)
    If Job.Success = True Then

Select Job.JobName

    Case "Insert"
        Dim str As String

       str=Job.Getstring    

       msgbox(str,"title")



     End Select
End if
Job.Release

End Sub

PHP:
<?php
$db_host        = '*****';
$db_user        = '***';
$db_pass        = '***';
$db_database    = '****';


$db = new PDO('mysql:host='.$db_host.';dbname='.$db_database, $db_user, $db_pass,array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8") );
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING);

$name = $_GET["user_id"];


$sql = "INSERT INTO user (name) VALUES (:name)";

$q = $db->prepare($sql);

$q->execute(array(':name'=>$name));


echo "data inserted!";


?>
Thanks ronell a question, I would have to generate different php files according to what I would like to do (insert, update, delete etc ....)?
 
Upvote 0
Thanks ronell a question, I would have to generate different php files according to what I would like to do (insert, update, delete etc ....)?

put jobs in a button,
Code:
Sub Insertbtn_Click

Dim job1 As HttpJob
    job1.Initialize("Insert", Me)
    job1.Download2("https://sample.net/insertdata.php", _
    Array As String("user_id",username))

End Sub

Sub Updatebtn_Click

Dim job1 As HttpJob
    job1.Initialize("update", Me)
    job1.Download2("https://sample.net/updatedata.php", _
    Array As String("user_id",username))

End Sub

Sub Deletebtn_Click

Dim job1 As HttpJob
    job1.Initialize("delete", Me)
    job1.Download2("https://sample.net/deletedata.php", _
    Array As String("user_id",username))

End Sub








Sub JobDone (Job AsHttpJob)
ProgressDialogHide
Log("JobName = " & Job.JobName & ", Success = " & Job.Success)
If Job.Success = True
Then
       Select Job.JobName
                Case "Insert"
                   Dim str AsString

                   str=Job.Getstring
                   msgbox(str,"title")

                 Case"update"



                  Case "delete"



End Select

End if
Job.Release

End Sub
 
Upvote 0
If you have B4A ver7 with resumable subs coupled with OKhttpUtils2 ... the JobDone event can be handle within the calling downloas Sub ..

Code:
Sub Insertbtn_Click
    Dim job1 AsHttpJob
    job1.Initialize("", Me)
    job1.Download2("https://sample.net/insertdata.php", Array As String("user_id",username))

   Wait For (job1) JobDone(job1 As HttpJob)
   If job1.Success Then
     Log(job1Getstring.)
   End if
   job1.Release

End Sub

With this , there is no need for a seperate JobDone event handling numerous requests
 
Upvote 0
Back
Top