I'm currently trying to get fingerprint templates from java, I have already sent the templates over HTTP request but I don't know how to get it on Laravel PHP backend to save them as blob on database, I'm sending templates as ByteArrayInputStream. Thanks in advance!
My code:
ByteArrayInputStream[] templatesArray = new ByteArrayInputStream[4];
for(int i=0; i<fingers.size(); i++)
{
templatesArray[i] = new ByteArrayInputStream(fingers.get(i).getTemplate().serialize());
}
Next I send that array over http with Retrofit. I'm getting them as usual on laravel using:
$request->input('fingers')
Is it a POST? Suppose from a practical debugging standpoint I'd suggest starting with a limited script in that PHP apps public/root that simply does a var_dump($_REQUEST); If you don't get data there you will need to look at limits in the PHP environment on max_post_size.
If you do get data you should drop the java tag, add a Laravel tag, and slightly rephrase the question with what you've learned.
Assume it's essentially a file upload. You don't need to base64_encode anything on that Java side do you?
Related
I need to send google analytics client id to server side where we will be running google measurement protocol API.
We have our GA implemented via GTM.
As per google recommendation, i am not supposed to parse _ga cookie hence i tried going through the recommended way. if i paste the code below on the console, it works fine but give me error ga reference is not defined as soon as i include this code within my jsp.
trackers = ga.getAll();
var i, len;
for (i = 0, len = trackers.length; i < len; i += 1) {
if (trackers[i].get('trackingId') === "UA-62222232-2") {
console.log(trackers[i].get('clientId'));
}
}
Please let me know what is the correct way of getting ga client id? i am unable to do so via jsp and thinking if it would be better to parse cookie within java code.
TIA
If you put the above code in a jsp file it becomes simply Javascript in the browser, so that's what you need to debug.
But I actually recommend that you ignore Googles' warning and simply parse the cookie. Even if you retrieve the clientId via Javascript you'd need to pass it on to the server, having the cookie parsed by server-side code solves that problem.
If you really want a fool-proof solution you can create and maintain a clientId in your Java code and pass it into the GA tag (e.g. via a datalayer variable since you use GTM). Since then you control the format for the client id yourself you can be sure that there are no surprises.
Front end: HTML
Backend: Java servlet
I have to get data from database to vue.js to show in an HTML tag without the servlet's help. How can I get that data?
The task is a todo list. When I click on a specific button, text is stored to a database. After that, I have to display all the content that is stored in the database. This means all the data of the todo list should be displayed without the help of a select query from the servlet. How is this possible?
You have to run your servlet eg http://localhost:8888
Create API in your servlet like: POST /api/tasks/ for creation, GET /api/tasks/ to get all tasks etc. I am not Java programmer so I am using general conventions as making API is similar in all languages. If you don't know how - use Google to find something like 'Creating API in Java'
Add eg. Axios to your Vue application.
Consume your API with axios like
Vue.axios.get('http://localhost:8888/api/tasks').then((res)=>{
//server response data is available in res.data
// do whatever your want with your data
})
Hope it helps.
I've been researching networking in android and have become familiar with the basic concepts. However, I've recently been working on an app intended for use by my school as a gradebook/assignment display of sorts. My school uses the following online gradebook: https://parents.mtsd.k12.nj.us/, and currently I'm trying to allow students to input their login credentials into my app which will then try to retrieve their respective information and display it in a gridview. However, I have never attempted to log in to a website from an android app before parsing data (I have gone through several similar questions but I am still a little confused)
Any help is appreciated!
[edit] After logging in I will use import.io to display gradebook data, website does not have api
If I understand You clearly u need to :
Connect to site using HttpURLConnection with user creditentials (do Post request to login page)
Obtain cookies from request (my favorite way is to parse - copy appropriate header field)
Use obtained cookies with HttpURLConnection.setRequestProperty("Cookie",obtainedCookies) and do another request(to user data page).
Get input stream
Parse stream
(to obtain Document "html" u can use jsoup example:
Document doc = JSoup.parse(stream, null, "");
Show data
GUIDELINES:
cookie how to
Most useful example
Caution:
-any http requests needs to be done outside main thread (ui thread) - u can use async task for this or intent service
EDIT on questions:
you ask whether to use Jsoup to handle connection?
my answer is NO!
Jsoup is a PARSER by using HttpURLConnection u gain full control over HTTP request so for example u can handle some specyfic exceptions or request propertiers which jsoup is not capable! from my experience after a while I start to disassemble the libraries and learn from them the basics and use parts of them in my code!
You could make a WebView with a custom HTML file which has a login and password field, and then have the HTML file run the same script as the website to log them in.
I have been working on an android application project that uses HTTP Get to send and receive data from MySQL through a PHP file using JSON from Java.
I have lately been running into some issues in theory behind best practices using HTTP Transport and passing Parameters via a URL.
First Question:
How should I be passing my data to my PHP Webservices ?
Currently I am just passing the data through single parameters using key value pairs like so:
myurl.com/retrieveinfo.php?user_id=453&password=sha1-hash-value
Should I be moving this type of request to append a JSON object onto the URL instead? like so:
myurl.com/retrieveinfo.php?{\"users\":{\"username\":\"User1Name\" ,\"user_id\":453 , \"password\":\"sha1-hash-value\"}}
Second Question:
*How should I be handling the JSON Response from the Server ? Do I need to push this work off to a handler and make sure the UI Thread is not the one doing this work? *
Currently I am just parsing the JSON using separate methods for each Object Type such as
User.Class
private void parseUserInfo(JSONObject response){
// Do all my Parsing for a User Object
try{
JSONArray users = response.getJSONArray("users");
JSONObject user = users.getJSONObject(0);
// Get the User info etc...
}catch(JSONException ex){
ex.printStackTrace();
}
}
Notes.Class
private void parseNotes(JSONObject response){
// Do all my Parsing for a Note Object
try{
JSONArray notes = response.getJSONArray("notes");
for (int index = 0; index < notes.length() ; index++)
{
JSONObject note = notes.getJSONObject(index);
// Get all the note info etc...
}
}catch(JSONException ex){
ex.printStackTrace();
}
}
Third Question:
I would like my PHP server files to only work for my Application. So what is the best way to secure my PHP files on my server so a request to my files wont go through if its run in a browser ?
Should I be sending some temp key that only my application knows about ?
Thanks
First Question:
You don't really want to put a JSON object on the url as a query parameter. The real two debates that I see is that you either 1) use the key value pairs you were using, or 2) make this a POST and send the JSON as a payload.
Since you are not planning on exposing the API to anyone, I don't really find it important for you to follow standard nomenclatures. Do whatever you want to do. However, from a REST standpoint, anything that retrieves info should be a GET call, and the data should be key-value pairs on the query string. However, it looks like you are passing in a username and password (ok, the sha of the pass). It is considered best practice to always pass user info as the payload. So almost all login type protocols use a POST for user data. User-id's or session id's are common in the query string but usernames and passwords should almost always be in a payload.
Note: sometimes in TLS (SSL) it is considered ok to include these things in the query string.
Second Question:
Honestly, I would just use Jackson. https://github.com/FasterXML/jackson
But otherwise, it is normal to have a seperate layer for parsing. In otherwords, one class handles all the parsing. You do not want to put this code inside your models if you can avoid it. The new layer would handle parsing and would pass the Java Model objects down to the next layer.
Third Question:
The easiest way to do this would simply be to check the user-agent header on the request. Make sure that the user-agent is your application, and not a browser.
However, it would still be possible for people to "spoof" this. Using a temp key wouldn't really help either, because once people sniff the traffic they can figure out the temp key.
The standard thing here is to do some type of session based key, where the application sends some type of MAC in order to prove it is a valid client.
You could also consider using OAUTH2 to protect your api's.
On one side I have a Java program that keeps feeding a database. On the other a web server (Apache). I need to transfer multiple entries, at once, from the program to a distant web server via a REST webservice.
While I have no trouble to send pairs of values from one to the others (using a HttpClient on the Java's side and a PHP file that writes into the server's own database on the other side), I lack knowledge about how I could build my HTTP request with data.
I've been using an Entity so far :
List<NameValuePair> list = new ArrayList<NameValuePair>();
list.add(new BasicNameValuePair("field1", "aaa"));
list.add(new BasicNameValuePair("field2", "bbb"));
httppost.setEntity(new UrlEncodedFormEntity(list));
and receiving it in my PHP file :
<?php
$field1=$_POST["field1"];
$field2=$_POST["field2"];
$con= mysql_connect("localhost","root");
if(!$con) die("Not able to connect");
mysql_select_db("mydb",$con);
mysql_query("INSERT INTO `mytable` (`field1`, `field2`) VALUES ('$field1', '$field2')");
mysql_close($con);
?>
Works great. Simple and easy. But now, as stated before, I wish to transfer several entries from my database. Each entry consists of 35 fields, which yields to some big data.
My question is : How can I insert my database entries within ONE (or as many as needed if one HTTP request capacity is full) and fetch the resulting data on the PHP file side ?
Bonus : with the previous solution, how can I compress that data?
You can use JSON for quick and simple transfer. For decoding, PHP supports $array = json_decode($json_string, TRUE);.
Not sure about compressing. Are you going to transfer lots of data this way to make it relevant?
Well, the thing you are doing will lead to SQL injection easily. Make sure you escape your data before running the insert query with mysql_real_escape_string or better - prepared statements.
Instead of using JSON, you could use Google Protocol Buffers. You need both JAVA and PHP extension to use protocol buffers but this is binary protocol that compresses the data amazingly well and allows you the flexibility to define different "repeatable" objects (entries) with same properties, so that when you open the protocol buffer on the PHP side, it will be easy to utilize the data.