Wednesday, February 27, 2019

How to create your own Web API using PHP and access it using a PhoneGap app

In this tutorial, we will create a simple end-to-end system consisting of MySQL database and also a php script to access the database with some simple API. Then, we will create a simple PhoneGap app to access the Database via the API.

Start HTTP web server and MySQL server of your xamp app. Then using phpmyadmin create a table consisting of:


Also, populate the table with some data as shown above.

In the xamp folder's htdocs folder, create a subfolder mysql and create a new file called jsonencode.php :

<?php
// This one is working
// The client is Phonegap app: PHP API Client
header('Access-Control-Allow-Origin: *');
$link = mysqli_connect("localhost","myuser","mypassword", "paul");
if(mysqli_connect_error()) {
die("There was an error");
}

//$query = "SELECT * FROM users";
$query = "SELECT * FROM users WHERE name = '".$_GET['name']."'";

$rows = array();

if($result = mysqli_query($link, $query)) {
while($r = mysqli_fetch_assoc($result)) {
$rows[] = $r;
}
print json_encode($rows);
}

?>

Create a client PhoneGap app called PHP API Client containing 2 files: index.html and app.js.

index.html :

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta
name="viewport"
content="initial-scale=1, maximum-scale=1, user-scalable=no, width=device-width"
/>
<title>Blank App</title>
<link
rel="stylesheet"
href="http://code.jquery.com/mobile/1.4.5/jquery.mobile-1.4.5.min.css"
/>
<script src="http://code.jquery.com/jquery-1.11.1.min.js"></script>
<script src="http://code.jquery.com/mobile/1.4.5/jquery.mobile-1.4.5.min.js"></script>
</head>
<body>
<div data-role="page">
<div data-role="header" data-position="fixed">
<h1>PHP Client</h1>
<div class="searchform" style="padding:6px">
<label for="text-basic">Enter Name:</label>
<input
type="search"
name="text-basic"
id="txtinputname"
value=""
placeholder="Enter Name"
/>
<input
type="button"
value="Search"
data-icon="search"
id="btnsearch"
/>
</div>
</div>
<div role="main" class="ui-content centered">
<div id="result">Search Results...</div>
<br />
<div id="list">
<ul data-role="listview" id="resultlist"></ul>
</div>
</div>
<div data-role="footer" data-position="fixed">
<h2>by PHP API Client Crew</h2>
</div>
</div>

<!-- <script type="text/javascript" src="cordova.js"></script> -->
<script type="text/javascript" src="app.js"></script>
</body>
</html>

app.js:

$('#btnsearch').click(function() {
var name = $('#txtinputname')
.val()
.toLowerCase();
console.log(name);
var url = 'http://192.168.137.84/mysql/jsonencode.php?name=' + name;
console.log(url);
//clear the listview
$('#list').html("<ul data-role='listview' id='resultlist'></ul>");

$.ajax(url)
.done(function(data) {
var obj = JSON.parse(data);
console.log(data);
//console.log(obj[0].name);
var output = '';
if (obj[0]) {
obj.forEach(function(record) {
console.log(record.name);
output +=
'<li>id: ' +
record.id +
' Name: ' +
record.name +
' email: ' +
record.email +
'</li>';
});
} else {
output += '<li>Not Found</li>';
}

$('#resultlist').append(output);
$('#resultlist')
.listview()
.listview('refresh');
})
.fail(function() {
$('#result').html(name + ' not found!');
});
});

The index.html will refer to the app.js

Then Run the PhoneGap app using PhoneGap Desktop App and connect to it with a Browser and try to access some record by keying in  a name:






Thursday, February 21, 2019

Construct 2: Building APK files using Phonegap

Configure your Construct 2 Project Settings as follows:



Note Orientations:  Landscape

Note that the default Minimum Android is 5.0 (Lolipop).  Do not change to default minimum 4.4 (Kitkat) as Phonegap will fail to build it. So devices like Asus Zenfon is out. You can refer to this version chart.


Create a Github folder within your Game Project folder.
In Construct 2, export a Cordova project with this setting:


Then delete config.json, leaving just the config.xml file. No need to edit it. Here is a sample config.xml file.

Use Inkscape to create the icon files and loading-icon and replace the corresponding files in folder www.

Then, login to github.com and create a new project. Push your project to github.

Next, login to build.phonegap.com and create a new app and paste your github .git link for your newly created git.

Unlock your Android key and build the apk file.




Friday, February 15, 2019

Creating APK file with PhoneGap

See this sample project:

https://github.com/paulchin/Get-PokeDetails

When creating a new app, be sure to download the jquery-mobile libraries instead of using links to cdn.   You can download jquery-mobile here.  After unzipping, be sure to delete the demo folder.

You also need jquery. Be sure to get the correct version 1.11.1 as at time of writing.
You can get the correct jquery  here.

Then, add a whitelist plugin in the config.xml otherwise your app won't be able to connect to any websites.

Also be sure to target sdk 26 in the config.xml as this is mandated by google.

Use png 144 x 144 pixel for the icon.

See this config.xml file.

Follow the instructions here for creating keystore:


https://www.youtube.com/watch?v=7iAPWc8IGb0





Ajax: Search Pokemon

https://github.com/paulchin/hc-webdev-notes/blob/master/4-jquery/searchpokemon.html


Ajax examples

See:

https://hjunotes.blogspot.com/

Basic template:

$.ajax('http://www.somewebsite.com')
.done(function(){})
.fail(function(){});