Sending Java server data to a React front end - java

Here is what I've done so far: There is an API (Hacker News - https://github.com/HackerNews/API) from which I send a GET request from my Java program and retrieve the data. I am able to parse the (JSON) data so that it can be manipulated by the JDK.
Here is what am trying to do next: I want to have my Java program/server communicate with my React application so that React can retrieve the parsed JSON data and display it on a webpage.
Confusion: How do I have React get the data? Do I set it up so that it sends a GET request from my Java server? I have consulted more tutorials than I can count, but I am simply not understanding how to do it.
My code can be found here https://github.com/EmekaEnshinyan/HackerNews-API-Design-Back-End

Your React application is the frontend, and the Java program is the backend, in which case you would send a GET request from React to the Java program using clients like Axios or the built-in fetch.
The Java program will need to be served on a server or local development server to receive, handle that request, and send a JSON response back to React. You can take a look at creating a RESTful API in Java.

Related

Android studio jDBC not connecting to the mysql server with jdbc [duplicate]

This question already has answers here:
Can an Android App connect directly to an online mysql database
(8 answers)
Closed 6 years ago.
I have a website already setup which uses mysql database. I want to know how can i connect my app to that database.
What i wanna achieve is that my app should make a request to find a table of a defined "ID" or name. The table contains links to images or image names. I want the app to retrieve those images and display them on the android app.
What are the ways this can be done?
Can I use PHP to develop an android app?
Android does not support MySQL out of the box. The "normal" way to access your database would be to put a Restful server in front of it and use the HTTPS protocol to connect to the Restful front end.
Have a look at ContentProvider. It is normally used to access a local database (SQLite) but it can be used to get data from any data store.
I do recommend that you look at having a local copy of all/some of your websites data locally, that way your app will still work when the Android device hasn't got a connection. If you go down this route then a service can be used to keep the two databases in sync.
The one way is by using webservice, simply write a webservice method in PHP or any other language . And From your android app by using http client request and response , you can hit the web service method which will return whatever you want.
For PHP You can create a webservice like this. Assuming below we have a php file in the server. And the route of the file is yourdomain.com/api.php
if(isset($_GET['api_call'])){
switch($_GET['api_call']){
case 'userlogin':
//perform your userlogin task here
break;
}
}
Now you can use Volley or Retrofit to send a network request to the above PHP Script and then, actually the php script will handle the database operation.
In this case the PHP script is called a RESTful API.
You can learn all the operation at MySQL from this tutorial. Android MySQL Tutorial to Perform CRUD.
Yes you can connect your android app to your PHP to grab results from your database. Use a webservice to connect to your backend script via ASYNC task and http post requests. Check this link for more information Connecting to MySQL
Use android vollley, it is very fast and you can betterm manipulate requests.
Send post request using Volley and receive in PHP
Basically, you will create a map with key-value params for the php request(POST/GET), the php will do the desired processing and you will return the data as JSON(json_encode()). Then you can either parse the JSON as needed or use GSON from Google to let it do the parsing.

react native receiving push notification from pentaho data service through java backend server

So I am using pentaho data integration to do data transformation. Then, using that transformed data, I would like to let the mobile(react-native) users know if something important changes. So here's my plan to do it.
I will set up the java server API in-between react-native and Pentaho.
make Pentaho transformed data to be accessible from java API through pentaho data service using JDBC.
Java API will check periodically to that data service and compared with previously stored data
If something important changes java api will send APNS and GCM to send notification for mobile users
My question then is,
is this the correct way to do it?
can java api check the pentaho data service periodically?

Is it secure to build android app that works with PHP API on browser?

I'm learning to build android apps using java & android studio.
For my app i need internet connection and a server side database, I'm already php developer so i tought to build a PHP web API and by json, send and recieve data from specific URL.
For example, any user that will register inside my app, i can send his data to:
(via POST or GET method.)
example.com/api/user_register.php
Is it secure? How can i get data and send to an existing domain in java?
I'm looking for something similliar to file_get_content() or CURL in PHP.
Also i need to build something like push notifications..
Creating an web API to access a MySQL Database for Android is expected as there is no direct connection to the database possible on Android.
I've done something similar with one of my own apps. How secure it is depends on how you make it and where your PHP API is. I.e. is the data being sent to/from HTTP or HTTPS. In my own app, I encrypt the post data on the device, post it to my PHP API which decrypts and processes the request.
It then builds a response and json_encodes it, which is then encrypted and sent back to the device, where the device decrypts it and processes the JSON result. All of this is also done over HTTPS so hopefully, its fairly secure.

Is it possible to add PHP scripts in a Java server?

I have a java server running with the spring framework. For now I have to develop a GCM service to send data from the server to an Android app.
I have been seeing some tutorial on Internet and all of them implements this service in a php script. I'm wondering if I can use them in my java server with spring.
I know I can rewrite the code to Java but I don't have so much time as it's a work for University, so I don't wanna waste much time rewriting all of this..
So I'd like to know if it is possible to add these php scripts and if so, how can I do it?
Even if you interface your Java server with some PHP code that sends GCM messages, you would still have to pass registration IDs and notification payload to that code. Wouldn't it be easier to simply use the gcm-server.jar supplied by Google?
Using gcm-server.jar, the code you need to send a GCM message is as simple as this :
Sender sender = new Sender(apiKey);
Message message = new Message.Builder()
.delayWhileIdle(true)
.addData("key1", "value1")
.addData("key2", "value2")
.build();
Result result = sender.send(message, registrationId, numOfRetries);
If you wish to handle error responses, you should check the result instance to see if the message was successfully received by the GCM server, and if not, what error occurred.
The only other thing you'll have to implement is some servlet that accepts registration IDs from your Android app and stores them in your DB. Perhaps you can use existing PHP code for that part (you won't need any interface between the PHP code and Java code).
you can execute external programs from your java code
you will have to pass the data back and forth somehow. reading/writing stdin and stdout probably
maby this will help: Execute external program in java
Yes, you can. They have interpreters for PHP, JavaScript, and even Python written in pure Java. For PHP, one such offering is Caucho Quercus.

What to I need to do to on my server to communicate with an Android app?

I am currently writing and Android app, and I need to do some stuff with a server. Actually I just don't know where to begin, since I don't know very much about server side.
My Android app will send Java objects and images to my server, so what do I need to write or use on my server ? Is it possible to write Java code instead of PHP on the server side ?
Could you please give me the steps to prepare my server ?
Is it possible to write Java code instead of PHP on the server side ?
You can use any programming language you like for server side programming. This question provides some useful information on various Java based approaches using the HTTP protocol.
will send Java objects
You will need to serialise them to a data format. JSON and XML are popular for transmitting structured data across HTTP.
and images to my server
HTTP allows the transmission of files and Java can construct the request.
I see that you just want to communicate between the server and client(Android). I would recommend considering REST style web service for your server, where you can use either JSON (recommended) or XML format.
How to implement REST in a web application?

Categories