Android Question web request too long

MohammadNew

Active Member
Licensed User
Longtime User
Hello everyone,

I have problem with insert data

error web request too long > because I insert base64string

B4X:
Dim InsertNewPerson As HttpJob
    InsertNewPerson.Initialize("InsertNewP", Me)
    InsertNewPerson.download2("http://" & ServerIP & "/persons.php", Array As String ("action", "InsertNewPerson", "name", NameET.Text, "age", AgeET.Text, "img",img64))

but without base64string it is done.
 

DonManfred

Expert
Licensed User
Longtime User
Use PostMultipart instead of download(2)
The point is to make a POST Request, not a GET Request.
 
Upvote 0

sorex

Expert
Licensed User
Longtime User
I guess

B4X:
InsertNewPerson.postString("http://" & ServerIP & "/persons.php","action="& InsertNewPerson &"&name="& NameET.Text &"&age="& AgeET.Text &"&img="& img64))

will do
 
Upvote 0

sorex

Expert
Licensed User
Longtime User
oh, action is a string value and an http job. didn't see that.

try this

B4X:
InsertNewPerson.postString("http://" & ServerIP & "/persons.php","action=InsertNewPerson&name="& NameET.Text &"&age="& AgeET.Text &"&img="& img64)
 
Upvote 0

MohammadNew

Active Member
Licensed User
Longtime User
Nothing happened , this is logs

B4X:
Logger connected to:  samsung SM-A700FD
--------- beginning of system
--------- beginning of main
** Activity (main) Create, isFirst = true **
** Activity (main) Resume **
** Activity (main) Resume **
** Activity (main) Pause, UserClosed = false **
sending message to waiting queue (OnActivityResult)
running waiting messages (1)
24689
** Activity (main) Resume **
Back from Job:GetP
Response from server: []

** Service (httputils2service) Create **
** Service (httputils2service) Start **
** Service (httputils2service) Start **
Back from Job:InsertNewP
Response from server:

Back from Job:GetP
Response from server: []

** Activity (main) Pause, UserClosed = true **
** Activity (main) Create, isFirst = true **
** Activity (main) Resume **
** Activity (main) Pause, UserClosed = false **
sending message to waiting queue (OnActivityResult)
running waiting messages (1)
24689
** Activity (main) Resume **
** Service (httputils2service) Create **
** Service (httputils2service) Start **
** Service (httputils2service) Start **
Back from Job:GetP
Response from server: []

Back from Job:InsertNewP
Response from server:

** Activity (main) Pause, UserClosed = false **

Thanks again
 
Upvote 0

MohammadNew

Active Member
Licensed User
Longtime User
look please , this is my php file

B4X:
<?php

$host = "my server";
$user = "my user";
$pw = "my pass";
$db = "my db";

$con = mysql_connect($host,$user,$pw) or die(mysql_error());
mysql_select_db($db) or die(mysql_error());
mysql_query("SET CHARACTER SET utf8");
mysql_query("SET NAMES 'utf8'");

$action = $_GET["action"];
switch ($action)

{
    case "CountPersons":
        $q = mysql_query("SELECT * FROM persons");
        $count = mysql_num_rows($q);
        print json_encode($count);
    break;
   
    Case "GetPersons":
        $q = mysql_query("SELECT name, age FROM persons");
        $rows = array();
        while($r = mysql_fetch_assoc($q))
        {
            $rows[] = $r;
        }
        print json_encode($rows);
    break;
   
    case "InsertNewPerson":
        $name = $_GET["name"];
        $age = $_GET["age"];
        $img= $_GET["img"];
        $q = mysql_query("INSERT INTO persons (name, age, img) VALUES ('$name', $age, $img)");
        print json_encode("Inserted");
    break;
   
        case "UpdatePerson":
        $name = $_GET["name"];
        $age = $_GET["age"];
        $img= $_GET["img"];
        $q = mysql_query("UPDATE persons set name = '$name', age= $age, img= $img where name='$name'");
        print json_encode("Updated");
    break;

        case "DeletePerson":
                $name = $_GET["name"];       
        $q = mysql_query("DELETE from persons where name='$name'");
        print json_encode("Deleted");
    break;



}



?>
 
Upvote 0

sorex

Expert
Licensed User
Longtime User
you didn't change it to the new situation so it's normal that it doesn't work.

do a search'n replace (ctrl-h) of $_GET to $_POST
 
Upvote 0

MohammadNew

Active Member
Licensed User
Longtime User
like this MR. Sorex
but what about my code in b4a

B4X:
<?php

$host = "";
$user = "";
$pw = "";
$db = "";

$con = mysql_connect($host,$user,$pw) or die(mysql_error());
mysql_select_db($db) or die(mysql_error());
mysql_query("SET CHARACTER SET utf8");
mysql_query("SET NAMES 'utf8'");

$action = $_POST["action"];
switch ($action)

{
    case "CountPersons":
        $q = mysql_query("SELECT * FROM persons");
        $count = mysql_num_rows($q);
        print json_encode($count);
    break;
   
    Case "GetPersons":
        $q = mysql_query("SELECT name, age FROM persons");
        $rows = array();
        while($r = mysql_fetch_assoc($q))
        {
            $rows[] = $r;
        }
        print json_encode($rows);
    break;
   
    case "InsertNewPerson":
        $name = $_POST["name"];
        $age = $_POST["age"];
                $img= $_POST["img"];
        $q = mysql_query("INSERT INTO persons (name, age, img) VALUES ('$name', $age, $img)");
        print json_encode("Inserted");
    break;
   
        case "UpdatePerson":
        $name = $_POST["name"];
        $age = $_POST["age"];
                $img= $_POST["img"];
        $q = mysql_query("UPDATE persons set name = '$name', age= $age, img= $img where name='$name'");
        print json_encode("Updated");
    break;

        case "DeletePerson":
                $name = $_POST["name"];       
        $q = mysql_query("DELETE from persons where name='$name'");
        print json_encode("Deleted");
    break;

}

?>
 
Upvote 0

MohammadNew

Active Member
Licensed User
Longtime User
this is my code b4a

B4X:
#Region  Project Attributes
    #ApplicationLabel: Xampp php MySql
    #VersionCode: 1
    #VersionName:
    'SupportedOrientations possible values: unspecified, landscape or portrait.
    #SupportedOrientations: portrait
    #CanInstallToExternalStorage: False
#End Region

#Region  Activity Attributes
    #FullScreen: False
    #IncludeTitle: True
#End Region

Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.
   
        Dim Chooser As ContentChooser
                   
        Dim img64 As String
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 Base64Con As Base64Image
   
    Private PersonsListview As ListView
    Private CountPersonsButton As Button
    Private GetPersonsButton As Button
    Private NameET As EditText
    Private AgeET As EditText
    Private InsertNewPersonButton As Button
    Private Label1 As Label
    Private Label2 As Label
   
    Private ServerIP As String
   
   
    Private btnupdate As Button
    Private btndelete As Button
    Private ImageView1 As ImageView
End Sub

Sub Activity_Create(FirstTime As Boolean)
    'Do not forget to load the layout file created with the visual designer. For example:
   
   
    Activity.LoadLayout("1")
    Activity.Title ="Playing with php/MySql"
    
    ServerIP="mohammadnew-001-site1.dtempurl.com" ' The ip address where you Xampp installation runs
   
End Sub

Sub getdata
    PersonsListview.Clear
   
    Dim GetPersons As HttpJob
    GetPersons.Initialize("GetP", Me)
'    GetPersons.download2("http://" & ServerIP & "/persons.php", Array As String ("action", "GetPersons"))
   
    GetPersons.PostString("http://" & ServerIP & "/persons.php", ("action=GetPersons"))
   
    NameET.Text = ""
    AgeET.Text = ""
   
    NameET.RequestFocus
End Sub

Sub CountPersonsButton_Click
    Dim CountPersons As HttpJob
    CountPersons.Initialize("CountP", Me)
    CountPersons.download2("http://" & ServerIP & "/persons.php", Array As String ("action", "CountPersons"))
   
End Sub

Sub GetPersonsButton_Click
    getdata
End Sub

Sub InsertNewPersonButton_Click
   
    If NameET.Text = "" Then
       Msgbox("Name is missing or to short", "Name")
       Return
    End If
   
    If AgeET.Text = "" Then
       Msgbox("Age is missing", "Age")
       Return
    End If
           
    Dim InsertNewPerson As HttpJob
    InsertNewPerson.Initialize("InsertNewP", Me)
'    InsertNewPerson.download2("http://" & ServerIP & "/persons.php", Array As String ("action", "InsertNewPerson", "name", NameET.Text, "age", AgeET.Text, "img",img64))
   
    InsertNewPerson.postString("http://" & ServerIP & "/persons.php","action=InsertNewPerson&name="& NameET.Text &"&age="& AgeET.Text &"&img="& img64)
       
    getdata
End Sub


Sub JobDone(Job As HttpJob)
    ProgressDialogHide
    If Job.Success Then
        Dim res As String
        res = Job.GetString
        Log("Back from Job:" & Job.JobName )
        Log("Response from server: " & res)
               
        Dim parser As JSONParser
        parser.Initialize(res)
       
        Select Job.JobName
                       
            Case "GetP"
                Dim ListOfPersons As List
                Dim PersonName As String
                Dim PersonAge As Int
               
                ListOfPersons = parser.NextArray 'returns a list with maps
               
                PersonsListview.Clear
               
                If ListOfPersons.Size=0 Then
                    PersonsListview.AddSingleLine ("No persons found...")
                Else
                    For i = 0 To ListOfPersons.Size - 1
                        Dim Person As Map
                        Person = ListOfPersons.Get(i)
                                           
                        PersonName = Person.Get("name")
                        PersonAge = Person.Get("age")
                       
                        PersonsListview.AddSingleLine (PersonName & ", " & PersonAge)
                       
                    Next
                End If
       
            Case "CountP"
                PersonsListview.AddSingleLine ("Persons in table: " & parser.NextValue)
               
            Case "InsertNewP"
                'Do nothing
               
        End Select
       
       
       
    Else
        ToastMessageShow("Error: " & Job.ErrorMessage, True)
    End If
    Job.Release
End Sub
Sub Activity_Resume

End Sub
Sub Activity_Pause (UserClosed As Boolean)

End Sub

Sub ImageView1_Click
    If Chooser.IsInitialized = False Then
        Chooser.Initialize("chooser")
    End If
    Chooser.Show("image/*", "Choose image")
End Sub

Sub chooser_Result(Success As Boolean, Dir As String, FileName As String)   
    If Success Then
      
        img64 = Base64Con.EncodeFromImage(Dir,FileName)
              
       ImageView1.Bitmap=LoadBitmap(Dir,FileName)

    Else
        ToastMessageShow("No image selected", True)
    End If
End Sub

Sub CreateScaledBitmap(Original As Bitmap, Width As Int, Height As Int, Filter As Boolean) As Bitmap
    Dim jo As JavaObject
    jo.InitializeStatic("android.graphics.Bitmap")
    Return jo.RunMethod("createScaledBitmap", Array (Original, Width, Height, Filter))
End Sub
 
Upvote 0

sorex

Expert
Licensed User
Longtime User
the problem is on the server. check your database fieldnames and their size. if the size is too small it won't insert either.

or echo the insert sql string and copy it to phpmyadmin and it will tell you what the problem is if yo don't see it.

put

echo "INSERT INTO persons (name, age, img) VALUES ('$name', $age, $img)";

right after the sql execute and you'll get the query in your app in the log
 
Last edited:
Upvote 0
Top