Home » Java » java – How do I get two pieces of data back from a string using try catch?-Exceptionshub

java – How do I get two pieces of data back from a string using try catch?-Exceptionshub

Posted by: admin February 25, 2020 Leave a comment

Questions:

So I am pretty new to coding and I am trying to make a scouting app for my robotics team. I am using API from The Blue Alliance, and right now I am trying to get only one piece of data back from a certain api at a time.

I have gotten it so that I get back one piece (the city) when pressing a button but now I want to add a second button to get back something else. How would I go about this?

Once again I am very new to this.

Thank you in advanced
This is the One that does work. I have to comment the second return out because it won’t work

    import com.thebluealliance.api.v3.models.Team;

import java.io.IOException;

public class TBAAsyncGetter extends AsyncTask<TBA, Void, String> {

    @Override
    protected String doInBackground(TBA... tbas) {

        try {
            Event[] ourEvents = tbas[0].teamRequest.getEvents(5496);
            Event event = ourEvents[0];

            return ourEvents[0].event_code;
            //return ourEvents[0].event_code;


        } catch (IOException e) {
            e.printStackTrace();
            return  null;
        }


    }


}

these are the Buttons and such. (ignore all the comments)

package com.example.scoutingapp4;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

import com.google.gson.Gson;
import com.thebluealliance.api.v3.TBA;

import java.util.concurrent.ExecutionException;

public class OurTeamFragment extends Fragment implements View.OnClickListener {
    Gson gson = new Gson();
    String authKey = "tatIjlJ60oDWTv9aDk1ZDTL5GM1IJyafzPeJpWk4dR5adIgsdJt3sXvimEHA3MrI";
    TBA tba = new TBA(authKey);

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        super.onCreateView(inflater, container, savedInstanceState);

        return inflater.inflate(R.layout.fragment_ourteam, container, false);
    }


    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Button btnGet = (Button) getView().findViewById(R.id.btnRegional);
        assert btnGet != null;
        btnGet.setOnClickListener(this);

        Button btnGet2 = (Button) getView().findViewById(R.id.btnEventCode);
        assert btnGet2 != null;
        btnGet2.setOnClickListener(this);




    }


    public void onClick(final View v) {
        v.setEnabled(false);
        switch (v.getId()) {

            case R.id.btnRegional:

                try {

                    TextView textHeyo = (TextView) getView().findViewById(R.id.text_regional);
                    textHeyo.setText(new TBAAsyncGetter().execute(tba).get());

                } catch (  ExecutionException | InterruptedException e) {
                    e.printStackTrace();
                }
//                AsyncHttpClient client = new AsyncHttpClient();
//                client.addHeader("X-TBA-Auth-Key", "tatIjlJ60oDWTv9aDk1ZDTL5GM1IJyafzPeJpWk4dR5adIgsdJt3sXvimEHA3MrI");
//                client.get("https://www.thebluealliance.com/api/v3/team/frc5496/events/2019", new AsyncHttpResponseHandler() {
//                    @Override
//                    public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) {
//                        if (responseBody != null) {
//                            TextView textHeyo = (TextView) getView().findViewById(R.id.text_view_result);
//                            assert textHeyo != null;
//                            gson.toJson(new String(responseBody));
//
//                            System.out.println("body=" + new String(responseBody));
//                            textHeyo.setText(new String(responseBody));
//
//                        }
//                        v.setEnabled(true);
//                    }
//
//                    @Override
//                    public void onFailure(int statusCode, Header[] headers, byte[] responseBody, Throwable error) {
//
//                        TextView textHeyo = (TextView) getView().findViewById(R.id.text_view_result);
//                        textHeyo.setText(new String("Boom! ") + new String(responseBody));
//
//                        v.setEnabled(true);
//                    }
//
//                });
                break;

            case R.id.btnEventCode:
                try {

                    TextView textHeyo = (TextView) getView().findViewById(R.id.text_event_code);
                    textHeyo.setText(new TBAAsyncGetter().execute(tba).get());

                } catch (  ExecutionException | InterruptedException e) {
                    e.printStackTrace();
                }
//                AsyncHttpClient client2 = new AsyncHttpClient();
//                client2.addHeader("X-TBA-Auth-Key", "tatIjlJ60oDWTv9aDk1ZDTL5GM1IJyafzPeJpWk4dR5adIgsdJt3sXvimEHA3MrI");
//                client2.get("https://www.thebluealliance.com/api/v3/event/2020cadm/teams", new AsyncHttpResponseHandler() {
//                    @Override
//                    public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) {
//                        if (responseBody != null) {
//                            TextView textHeyo = (TextView) getView().findViewById(R.id.text_view_result);
//                            assert textHeyo != null;
//                            System.out.println("body=" + new String(responseBody));
//                            textHeyo.setText(new String(responseBody));
//                        }
//                        v.setEnabled(true);
//                    }
//
//                    @Override
//                    public void onFailure(int statusCode, Header[] headers, byte[] responseBody, Throwable error) {
//
//                        TextView textHeyo = (TextView) getView().findViewById(R.id.text_view_result);
//                        textHeyo.setText(new String("Boom! ") + new String(responseBody));
//
//                        v.setEnabled(true);
//                    }
//
//                });
                break;
        }
    }
}

How to&Answers:

It looks like with the way you have it setup you could change it to either accept an additional parameter on the TBAAsyncGetter class’s doInBackground method where you can also pass in something to specify which value you want back. Another way as mentioned is to change the return type from String to an object of your creation that holds the info, or even just returning the whole Event array info and picking out what you need in your OurTeamFragment class.

Another way would be to have two separate doInBackground methods that you call, one for each set of data you are trying to get back and you just call the one you need at each time. For instance, this could be doInBackgroundGetCity and doInBackgroundGetOther and each is hardcoded to return that specific value. It isn’t the cleanest solution but these may give you a nudge in the right direction for something to help yo solve this.