Alright, so I have attempted to learn this JSON business, and have run
into some issues. I followed the tutorial at:
http://code.google.com/webtoolkit/doc/latest/tutorial/JSON.html

Once I thought I understood it, I began to implement it within my
project. However, I cannot successfully retrieve the data. I have a
Data.php file that successfully connects to my server and reads in the
data, and formats as JSON data should be. Yet, when I run my project,
I get an HTTP response code of 0, and thus no data returned.

I believe the problem to be with my url. I have noticed that the way
the tutorial above is set up, the user of the stock watcher
application must enter a symbol, and then the url is adjusted to
request that symbol. However, I have coded my Data.php to output all
entries in the table, as I want to access and display all entries upon
startup. Therefore, I am confused as to how my url should be formated.
Here is the code for my Data.php, excluding username and password
info:

<?
        header('Content-Type: text/javascript');
        header('Cache-Control: no-cache');
        header('Pragma: no-cache');

        //username and password stuff here

        $con = mysql_connect($host, $user, $pass);
        mysql_select_db($db, $con);

        echo '[';

        $result = mysql_query("SELECT * FROM sampleTreeTable");

        $i = 0;

        while($row = mysql_fetch_array($result)) {
                if($i > 0) {
                        echo ',';
                }
                $tItem = $row['treeItem'];
                $tInfo = $row['treeInfo'];

                echo '{';
                echo "\"treeItem\":\"$tItem\",";
                echo "\"treeInfo\":\"$tInfo";
                echo '}';

                $i = $i + 1;
        }

        echo ']';
?>

This outputs my simple little test data as:

[{"treeItem":"Requirements","treeInfo":"5},
{"treeItem":"Decisions","treeInfo":"4},
{"treeItem":"Tradeoffs","treeInfo":"6},{"treeItem":"Co-
occurances","treeInfo":"8}]

The JSON url I am using is "http://xxxxxx.xxx.xxxx.xxx/Data.php";
I noticed that in the tutorial, they would have me add something like
"?q=" and then the name of each of the items I want separated by +.
However, I want to simply request all the data from Data.php.

So, how can I write a url to do this?

On Jun 25, 5:04 am, Chris Boertien <[email protected]> wrote:
> You might want to take a look at 
> this:http://code.google.com/webtoolkit/doc/latest/tutorial/JSON.html
>
> Although the examples there are using a servlet for the server-side,
> that is not a requirement. The server simply needs to be accessible at
> the requested URL and output the formatted JSON.
>
> The basic idea is to make a request to the server and have the server
> send back the json data structure. You can then eval, or use a js json
> parser, and setup Overlay types so you get to use the objects like
> regular java objects.
>
> 2010/6/24 Jaroslav Záruba <[email protected]>:
>
>
>
> > I believe the module loads on document's load event, therefore pausing PHP
> > does not help indeed.
> > You can load the JSON using JsonpRequestBuilder. That means another
> > (XHR)request to server. If that's an issue I'd try to output the JSON using
> > PHP as you do now, but not into <script/> element. I would hide it within
> > the page and afterwards convert that JSON-string into a JS-object in GWT.
> > Or you could place the JSON-string into a <script/> element using GWT, tthat
> > should fire the addTreeItems-method right away.
> > When I needed to load some data from (3rd-party) server I created my
> > receiving JS-function and the <script/> element with src-attribute by myself
> > in onModuleLoad. It worked. And I guess that's similar to what the
> > JsonpRequestBuilder does, but I did not know that class before. I'm quite
> > new to GWT.
> > That's also kind of disclaimer: I'm not sure whether the above is the best
> > and most elegant solution. :)
> > On Fri, Jun 25, 2010 at 3:25 AM, [email protected] <[email protected]>
> > wrote:
>
> >> Thanks, that makes sense. However, do you have any suggestions on how
> >> I might work around it? I want to be able to load all of the data on
> >> startup, so I have called the PHP function that calls the javascript
> >> function in the <body> of the .php file. I tried to throw in a PHP
> >> sleep to delay the javascript from being called, but the sleep seems
> >> to also delay the loading of the module, thus really giving me no
> >> benefit. Any other suggestions would be great.
>
> >> On Jun 24, 8:47 pm, Jaroslav Záruba <[email protected]> wrote:
> >> > It seems that your <script/> might get evaluated/executed before the
> >> > module
> >> > has finished loading and therefore your function window.addTreeItem
> >> > might
> >> > not exist yet.
> >> > Several workarounds for that are possible I think.
>
> >> > On Thu, Jun 24, 2010 at 9:39 PM, [email protected]
> >> > <[email protected]>wrote:
>
> >> > > Hi everyone,
>
> >> > > I am faced with a situation in which I must pass variables read in
> >> > > from a database table using PHP to my GWT application. I am doing this
> >> > > by using PHP to read in the data(which are Strings), and then calling
> >> > > a JSNI function from my GWT application like so:
>
> >> > > $result = mysql_query("SELECT * FROM sampleTreeTable");
>
> >> > > while($row = mysql_fetch_array($result)) {
> >> > >       $temp = $row['treeItem'];
> >> > >       echo "<script language=javascript>addTreeItems('$temp');</
> >> > > script>";
> >> > > }
>
> >> > > I know that my echo statement works, because when I call other locally
> >> > > defined javascript functions in the place of addTreeItems, they
> >> > > execute just fine. It is only the JSNI function addTreeItems that does
> >> > > not seem to execute.
>
> >> > > The function addTreeItems is defined in the Java source code for my
> >> > > GWT application as follows:
>
> >> > > public void serverAdd(String s) {
> >> > >       //some code
> >> > > }
>
> >> > > public native void setShowTrigger(TreeTest x)/*-{
>
> >> > >          $wnd.addTreeItem = function(s) {
>
> >> > >  [email protected]::serverAdd(Ljava/lang/
> >> > > String;)(s);
>
> >> > >          };
> >> > >  }-*/;
>
> >> > > When running my GWT application in development mode, accessing
> >> > > addTreeItems through the html file works just fine, most of the time
> >> > > anyways. Sometimes when I first start debugging it will not execute,
> >> > > but if I refresh the page it always executes just fine.
> >> > > The real problem occurs once I compile my application and deploy it to
> >> > > my web server. I seem to be unable to call addTreeItems once my
> >> > > application is deployed. Once I deploy my GWT generated html file, I
> >> > > change the file from a .html to a .php file so that I can add the PHP
> >> > > code, and everything still works just fine except my JSNI addTreeItems
> >> > > method.
>
> >> > > So basically, I am completely lost on this issue. Anyone have any idea
> >> > > how to accomplish what I am trying to do? I know I could use RPC to
> >> > > connect to the database and do most all of this within my GWT
> >> > > application, but the requirements of this project stipulate that I
> >> > > avoid doing so. Any help at all would be great.
>
> >> > > --
> >> > > You received this message because you are subscribed to the Google
> >> > > Groups
> >> > > "Google Web Toolkit" group.
> >> > > To post to this group, send email to
> >> > > [email protected].
> >> > > To unsubscribe from this group, send email to
>
> >> > > [email protected]<google-web-toolkit%2Bunsubs
> >> > > [email protected]>
> >> > > .
> >> > > For more options, visit this group at
> >> > >http://groups.google.com/group/google-web-toolkit?hl=en.
>
> >> --
> >> You received this message because you are subscribed to the Google Groups
> >> "Google Web Toolkit" group.
> >> To post to this group, send email to [email protected].
> >> To unsubscribe from this group, send email to
> >> [email protected].
> >> For more options, visit this group at
> >>http://groups.google.com/group/google-web-toolkit?hl=en.
>
> > --
> > You received this message because you are subscribed to the Google Groups
> > "Google Web Toolkit" group.
> > To post to this group, send email to [email protected].
> > To unsubscribe from this group, send email to
> > [email protected].
> > For more options, visit this group at
> >http://groups.google.com/group/google-web-toolkit?hl=en.

-- 
You received this message because you are subscribed to the Google Groups 
"Google Web Toolkit" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/google-web-toolkit?hl=en.

Reply via email to