Writing response from Java Http Server to jQuery - java

I wrote some Java program working on iSeries server. It can handle all Http requests to this server.
I also wrote Html page which is opened on local computer and I use some jQuery (Ajax) requests to iSeries server and I want to get some data from program which is running on it.
My server program sees that it gets some requests (POST and GET) with all parameters etc. and responses to it.
Unfortunately jQuery can't read answer data while simple Java program written and run on my local computer connects to iSeries server can read all responses from this server.
How can I write response in Java which will be avaiable to be read by jQuery?

It's very easy to process data in JSON format in the client side with jQuery. Try to program your server-side to return your data in JSON format.

Related

What must I do to receive JSON data from a server on an android app

I have PHP script on the server side which executes the SQL query and produces a JSON string from it. But how do I get that code to execute to then retrieve and decode the JSON on the Android app?
I am quite new to android so as much help would be very appreciated.
I can't write you the whole tutorial but i can give you a to-do list which you can check your progress.
Your application to generate JSON script in txt, or real time response.
Setup your backend server(you have sql server already and also PHP, so i guess you have set it up already, probably WAMP or similar), which is the endpoint/url path to generate the response required, in this case your JSON response.
Check the API is working or not by using "advanced rest client" or simple type your endpoint in browser, if you can get the correct response from it, the API is working as expected.
Created your android app, with simple HTTP request/response, send a HTTP request to the url/path you setup, see if you can get the JSON response or not.
Please comment here if you need further details, this is relatively easy and should be able to finished in 1 day for beginner.

How can I received parameters from a Java program in a PHP function

I am trying to make my java program and PHP program interoperable. Major part of the processing is carried in PHP, I need a java program to call a PHP function and send some parameters.
Can anyone help me here ?
You can implement a wannabe web service on your back-end:
Have some php script, that will be processing POST requests coming to specific URL
//in your php script you can access and process your POST data
$somekey = $_POST['somekey'];
process($somekey);
From your java program you need to create and send POST request to that URL

Starting my first android app. How can I easily test if the server has received a message?

I am experienced with Java and have some experience with PHP (the server scripting language I will be using unless anybody tells me I shouldn't for some reason. I want to just send a string via POST to the server. Because there is not actually a webpage being created, I can't just echo the string if it is received. How should I test if the string was received by the server?
Edit:
After further research, it seems like echo doesn't just print to a browser, it sends a string through the http connection to whatever is connected to the php page. I should then be able to echo a response and receive it through an input string on the Java end. Is this correct?
You could use echo, as you mentioned, but the more common and reusable method of debugging PHP as you progress is the use of error_log(). You can view its output on the PHP server in the php.log file, commonly found at /tmp/php.log.
You can watch this file in real-time via the Unix command tail -f /tmp/php.log.
Further, you can output various forms of data by calling it with print_r() like error_log(print_r($data, TRUE));.
I believe you should be properly sending back Responde codes in your php scripts.
Please check: How to send a status code in PHP and Android: How get the status-code of an HttpClient request.

Viewing HTTP POST requests send to local server

I'm sending JSON data from my Java application to my local webserver with my PHP script that is receiving this message. Now as far as I know I can only view what has been received by for example inserting the data in a database. Is there a way/application to view the live POST requests sent to my PHP webserver?
I like to use fiddler for these kinds of tasks if the java HTTP library has support for proxies. Fiddler will list all information about the HTTP requests that is available. It will by default log all HTTP requests across your system, but can be told to limit to one application.
You can try setting your httpd logging level to verbose or (depending on what httpd it is) try to use extension that would do log all the data send in requests
For debugging purposes why not just write the POST data to a file?
i.e.
file_put_contents(<some filename>, $HTTP_RAW_POST_DATA);

Connecting from phonegap android to java server

We have a server written in java, and an application written in phonegap for the android with javascript.
How can we transfer and receive information from the application to the server?
We've tried using DWR to no avail, as the html file is on the android, so we can't call methods on the server.
Is there anyway around it so we can use DWR or another method to contact the server?
Thanks
We transfer and receive information by using AJAX calls and jQuery, and setting the server headers correctly (i.e., setting "Access-Control-Allow-Origin" to "*"). This allows any client to make cross-domain AJAX calls.

Categories