Category: Code

To make HTTP request to remote services, we have to use Meteor.http package.
Meteor.http package can be added to project by command “meteor add http”.

Meteor.http supports both synchronous and asynchronous requests. But in this example/usecase, we want to return results to client. And Meteor by default supports synchronous services on server (which conflicts with node.js asynchronous model), so I will use synchronous Meteor.http api in this example.

var url = 'http://.....';
var result = Meteor.http.get(url, {timeout:30000});
if(result.statusCode==200) {
    var respJson = JSON.parse(result.content);
    console.log("response received.");
    //custom code
    if(respJson.message.code == 0) { //IF OK
        console.log('returning response');
        return respJson;
    } else {
        throw new Meteor.Error(respJson.message.code, respJson.message.text);
    }
} else {
    console.log("Content issue: ", result.statusCode);
    throw new Meteor.Error("");
}

Full source code is available at https://gist.github.com/2922814
Live demo (twitter): http://remoteservice.meteor.com

Default Phonegap way of interacting with the Native code is to initiate a plugin request from Javascript, process in the Native code and reply the result with data which can be used in Javascript again. However many times it may require to update Javascript values or HTML DOM elements without initiating any plugin call from Javascript. Below code snippet uses a Plugin method as a gateway to call a Javascript method from Native Android code.

Example with full source code for Android is available here

By default PhoneGap File API creates only one directory at a time and the developer has to write the additional code in case nested directory structure is required. Below is an example of a small utility method which creates the nested directory structure based on the given path.