First Android App

Hi All,

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.

Here’s a link to stack overflow where I described what I’m trying to do.

1 Like

why don’t you start learning java?

here for example a course:

2 Likes

You want to do quite a lot for not knowing java :slight_smile:

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.

3 Likes

I agree it’s an ambitious project to use to learn java and that’s why I’m breaking it down into small steps.

The next small step is, what would I google to find where the barcode scanner is storing the number?

Looking at this block of code:

   @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 {
            textView.setText(intentResult.getContents());
        }
        super.onActivityResult(requestCode, resultCode, data);
}

Is it int resultCode ? If yes, how would I reference it or any suggestions on what to google?

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.

See this post to find the backtick on your keyboard.
Note: Backticks (`) are not single quotes (’).

1 Like

Ah nice, I was going for that with the block quote, I didn’t realize (obviously, lol) that’s how you format text as code.
Thanks for the help out :smiley:

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.

Example 3 is looking closest to what I’m trying to do, still skimming the examples.

    super.onActivityResult(requestCode, resultCode, data);
    IntentResult result = IntentIntegrator.parseActivityResult(requestCode, resultCode, data);
    if (result != null) {
        //if QRcode has nothing in it
        if (result.getContents() != null) {
            //if QRCode contains data
            //retrieve results
            String resultContents = result.getContents();
            String[] values = resultContents.split(":");
            String userName = values[0];
            String userEmail = values[1];
            addContact(userName, userEmail);
        }
    }
}

Updated the code:

    @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);

    }

And pushed it to the repo

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

so the documentation says this…

URL to read data for a product:

Example: https://world.openfoodfacts.org/api/v0/product/737628064502.json

so you got this part right

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.

1 Like

OK, I’ve up date main and it’s throwing the same error as seen here.

I’ve been messing with the try and catch solution but I can’t get the code to build.

Gonna go try the throw suggestion as best as I understand it and see what happens.

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.

Gonna have to pick this up tomorrow.

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.

1 Like

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.

This was actually helpful, thank you.

This was not helpful.

Yeah, I learned that yesterday.
try/catch/throw never really made a lot of sense to me before.

When I was in high school we only had C++ 89 so I was taught error checking with if statements and always found java confusing.

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:

I can only put two links in a post, lol.

Last night I realized I have been waaayy over thinking this.
I’m already getting all the JSON from the openfoodfacts API as a string.

Finding a word in a string is one of the most basic programming tasks ever.
Getting started on doing this now that I have a minute: