0

im new and I making Android app which will be display info about items in Guild Wars 2 game. That data is here: https://api.guildwars2.com/v2/items I need read whole list of this items (name, priece, icon) showing them like list view. Problem is that i need modify url to specific item like: https://api.guildwars2.com/v2/items/256 Is possible to acces to all items once? My code :

package com.wingnity.jsonparsingtutorial;

import java.io.IOException;
import java.util.ArrayList;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.net.ParseException;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;

public class MainActivity extends Activity {

ArrayList<Actors> actorsList;

ActorAdapter adapter;

@Override
protected void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    actorsList = new ArrayList<Actors>();
    new JSONAsyncTask().execute("https://api.guildwars2.com/v2/items");

    ListView listview = (ListView)findViewById(R.id.list);
    adapter = new ActorAdapter(getApplicationContext(), R.layout.row, actorsList);

    listview.setAdapter(adapter);

    listview.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> arg0, View arg1, int position,
                long id) {
            // TODO Auto-generated method stub
            Toast.makeText(getApplicationContext(), actorsList.get(position).getName(), Toast.LENGTH_LONG).show();              
        }
    });
}


class JSONAsyncTask extends AsyncTask<String, Void, Boolean>
{

    ProgressDialog dialog;

    @Override
    protected void onPreExecute()
    {
        super.onPreExecute();
        dialog = new ProgressDialog(MainActivity.this);
        dialog.setMessage("Wczytywanie prosze czekac");
        dialog.setTitle("Polaczenie z serwerem");
        dialog.show();
        dialog.setCancelable(false);
    }

    @Override
    protected Boolean doInBackground(String... urls)
    {
        try {

            //------------------>>
            HttpGet httppost = new HttpGet(urls[0]);
            HttpClient httpclient = new DefaultHttpClient();
            HttpResponse response = httpclient.execute(httppost);

            // StatusLine stat = response.getStatusLine();
            int status = response.getStatusLine().getStatusCode();

            if (status == 200)
            {
                HttpEntity entity = response.getEntity();
                String data = EntityUtils.toString(entity);


                JSONObject jsono = new JSONObject(data);
                JSONArray jarray = jsono.getJSONArray("256");

                for (int i = 0; i < jarray.length(); i++)
                {
                    JSONObject object = jarray.getJSONObject(i);

                    Actors actor = new Actors();

                    actor.setName(object.getString("name"));
                    actor.setDescription(object.getString("description"));
                    actor.setDob(object.getString("type"));
                    actor.setCountry(object.getString("level"));
                    actor.setHeight(object.getString("rarity"));
                    actor.setSpouse(object.getString("vendor_value"));
                    actor.setChildren(object.getString("default_skin"));
                    actor.setImage(object.getString("icon"));

                    actorsList.add(actor);
                }
                return true;
            }

            //------------------>>

        } catch (ParseException e1)
        {
            e1.printStackTrace();
        } catch (IOException e)
        {
            e.printStackTrace();
        } catch (JSONException e)
        {
            e.printStackTrace();
        }
        return false;
    }

    protected void onPostExecute(Boolean result)
    {
        dialog.cancel();
        adapter.notifyDataSetChanged();
        if(result == false)
            Toast.makeText(getApplicationContext(), "Unable to fetch data from server", Toast.LENGTH_LONG).show();

    }
}

}

1 Answer 1

1

You could use bulk expansion in this API prior to it's documentation. API documentation

But there are a problem that you can't use smthing like https://api.guildwars2.com/v2/colors?ids=all for items. But you can get items by list of id's. This is for 10 items, for example: https://api.guildwars2.com/v2/items?ids=1,2,3,4,5,6,7,8,9,10

So as I think you can write your code this way(where "n" is a number of all items):

String GET_ITEMS_URL = "https://api.guildwars2.com/v2/items?ids="; 
for(int i = 1; i <= n; i++){
     GET_ITEMS_URL += i + ",";
}

It is a little tricky, but it works as a solution for this API.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.