What is the best way to send data from Java to PHP? - java

I want to access a MySQL database from Java, but remote connection is disabled by the host.
So I will send the data to PHP and then PHP will locally access the database.
The data is pretty big (about 2~4kb)
I've never done this before.
What should I do?

Probably the best way would be to write a web service in PHP that you call from Java.
Another somewhat easier way would be to use the HttpURLConnection class to simply do a POST of the data to the PHP page.
If the PHP page is publicly accessible, you'd want to make sure you have some kind of authentication mechanism in place and use HTTPS as well.

Http web-services

Sounds like your only option is to create a Web Service. You can use Apache's HttpClient in Java to create requests and have your PHP return a response that you can easily parse. Depending on what you are trying to do, an XML response might be overkill. I've used JSON in the past if I know exactly what I am getting back.

I assume the Java program will be the client. I think the simplest would be to just create a simple PHP page, POST any possible parameters to that page from Java, and return the data as a JSON structure.
There are a number of JSON tools for Java and PHP listed on JSON page: http://json.org/

You might want to have a look to Thrift:
http://incubator.apache.org/thrift/

Are you saying that you are going to run java from php, so that php has the handle to the mysql database and you will pass the data to java?
check out java php bridge

Related

Get list variable from mysql in javascript

Hello i want to acess data from my mysql server
in javascript. But i have no clue how to do so.
I am running tomcat so it is not handy to use php.
Can someone please help me !
I thought maybe there is some way to get the variables
from my servlet, but it seems not possible to get
javascript connected to java or is there ?
var blueDates = magic way to get data from my mysql server;
Greetings,
Rick
You have to implement a servlet that returns the data from mysql. A good way to do so is to implemente a servlet that returns a JSON object containing the mysql data.
Once this is done use AJAX to ask the servlet for the data.
There is a lot of information about implementing this. After all this is a minimal REST implementation for a GET request. Just ask Stackoverflow or Google.

localhost and AJAX/JQuery

I am very new in AJAX/JQuery. I have problem with following thing. I am hoping to get some information and suggestion on following thing.
I have data real time generated data (eg. xyz) in my localhost server which i have programmed using Java.
My question is
Is it possible to retrieve this data ( i.e. xyz) from localhost server using AJAX/JQuery to JavaScript????
What function does that and how do we do that ?? Plz show me with example..it will be very great help for me
Ajax is something just like common HTTP requests, but it is sent inside your jvascript code and can be moderate with javascript.
Independent of the server side script you have, you can use Ajax to send requests and get responses from that server side application (In server side it can be treated as a simple HTTP reauest).
Finally this is a helpful toturial: W3School Tutorial , and if you are familiar with JQuery or using it in your current project, it is the reference for JQuery Ajax: JQuery.ajax, both with samples.

how to retrieve data from phpmyadmin and display in android application

i had used eclipse to develop an android apps that can allow user to register.
after they register successfully and go into the main page, they are able to see their own profile which consists of the details they keyed in during register. (The details will saved into phpmyadmin)
any method can retrieve those details from the database and display it on the profile page? (i am using php and java)
thanks for answering..
You can always use "Zend AmF" or "AMF PHP" for comunicating with the app, binary.
http://www.silexlabs.org/amfphp/
http://framework.zend.com/manual/1.12/en/zend.amf.html
or amf mixed with codeigniter
https://github.com/EllisLab/CodeIgniter
Then use:
- http://code.google.com/p/klio-mobile-dataservice/
to connect with your server
You need to have a server, like a Tomcat, or anyother server, to which you will post the data
The Servlet or the PHP script, will read the values, store them in database.
The Android application will get the data from another Servlet or a PHP page, using a HTTP Get request, and show it on the screen.
You can alternatively, store the keys locally in the device, using SharedPreferences, and get them in the next screen.
You have to write a php script wich parses data from your database into for example JSON-Format. If you had parsed the json you can use the JSON parser in android to show he content in your activitys.
Here is a very nice tutorial for json parsing in android:
http://www.androidhive.info/2012/01/android-json-parsing-tutorial/
Basically you have to create Web Service in PHP or Java and from there you need to get your data.
You have to create Restful API which returns data in the form of XML or JSON and you need to parse those data at android end. By this way you can get your data from PHP.
Here is the best resource which i found when i was searching for this kind of scenario. The author has explained very well in this and you will get very good idea about the scenario. Also provide sample code for reference.
Refer this link.
Hope this helps!

JSONP or other alternatives?

I a deveveloping a web site that comunicates with a custom made webserver by me in Java. The web site is made in PHP/JavaScript/JQuery running on Apache and i made a simple second webserver in Java to support some designed features by me, and this server runs under another port XXXXX. The problem is, i want to make requests in jQuery to second server the domain is diferent, the page runs on domain and the $.getJSON function calls domain:XXXXX wich is not allowed. I thought user $.getJSONP but im concerning concerned issues. The connections between two points is authed (i was think by passing a token beyond the callback generated by jquery). The two poins are supported by. Is there safe in this case use $.getJSONP or exists other alternatives thinking in browsers support(IE7+ and FF3+).
Sorry for my english :)
Best regards lealoureiro
JSONP should work for your needs, however your other option would be to have a proxy service on your second server that would make the request server side. Your client-side code could then access all the data natively via json instead of jsonp.

How to implement communication between Java client application (Android) and PHP server application?

I have a simple Java client application (Android app). I have to write a PHP server application which receives a request from the Java client application to write some data to a MySQL database or read some data from the MySQL database. It should respond with a status message (Write failed/success) or the data requested respectively.
How would I get the Java client send a request and receive the reply from the PHP program and how would the PHP program receive the request and send the reply? I have googled about SOAP and REST architectures, but looking for a simple tutorial which will allow me to implement this simple program.
Thanks.
With basic Java SE API you can use java.net.URLConnection to fire a HTTP request. A basic example of firing a GET request can be found in Sun tutorial on the subject. A POST request isn't much different, you instead just need to set URLConnection#setDoOutput() to true and write the query string to URLConnection#getOutputStream() instead of in URL.
Note that it's "lazily executed", the request will only be fired if you actually obtain the response stream by URLConnection#getInputStream(), even though you don't need it.
If you want less verbose code and/or more control over the request, then I can recommend to use Apache Commons HttpComponents Client.
The PHP program in turn can just be written the usual way. Get request parameters by $_GET, $_POST and so on and echo the response. Nothing special needs to be done here. You may however consider to use an more easy parseable response format, such as XML or JSON.
You should build a simple JSON or XML based REST web service with PHP.
Then with the Android SDK, you can use the HttpClient module to connect to your web service. The SDK also provide a JSON and XML module to parse the result.

Categories

Resources