Giter VIP home page Giter VIP logo

Comments (3)

fKunstner avatar fKunstner commented on September 16, 2024

The problem comes from Android. You are not able to make internet connections in the main thread, that's what AsyncTasks are for.

You'll encounter much more of these, check what LogCat is telling you and google the exception, there's always a stackoverflow Q&A about it.

from riot-api-java.

taycaldwell avatar taycaldwell commented on September 16, 2024

fKunstner is correct. The API call is a network operation, therefore it must be performed off the main UI thread.

Fortunately, Android makes it easy for you through the use of AsyncTasks.

You will need to create a new subclass that extends AsyncTask, and make the API call in the doInBackground method.

Also be sure to include the Internet permission to your app.

I can provide you with example code of what I am talking about when I get home.

Cheers,
Rithms

from riot-api-java.

taycaldwell avatar taycaldwell commented on September 16, 2024

Here is an example of what you might want to do:

public class MainActivity extends Activity{

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        final Button button = (Button) findViewById(R.id.button1);
        button.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                FetchSummonerTask summonerTask = new FetchSummonerTask();
                summonerTask.execute("summoner name");
            }
        });

    }

    public class FetchSummonerTask extends AsyncTask<String, Void, Summoner> {

        @Override
        protected Summoner doInBackground(String... params) {

            RiotApi api = new RiotApi(
                    "API-KEY");
            try {
                api.setRegion(Region.EUW);
                Map<String, Summoner> summoners = api.getSummonerByName(params[0]);
                if(summoners != null) {
                    return summoners.get(params[0].replaceAll("\\s+", "").toLowerCase());
                }
            } catch (RiotApiException e) {
                e.printStackTrace();
            }

            return null;
        }

        @Override
        protected void onPostExecute(Summoner result) {
            super.onPostExecute(result);
            if(result != null) {
                TextView dis = (TextView) findViewById(R.id.textView1);
                dis.setText(String.valueOf((result.getSummonerLevel())));
            }
        }
    }
}

This code hasn't been tested, but it should give you an example of what needs to be done.

Also, you might even want to have global class that contains your RiotApi object, that way, you don't need to create a new one every time you want to make an API call in your app, and can just use one RiotAPI object for your whole Android app.

An example of this would be:

import android.app.Application;
import main.java.riotapi.RiotApi;

public class GlobalClass extends Application {

    private RiotApi api;

    public GlobalClass() {
        this.api = new RiotApi("API-KEY");
    }

    public RiotApi getApi() {
        return api;
    }

}

An example doInBackground method of an AsyncTask, similar to the previous one would be:

 protected Summoner doInBackground(String... params) {

                try {
                    Summoner summoner = ((GlobalClass)getActivity().getApplication()).getApi()
                            .getSummonerByName(params[0])
                            .get(params[0].replaceAll("\\s+", "").toLowerCase());
                    if(summoner != null) {
                        return summoner;
                    }
                } catch (RiotApiException e) {
                    e.printStackTrace();
                }

                return null;
            }

Hope this helps.

from riot-api-java.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.