1

What is the best way to make HTTP GET in PhoneGap? I don't want to use Java for this, so now the question is whether it should be done in jQuery, or in JavaScript. I read that JavaScript in PhoneGap is mostly used for UI events. Is there any other way to accomplish HTTP GET in PhoneGap?

EDIT: In the book written by Andrew Lunny (PhoneGap Beginner's Guide), author mentions and gives examples about how to access remote resources utilizing Twitters search API - making HTTP requests to the server. In this case it is not very useful to me, because he is getting JSON object as a response from server. In my application I need strict html body as a response.

EDIT(as proposed by codemonkey and Simon MacDonald):

<link rel="stylesheet" href="http://code.jquery.com/mobile/1.0/jquery.mobile-1.0.css" />  
<script type="text/javascript" src="http://code.jquery.com/jquery-1.4.3.min.js"></script>
<script type="text/javascript" src="http://code.jquery.com/mobile/1.0a1/jquery.mobile-1.0a1.min.js"></script>

<script type="text/javascript">
$.get('http://www.google.com', function(data) {
      alert(data);
    });
</script>

In this case question followed out of proposal:

Should I be using external libraries (jQuery-mobile/jQuery) or is it better to include them in the project. Of course with the first proposal I will force user to download libraries every time he will be forced to use them, which is a little inefficient, or should I just include them in the project? In this case, what is the proper location in the project(file system) to put jQuery libraries, 'www', 'libs', 'res', etc.?

As with the proposal of Simon MacDonald:

<script>
function get() {
        var request = new XMLHttpRequest();
        request.open("GET", "http://www.google.com", true);
        request.onreadystatechange = function() {
            if (request.readyState == 4) {
                if (request.status == 200 || request.status == 0) {
                    // -> request.responseText <- is a result
                }
            }
        }
        request.send();
    }
</script>

Advantage: No additional libraries are needed

Michael Petrotta
  • 59,888
  • 27
  • 145
  • 179

2 Answers2

4

Well you can use what Andrew wrote as there is no reason why you can't just get the html code instead of JSON from the remote url. You can use jQuery to do the GET but if you don't need to add that library you can just use straight XHR.

Here is a quick tutorial I wrote:

http://simonmacdonald.blogspot.com/2011/12/on-third-day-of-phonegapping-getting.html

On the line:

var tweets = JSON.parse(request.responseText);

you would just do:

var myBody = request.responseText;

as it would be the full HTML of the page you requested.

Simon MacDonald
  • 23,253
  • 5
  • 58
  • 74
  • Your proposal is easy to use and does not require any additional libraries. For now it is the best solution out of all the proposed ones –  Jan 04 '12 at 16:09
0

I think jQuery would be your best option. PhoneGap does not provide anything to make GET requests. It's main role is to allow your HTML/JS application to access native resources.

codemonkey
  • 5,257
  • 2
  • 18
  • 16