I copied an example from a YouTube video and got this barcode reader working on my phone.
My issue is I’m literally cutting and pasting the code without knowing how java works.
So far I’ve been able to google the errors to get the app working and transferred from my local host to my phone but I don’t know what to google anymore.
I want to take that number the barcode reader is getting and use it to fill out a rest API from open food facts that would looks something like this in bash: https://world.openfoodfacts.org/api/v0/product/${scanResults}.json
Then I want to curl the json blob and get the ingredients object.
Once I have the ingredients, I want to diff that against a list of known allergens and return a response to the screen of something like “You are allergic to this” or “You are not allergic to this.”
Here’s a link to my repo and there’s some commented code where one my my friends suggested using URL, but I haven’t had the chance to try that yet.
We don’t tend to do other people’s coding for them here. You might get some suggestions on how to approach the problem or where to look for solutions, but I think you need to learn java.
I’ve edited your post for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.
You can also use the “preformatted text” tool in the editor (</>) to add backticks around text.
I’m just kind of blogging stream of conscience here while I’m puzzling this out, don’t mind me or chip in if you have something helpful. Any help is appreciated.
It’s looking like I’m setting the textView to the results of the barcode scan here: textView.setText(intentResult.getContents());
So now I’m thinking the number I’m looking for is in intentResult and we’re using .getContents() to grab it.
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
IntentResult intentResult = IntentIntegrator.parseActivityResult(requestCode, resultCode, data);
if (intentResult != null) if (intentResult.getContents() == null) {
textView.setText("Cancelled");
} else {
// pretty sure this is how I get the string the barcode scan is returning.
String resultContents = intentResult.getContents();
textView.setText(resultContents);
}
super.onActivityResult(requestCode, resultCode, data);
}
have you not tested if its the string barcode coming back?
try printing it to console see if its a code
im not used to java very much so i cant help you to much, iv only used kotlin a few times and the rest has been react native
this should be a straight one from the android java to the url and ready to go situation, but you first need to check if your getting the code from however your getting the code(barcode) from, and as iv said i dont know java so i dont know if String resultContents = intentResult.getContents(); is the right way or not
all you have to do is use println(scanResults ) to see if your getting a code, if not then we will atleast know that the problem is coming from fetching the barcode part and work from there.
I’ve updated main with a try / catch block and the code builds now but I haven’t tested it.
I’m assuming it won’t do anything right now because there’s nothing in the excepting block.
this right here is why mate, try and catch is not a solution, just because you put try and catch in your code does not mean its gonna build. try means test this and see its ok and catch means it didn’t work its not ok and show me why.
OK, after watching this video I’ve completed one of my steps.
I’ve got to the point I was trying to get to yesterday which means I can now scan a food item and get the barcode in the REST API and pull down all the json.
Next is getting just the ingredients out of the json.
So now that I’m pulling the JSON from the API and into the app, I’ve learned that what I want to do to the JSON is called deserialize it.
Some stuff I’ve been reading/watching: