Diablo3 Heroes-use volley and Gson to process JSON data for Blizzard APIs

Source: Internet
Author: User
Tags blizzard

Using volley and Gson to process JSON

The Diablo 3 API passed to us is a JSON data. Now let's try to parse it. After Baidu a bit, I initially know 2 tools. One is volley. This is used to get JSON data. One is Gson. This is used to parse JSON data.

Objectives of this chapter:

    • Read Blizzard's API data
    • Parse the data

Using volley to get JSON data

Volley supports native strings, images, and Json. Allows us to focus more on the logic of the application. Volley is obtained by using the following method.

$git Clone Https://android.googlesource.com/platform/framework/volley

Referencing volley in Android Studio

The File->import Module then follows the process, and if you need to install anything, Android Studio will automatically prompt you. (Note: VPN required)

Setting dependencies

For example, File->project Structure or Ctrl+alt+shift+s

Click on the app, then click on the rightmost green + sign to select module dependency, then select Volley. Now it's time to start using volley. Alt+enter the code after you've knocked it out. The code will work as expected.

In this application we are now going to a heroic character of the equipment data, we use the following API

http://tw.battle.net/api/d3/profile/kakyban-3879/hero/26135206

This JSON data is probably as follows

Our code in the program is

String URL = "Address of JSON";

Requestqueue queue = Volley.newrequestqueue (Getactivity ());

Stringrequest stringrequest = new Stringrequest (Request.Method.GET, URL,

New Response.listener<string> () {

@Override

public void Onresponse (String response) {

The response here is the JSON data that is returned

}

}, New Response.errorlistener () {

@Override

public void Onerrorresponse (Volleyerror error) {

If executed here, it means that volley is not working properly

}

});

ADD the request to the Requestqueue.

Queue.add (stringrequest);

Debug check, the data is not a problem.

Using Gson to parse JSON strings

Gson The main reason I use it may be that my Android studio appears to be integrated, and I quote it directly. Of course it is necessary to set dependencies, refer to the volley settings.

The JSON code for the sample is:

http://tw.battle.net/api/d3/profile/kakyban-3879/hero/26135206

This is the real data on the http://tw.battle.net, which you can view at any time.

The structure of JSON is like this.

First we create a class hero, which contains fields such as ID, name, class, and note that class is reserved, so I renamed Class1 and added @serializedname ("class") This means that the class field inside the JSON is mapped to Class1. There are also last-updated that contain "-" special symbols I map for lastupdated. A general Class I define as Hero,hero contains the Skills,items,followers,stats,kills,progression class, we first implement only hero and items.

public class hero{

@SerializedName ("Class")

Private String Class1;

Private Boolean dead;

Private followers followers;

Private number gender;

Private Boolean hardcore;

Private number ID;

private items items;

Private kills kills;

@SerializedName ("last-updated")

Private number lastupdated;

Private number level;

private String name;

Private number Paragonlevel;

Private number seasoncreated;

private Boolean seasonal;

Private Skills Skills;

Private Stats Stats;

Public String GetClass1 () {

return this.class1;

}

public void SetClass1 (String Class1) {

This.class1 = Class1;

}

public Boolean getdead () {

return this.dead;

}

public void Setdead (Boolean dead) {

This.dead = dead;

}

Public followers Getfollowers () {

return this.followers;

}

public void Setfollowers (followers followers) {

This.followers = followers;

}

Public number Getgender () {

return this.gender;

}

public void Setgender (number gender) {

This.gender = gender;

}

public Boolean Gethardcore () {

return this.hardcore;

}

public void Sethardcore (Boolean hardcore) {

This.hardcore = hardcore;

}

Public number GetId () {

return this.id;

}

public void SetId (number ID) {

This.id = ID;

}

Public Items GetItems () {

return this.items;

}

public void Setitems (items items) {

This.items = items;

}

Public kills Getkills () {

return this.kills;

}

public void Setkills (kills kills) {

This.kills = kills;

}

Public number getlastupdated () {

return this.lastupdated;

}

public void setlastupdated (number lastupdated) {

this.lastupdated = lastupdated;

}

Public number Getlevel () {

return this.level;

}

public void SetLevel (number level) {

This.level = level;

}

Public String GetName () {

return this.name;

}

public void SetName (String name) {

THIS.name = name;

}

Public number Getparagonlevel () {

return this.paragonlevel;

}

public void Setparagonlevel (number Paragonlevel) {

This.paragonlevel = Paragonlevel;

}

Public number getseasoncreated () {

return this.seasoncreated;

}

public void setseasoncreated (number seasoncreated) {

this.seasoncreated = seasoncreated;

}

public Boolean getseasonal () {

return this.seasonal;

}

public void Setseasonal (Boolean seasonal) {

This.seasonal = seasonal;

}

Public Skills Getskills () {

return this.skills;

}

public void Setskills (Skills Skills) {

This.skills = skills;

}

Public Stats getstats () {

return this.stats;

}

public void Setstats (Stats Stats) {

This.stats = stats;

}

}

Items are long

That's probably the way it is. The code to parse the data is as follows:

Gson Gson = new Gson ();

Hero Mhero = Gson.fromjson (JSON, hero.class);

JSON is a JSON string. If you implement only HERO,ITEMS2 classes, Gson automatically maps the data based on the corresponding field. Look in the debugger as shown, the data is successful.

Note: Items are a large bean within the bean class that must be static.

Summary:

This article simply explains how to read data from Blizzard's API and parse it into classes. All the methods are not the best. For example Gson, when I finished to find Fastjson seems faster. Fastjson is a library designed by Ali's engineers and does not rely on other third-party libraries, which are 6 times times faster than the Gson.

All of this is just to achieve the following effect:

The results of the API links in this article are also the data in this chapter.

Diablo3 Heroes-use volley and Gson to process JSON data for Blizzard APIs

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.