Android Question Restful API service

khwarizmi

Active Member
Licensed User
Longtime User
Hi all

How to use http post request to implement Restful API service?
 

khwarizmi

Active Member
Licensed User
Longtime User
the terminator screen:
 

Attachments

  • 1-23-2020 1-17-54 PM.png
    1-23-2020 1-17-54 PM.png
    16.6 KB · Views: 182
Upvote 0

aeric

Expert
Licensed User
Longtime User
the terminator screen:

I think the valid SQL should be:
SQL:
INSERT INTO Customers (first_name, last_name) SELECT "Ali", "Adil"
or
SQL:
INSERT INTO Customers (first_name, last_name) VALUES ("Ali", "Adil")

I never see SQL written as
SQL:
INSERT INTO Customers SET first_name = NULL, last_name = NULL
Edit: Found this is valid for MySQL
 
Last edited:
Upvote 0

khwarizmi

Active Member
Licensed User
Longtime User
this is the problem, It is supposed to take the values of the fields from the JSON array and place them in the sql statment according to my nodejs code.
B4X:
const sql = require("./db.js");

// constructor
const custloc = function(cust) {
this.first_name  = cust.first_name;
this.last_name = cust.last_name;
this.country_code = cust.country_code;
this.mobile = cust.mobile;
this.email = cust.email;
this.created_user_id = cust.created_user_id;
this.created_time = cust.created_time;
this.updated_user_id = cust.updated_user_id;
this.updated_time = cust.updated_time;
this.is_active = cust.is_active;
this.pw = cust.pw;
};

custloc.create = (newcustloc, result) => {
  sql.query("INSERT INTO customers SET ?", newcustloc, (err, res) => {
    if (err) {
      console.log("error: ", err);
      result(err, null);
      return;
    }

    console.log("created cust: ", { id: res.insertId, ...newcustloc });
    result(null, { id: res.insertId, ...newcustloc });
  });
};

custloc.findById = (id, result) => {
  sql.query(`SELECT * FROM customers WHERE id = ${id}`, (err, res) => {
    if (err) {
      console.log("error: ", err);
      result(err, null);
      return;
    }

    if (res.length) {
      console.log("found cust: ", res[0]);
      result(null, res[0]);
      return;
    }

    // not found custloc with the id
    result({ kind: "not_found" }, null);
  });
};

custloc.getAll = result => {
  sql.query("SELECT * FROM customers", (err, res) => {
    if (err) {
      console.log("error: ", err);
      result(null, err);
      return;
    }

    console.log("custs: ", res);
    result(null, res);
  });
};

custloc.updateById = (id, cust, result) => {
    
  sql.query(
    "UPDATE customers SET first_name = ? , last_name = ? , country_code = ? , mobile = ? , email = ? , created_user_id = ? , created_time = ? , updated_user_id = ? , updated_time = ? , is_active = ? , pw = ? WHERE id = ?",
    [cust.first_name, cust.last_name, cust.country_code , cust.mobile , cust.email , cust.created_user_id, cust.created_time , cust.updated_user_id, cust.updated_time , cust.is_active , cust.pw ,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 });
    }
  );
};

custloc.remove = (id, result) => {
  sql.query("DELETE FROM customers WHERE id = ?", 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("deleted cust with id: ", id);
    result(null, res);
  });
};

custloc.removeAll = result => {
  sql.query("DELETE FROM customers", (err, res) => {
    if (err) {
      console.log("error: ", err);
      result(null, err);
      return;
    }

    console.log(`deleted ${res.affectedRows} custs`);
    result(null, res);
  });
};

module.exports = custloc;
 
Upvote 0

Jorge M A

Well-Known Member
Licensed User
Longtime User
@Erel already said you:
Maybe you need to set the content type.

@DonMan already said you how:
j.GetRequest.SetContentType("application/json")
try add a AFTER the j.poststring

You don't mention if you've tried.

This is what i'm doing in similar case:
B4X:
    j.PostString("http://192.168.1.12:3000/customers", str)
    j.GetRequest.SetHeader("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:27.0) Gecko/20100101 Firefox/27.0")
    j.GetRequest.SetContentType("application/json")
    j.GetRequest.Timeout=60
    j.GetRequest.SetContentEncoding("UTF8")
 
Upvote 0

khwarizmi

Active Member
Licensed User
Longtime User
there is no respond. I will try to upload the complete code.
 
Upvote 0

aeric

Expert
Licensed User
Longtime User
I am based on the NodeJS tutorial from https://bezkoder.com/node-js-rest-api-express-mysql/

I installed Laragon and Postman

As suggested by Erel, DonManfred and Jorge M A, the solution is to add the following line.
B4X:
Job.GetRequest.SetContentType("application/json")

Attached is code I used that successfully add the record.

B4X:
Sub AddCustomer
    Dim Job As HttpJob
    Try
        Dim Map1 As Map
        Map1.Initialize
        Map1.Put("email", "ali@adil.com")
        Map1.Put("name", "Ali Adil")
        Map1.Put("active", True)
        Dim gen As JSONGenerator
        gen.Initialize(Map1)
        Dim jsn As String
        jsn = gen.ToString
        Log(jsn)
                    
        Job.Initialize("", Me)
        Job.PostString("http://172.20.10.3:3000/customers", jsn)
        Job.GetRequest.SetContentType("application/json")
        Wait For (Job) JobDone(Job As HttpJob)
        ProgressDialogHide
        If Job.Success Then
            Log(Job.GetString)
        Else
            Log(Job.ErrorMessage)
            Job.Release
        End If
    Catch
        Job.Release
        LogColor("Error: " & LastException.Message, Colors.Red)
    End Try
End Sub
 

Attachments

  • api.zip
    8.3 KB · Views: 176
  • node-api.zip
    9.8 KB · Views: 166
Last edited:
Upvote 0
Top