Comments (3)
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.
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.
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)
- How to separate TeamStats for each team?
- Gradle build not updated to V4?
- getLeagueById throws RiotApiException HOT 1
- GetGameDuration in Android Studio HOT 3
- get Summoner ID in champ select HOT 1
- Asking about the api HOT 3
- Endpoints for V4 not updated HOT 2
- Support for ACS
- getDataChampion returning 403 HOT 2
- Summoner is in game
- How do I import this in a Scala IntelliJ Project
- get runes pages by summoner
- NumberFormatException when trying to call getMatch() HOT 1
- 403: Forbidden HOT 2
- TeamFightTactics
- Add Tournament Stub
- 403 Forbidden HOT 2
- getDataChampion returns 403 Forbidden
- Cant get the name of a champion
- TFT API Data
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from riot-api-java.