Archive for the ‘snippets’ Category

I needed to parse JSON response from a server.

I did my research and the most important thing i looked for was SIMPLICITY of API and small number of files to integrate to my existing project.

Finally, I found what I was looking for. Not something fancy JSON library but rather a very simple one that does the trick very nicely without any obvious bugs.

 

https://github.com/udp/json-parser

 

The code to print every Json response:

http://pastebin.com/i201FsEE

Advertisements

Perl script for batch whois

Posted: February 14, 2013 in Perl, snippets

I created a perl script that reads a file with domains to check and output the valid ones.It can also go through domain name combinations.

#!/usr/bin/perl

sub whois
{
#print “doing whois for “.$_[0].”: “;
my $domain = $_[0];
$domain =~ s/www\.//g;
open(WHOIS_CMD, “whois -h whois.isoc.org.il \”$domain\”|”) or die “Can’t run ‘$whois_cmd $domain’\n$!\n”;
$i=0;
while(<WHOIS_CMD>)
{ # each line of output is put into $_
if ($_ =~ /No data was found to match the request criteria|No match|A C C E S S D E N I E D/i)
{
#print $_;
return 0;
}
}
return 1;
}
if ($#ARGV>(-1))
{
print $ARGV[0];

my $file = $ARGV[0];
open my $domains, $file or die “Could not open $file: $!”;

while( my $domain = <$domains>)
{
chomp($domain);
if (whois($domain)==1)
{
print “$domain : ok\n”;
}
else
{
print “$domain : fail\n”;
}
}

close $domains;
}

print “\n\n”;

 

$.mobile.changePage(“#page2”);

(from this post)

ok. this isn’t working. I’m getting the following error:

02-14 18:20:12.093: E/Web Console(17689): Uncaught TypeError: Cannot call method ‘changePage’ of undefined at file:///android_asset/www/goals.html:112

bad bad bad

need to find something else. so i tried this

$.mobile.back();

still not working i’m getting this error

02-14 18:23:16.082: E/Web Console(17915): Uncaught TypeError: Cannot call method ‘back’ of undefined at file:///android_asset/www/goals.html:112

i’ll try something else… following this site i’m trying

$.mobile.loadPage( pageUrl, { showLoadMsg: false } );

shit. nothing seems to work. now i’m getting this error

02-14 18:27:24.265: E/Web Console(18181): Uncaught TypeError: Cannot call method ‘loadPage’ of undefined at file:///android_asset/www/goals.html:112

tried this:

$.mobile.changePage(“index.html”, null, true, true);

and got

02-14 18:30:33.429: E/Web Console(18465): Uncaught TypeError: Cannot call method ‘changePage’ of undefined at file:///android_asset/www/goals.html:112

it seems like $.mobile is not recognized…. 😦 what to do what to do?

i tried also to change the calling of this code not from deviceReady event of phonegap but with this:

$(document).bind(“pagecreate”, function(){

found it on this link. also not working… not even triggering…damn

also tried to change the calling to this

$(‘#goals-page’).live(“pageinit”, function() {

and failed. I’m getting this error

02-14 18:39:05.158: E/Web Console(19354): Uncaught TypeError: Object [object Object] has no method ‘live’ at file:///android_asset/www/goals.html:103

i’m strating to get pissed off.

OK. FOUND THE ANSWER HERE. I used this:

window.location.href = “index.html”;

I already created a semaphore in my java native activity. I needed it to add voice samples to a buffer from one thread and read and empty this buffer on other thread. Started to get data aborts so I investigated and found the semaphore isn’t working as I expected.

The code I use is this:

declare the semaphore:

static public final Semaphore SEMAPHORE = new Semaphore(1);

and than do

SEMAPHORE.acquire();

and

SEMAPHORE.release();

and it isn’t working… 😦

So I find this code which creates a class and looks and sleeps until lock is acquired, but I don’t like it. Can’t believe this is the solution and it looks kinda old (2004 old).

I tried to implement the code from this post, looks decent. The synchronized architecture looks very logic.

WOW this is a great improvement in performance too!!!! I guess the “old semaphore” didn’t work as well as I thought it would. The new code involves this:

public Class MyClass {
  private static Object lock = new Object();
  public void myMethod() {
    // Stuff that multiple threads can execute simultaneously.
    synchronized(MyClass.lock) {
      // Stuff that only one thread may execute at a time.
    }
  }
}

(taken from the very same post)

So just encapsulates all sync’d code in “synchronized(MyClass.lock) {}” and it does the semaphoring for you.
happy happy

I want to show the users progress over time with some charts. So here I am searching google again for some answers.

Found this link:

Create Quick and Easy Progress Bar Charts with HTML5 and CSS3

p1

This isn’t exactly what I was looking for but an easy progress bar can be quite handy. Its worth testing…

Fuck, this article has no demo. This is NOT COOL. But it was easy creating out of the code presented on the page. So I guess I will try.

Ok, got it. Here is the result:

p2

and the code is:

<html>
<body>
<style>
/* Firefox – value portion of progress bar */
progress#bar::-moz-progress-bar {
background-color: red;
-moz-box-shadow: 1px 1px 1px #333333;
}

/* Chrome, Safari – value portion of progress bar */
progress#bar::-webkit-progress-value {
background-color: red;
-webkit-box-shadow: 1px 1px 1px #333333;
}

/* All other attributes */
progress#bar {
background-color: #F0F0F0;
border: solid #808080 2px;
border-radius: 5px;
-moz-box-shadow: 3px 3px 3px #C0C0C0;
-webkit-box-shadow: 3px 3px 3px #C0C0C0;
box-shadow: 3px 3px 3px #C0C0C0;
padding: 3px;
width: 250px;
height: 20px;
}
</style>

<strong>Project Completion Status</strong><br />
<br />
Project #1<br />
<progress id=”bar” value=”6″ max=”9″>
<em>6 of 9 stages completed</em></progress>
<br />
Project #2<br />
<progress id=”bar” value=”20″ max=”100″>
<em>20% complete</em></progress>
<br />
Project #3<br />
<progress id=”bar” value=”12″ max=”20″>
<em>12/20 components completed</em></progress>
<br />
Project #4<br />
<progress id=”bar” value=”4″ max=”8″>
<em>50% complete</em></progress>
<br />
Project #5<br />
<progress id=”bar” value=”9″ max=”10″>
<em>9 out of 10 stages completed</em></progress>
</body>
</html>

This will be handy someday 🙂

The next google search result looks very good. Its specifically about jquery-mobile charting. Exactly what I need.

jQuery Mobile Charts

+ the source code on github

If this will be the result on my end, I will be a happy man indeed. Lets give it a try.

The example works just fine. Now I will try to embed this to my phonegap app. lets see how it goes…

ok. it took me 10 minutes to integrate the sample code to my html5 code. (just the sample, no functionallity yet).

I had one problem with the addition, at first it didn’t work and than I tried to add this code:

<script src=”JQMCharts/scripts/jquery.jqplot.min.js” type=”text/javascript”></script>
<script src=”JQMCharts/scripts/jqplot.pieRenderer.min.js” type=”text/javascript”></script>
<script src=”JQMCharts/scripts/jqplot.barRenderer.min.js” type=”text/javascript”></script>
<script src=”JQMCharts/scripts/jqplot.categoryAxisRenderer.min.js” type=”text/javascript”></script>
<script src=”JQMCharts/scripts/underscore-min.js” type=”text/javascript”></script>
<script src=”JQMCharts/scripts/hideAddressBar.js” type=”text/javascript”></script>
<script src=”JQMCharts/scripts/app.js” type=”text/javascript”></script>

to the end of the file instead of the beginning.

this what fixed my problem.

I have no idea why…

anyways i don’t like this code so much. not very scalable…

So I gonna try this library:

http://www.jqplot.com/

ok so i downloaded jquery.jqplot.1.0.5r1122 and I’m following the basic installation instructions from the jqueryPlot website I hope it will be easy as it looks.

I added two files to my project and added the following lines

<!– jqueryplot –>
<script language=”javascript” type=”text/javascript” src=”jqueryplot/jquery.jqplot.min.js”></script>
<link rel=”stylesheet” type=”text/css” href=”jqueryplot/jquery.jqplot.css” />

I added a chart container:

<div id="chartdiv" style="height:400px;width:300px; "></div>

. and added the graph creation code

$.jqplot('chartdiv',  [[[1, 2],[3,5.12],[5,13.1],[7,33.6],[9,85.9],[11,219.9]]]);

not worknig 😦

I will try to add

<script language=”javascript” type=”text/javascript” src=”jqueryplot/jquery.min.js”></script>

as well.

Still nothing.. the chart isn’t showing…

GOT IT!! The problem was as usual that my code aims for phonegap so the $.jqplot(‘chartdiv’,  [[[1, 2],[3,5.12],[5,13.1],[7,33.6],[9,85.9],[11,219.9]]]); code wasn’t triggered because it was in the deviceReady event function. When I created a window.onLoad function instead – everything worked on Chrome.

 

Now I need to create a vertical bar chart. So I found this link.

Also succeeded to create a BarRender chart with it. and found this pretty awsume link to how add dynamic data to a jqplot chart.

Good day,

I tried for two hours now to figure out how to programically set an option for a selectmenu by mobiscroll. I tried several suggetion found on my google searches and here are my conclusions.

mobiscroll, you need to provide a better api for this. It takes too much time to do this (very very) basic and simple task. WTF.

What i did was to set values with the val() function, than set the mobiscroll functionallity and finally loading the dialog.

$(“#updateDayOfWorkoutPlan”).val(row.dayoftheweek);
$(“#updateTimeOfWorkoutPlan”).val(row.time);
$(“#updateDurationOfWorkoutPlan”).val(row.duration);
$(“#updateAlertOfWorkoutPlan”).val(row.alert);

$(“#updateDayOfWorkoutPlan”).mobiscroll().select();
$(“#updateTimeOfWorkoutPlan”).mobiscroll().time();
$(“#updateDurationOfWorkoutPlan”).mobiscroll().select();
$(“#updateAlertOfWorkoutPlan”).mobiscroll().select();

$.mobile.changePage(“#update-page”);

this is not pretty but got to keep things moving.

ok. so in the previous posts i:

  1. opened a native java UI Activity – see post
  2. created a plugin that uses CallbackContext and updates the phonegap database – see post

I want that every several seconds the database will be updated (in case of a crash, i want the data to be valid) so now I will connect the two.

The native Activity is going to trigger the callback function every some and some seconds so database is continuously updated.

 

so on the javascript side when user hits the “new” button:

function startWorkout ()
{
gCurrentWorkoutTimestamp = Number(Date.now())/1000;
// add a new item to database with the new workout
gWorkoutsTable.addWorkout({“date”: gCurrentWorkoutTimestamp, “duration”: 0});

// set a callback functionallity from native to phonegap to update database constantly during workout
window.registerWorkoutUpdate(
function(duration) {
gWorkoutsTable.updateWorkout({“date”: gCurrentWorkoutTimestamp, “duration”: duration});
loadWorkouts();
}
);

// now start native java activity
window.MainActivity.phonegapInterfaceLaunchTherapyUtility();
}

you can see that on a new workout, i’m starting a new timestamp (same as ID), adding a new sql INSERT statement and create the item in the database, registering in the phonegap plugin to get updates (on every update trigger an UPDATE sql statement with a new value) and finally – start the native UI java activity with phonegap js interface (same way i describe on this post).

 

Everything works just fine! 🙂