Android Question Restful API service (2)

khwarizmi

Active Member
Licensed User
Longtime User
Hi all
I hope you all are well, in good health and in safety

Subsequent to this post. The example given by @aeric here was related to post job. How can I treat with the other Restful Api Services (get, put, and delete) ??
 

khwarizmi

Active Member
Licensed User
Longtime User
How can I send a put request? where is the error in this code:

B4X:
Dim Map1 As Map
    Map1.Initialize
    Map1.Put("st_name", "John")
    Map1.Put("address", "34 tutti st")
    Map1.Put("phone", "09765")
    Map1.Put("location", "16.765445,32.655578")
    Dim gen As JSONGenerator
    gen.Initialize(Map1)
    Dim jsn As String
    jsn = gen.ToString
    Dim j As HttpJob
    j.Initialize("", Me)
    j.PutString(ur & "/students/6", jsn)
    Wait For (j) JobDone(j As HttpJob)
    If j.Success Then
        Log(j.GetString)
    End If
    j.Release
 
Upvote 0

khwarizmi

Active Member
Licensed User
Longtime User
thank you @oparra , in fact I'm using Nodjs not PHP. the code for the update in the example you mentioned is:

B4X:
Dim Job6 As HttpJob
    Job6.Initialize("Change", Me)
    Job6.Download2( Main.strURL & "change-password.php", _
    Array As String("Email", strEmail, _
    "Password1", strPassword1, _
    "Password2", strPassword2))

but doesn't work. I want to modify the PutString formula in my code:
B4X:
Dim Map1 As Map
Map1.Initialize
Map1.Put("st_name", "John")
Map1.Put("address", "34 tutti st")
Map1.Put("phone", "09765")
Map1.Put("location", "16.765445,32.655578")
Dim gen As JSONGenerator
gen.Initialize(Map1)
Dim jsn As String
jsn = gen.ToString
Dim j As HttpJob
j.Initialize("", Me)
j.PutString(ur & "/students/6", jsn)
Wait For (j) JobDone(j As HttpJob)
If j.Success Then
Log(j.GetString)
End If
j.Release
 
Upvote 0

khwarizmi

Active Member
Licensed User
Longtime User
yes .. json string.
this code in the model folder:

B4X:
custloc.updateById = (id, cust, result) => {
    
  sql.query(
    "UPDATE fines SET st_name = ? , address = ? , phone = ? , location = ? WHERE id = ?",
    [cust.st_name,cust.address,cust.phone,cust.location,id],
    (err, res) => {
      if (err) {
        console.log("error: ", err);
        result(null, err);
        return;
      }

      if (res.affectedRows == 0) {
        // not found custloc with the id
        result({ kind: "not_found" }, null);
        return;
      }

      console.log("updated cust: ", { id: id, ...cust });
      result(null, { id: id, ...cust });
    }
  );
};

and this one in the controllers folder:
B4X:
exports.update = (req, res) => {
  // Validate Request
  if (!req.body) {
    res.status(400).send({
      message: "Content can not be empty!"
    });
  }

  console.log(req.body);

  custloc.updateById(
    req.params.id,
    new custloc(req.body),
    (err, data) => {
      if (err) {
        if (err.kind === "not_found") {
          res.status(404).send({
            message: `Not found custloc with id ${req.params.id}.`
          });
        } else {
          res.status(500).send({
            message: "Error updating custloc with id " + req.params.id
          });
        }
      } else res.send(data);
    }
  );
};

in the routes folder :

B4X:
app.put("/students/:id", loc.update);
 
Last edited:
Upvote 0

khwarizmi

Active Member
Licensed User
Longtime User
thank you @oparra , in fact I'm using Nodjs not PHP. the code for the update in the example you mentioned is:

B4X:
Dim Job6 As HttpJob
    Job6.Initialize("Change", Me)
    Job6.Download2( Main.strURL & "change-password.php", _
    Array As String("Email", strEmail, _
    "Password1", strPassword1, _
    "Password2", strPassword2))

but doesn't work. I want to modify the PutString formula in my code:

How to use the PutString to pass a JSON array?
 
Upvote 0
Top