title: Loop AsyncTask to fetch JSON and store as object in same list
body: <p>I want to read and store all <code>JSON</code> values from <a href="http://www.gw2spidy.com/api/v0.9/json/item-search/Mini/0" rel="nofollow">this api Link</a> with <code>get request</code> <em>"Mini"</em> as example (which is actually an user input variable) and the last number is the page your are viewing. Every page can hold a max of 50 results. The <a href="http://www.gw2spidy.com/api/v0.9/xml/item-search/Mini/0" rel="nofollow">same link is also in XML</a> format (I must read and store as JSON, this is for easier understanding)</p>
  
  <p>In this exmaple there are 8 pages with a total of 359 results. I need to loop through all pages and add all the JSON values to the same object list. </p>
  
  <p>I have the code which work to read one page. I do not know how to make it loop through all pages and add to same object list.</p>
  
  <p>In the acitivty.java onCreate I call the AsyncTask.</p>
  
  <pre><code>String userSearchRequest = search_activity_data.getString("userSearchRequest");
  int page = 0;
  String spidy_iTN_url = "http://www.gw2spidy.com/api/v0.9/json/item-search/" + userSearchRequest + "/" + page;
  
  itemsByInput_AsyncTask itemsByInput_AsyncTask = new itemsByInput_AsyncTask();
  itemsByInput_AsyncTask.setItemListToListings(this);
  itemsByInput_AsyncTask.execute(spidy_iTN_url);
  </code></pre>
  
  <p>This is my AsyncTask class called <code>itemsByInput_AsyncTask.java</code></p>
  
  <pre><code>import constructors.itemResults_api_constr;
  import constructors.itemRoot_api_constr;
  
  public class itemsByInput_AsyncTask extends AsyncTask&lt;String, Void, JSONObject&gt; {
  
          JSONObject Jo_result;
          private itemListToListings itemListToListings;
  
       public void setItemListToListings (itemListToListings itemListToListings) {
          this.itemListToListings = itemListToListings;
      }
  
      @Override
      protected JSONObject doInBackground(String... params) {
          return spidyHttpGetRequest(params[0]);
      }
  
      public JSONObject spidyHttpGetRequest(String URL){
          try {
              HttpGet get = new HttpGet(URL);
              HttpClient client = new DefaultHttpClient();
              HttpResponse response = client.execute(get);
              HttpEntity entity = response.getEntity();
              String result = EntityUtils.toString(entity);
              Jo_result = new JSONObject(result);
  
          } catch (ClientProtocolException e) {
              e.printStackTrace();
          } catch (IOException e) {
              e.printStackTrace();
          } catch (JSONException e) {
              e.printStackTrace();
          }
  
          return Jo_result;
      }
  
      @Override
      protected void onPostExecute(JSONObject jsonObject) {
          super.onPostExecute(jsonObject);
          this.itemListToListings.itemListToListings(JoToJO_constructor(jsonObject));
      }
  
  
      public itemRoot_api_constr JoToJO_constructor(JSONObject Jo_result) {
          itemRoot_api_constr spidy_iTN_rootO = new itemRoot_api_constr();
  
          try {
              spidy_iTN_rootO.setCount(Jo_result.getInt("count"));
              spidy_iTN_rootO.setPage(Jo_result.getInt("page"));
              spidy_iTN_rootO.setLast_page(Jo_result.getInt("last_page"));
              spidy_iTN_rootO.setTotal(Jo_result.getInt("total"));
              JSONArray list = new JSONArray(Jo_result.getString("results"));
  
              for (int i = 0; i &lt; spidy_iTN_rootO.getCount(); i++) {
                  JSONObject resultsObject = list.getJSONObject(i);
                  itemResults_api_constr spidy_iTN_resultsO = new itemResults_api_constr();
  
                  spidy_iTN_resultsO.setData_id(resultsObject
                          .getInt("data_id"));
                  spidy_iTN_resultsO.setName(resultsObject
                          .getString("name"));
                  spidy_iTN_resultsO.setRarity(resultsObject
                          .getInt("rarity"));
                  spidy_iTN_resultsO.setRestriction_level(resultsObject
                          .getInt("restriction_level"));
                  spidy_iTN_resultsO.setImg(resultsObject
                          .getString("img"));
                  spidy_iTN_resultsO.setType_id(resultsObject
                          .getInt("type_id"));
                  spidy_iTN_resultsO.setSub_type_id(resultsObject
                          .getInt("sub_type_id"));
                  spidy_iTN_resultsO.setPrice_last_changed(resultsObject
                          .getString("price_last_changed"));
                  spidy_iTN_resultsO.setMax_offer_unit_price(resultsObject
                          .getInt("max_offer_unit_price"));
                  spidy_iTN_resultsO.setMin_sale_unit_price(resultsObject
                          .getInt("min_sale_unit_price"));
                  spidy_iTN_resultsO.setOffer_availability(resultsObject
                          .getInt("offer_availability"));
                  spidy_iTN_resultsO.setSale_availability(resultsObject
                          .getInt("sale_availability"));
                  spidy_iTN_resultsO.setSale_price_change_last_hour(resultsObject
                          .getInt("sale_price_change_last_hour"));
                  spidy_iTN_resultsO.setOffer_price_change_last_hour(resultsObject
                          .getInt("offer_price_change_last_hour"));
                  spidy_iTN_rootO.addObject(spidy_iTN_resultsO);
              }
  
          } catch (JSONException e) {
              e.printStackTrace();
          }
  
          return spidy_iTN_rootO;
      }
  
      public interface itemListToListings {
          public void itemListToListings(itemRoot_api_constr resultClass);
      }
  
  
  }
  </code></pre>
  
  <p>And finally in my activity.java i can use my object in the method <code>itemListToListings()</code>.</p>
  
  <p>How can I make this loop through all pages (<code>last_page</code> property) and add all JSON values as object in the same list.</p>
  
  <p>EDIT: My <code>itemListToListings</code> function in my activity.</p>
  
  <pre><code>public void itemListToListings(final itemRoot_api_constr spidy_iTN_construct) {
              ArrayList&lt;listItemWidgets_constr&gt; image_details = getListData(spidy_iTN_construct);
              final ListView lv1 = (ListView) findViewById(R.id.listView);
              lv1.setAdapter(new itemListAdapter(this, image_details));
              lv1.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                  @Override
                  public void onItemClick(AdapterView&lt;?&gt; a, View v, int position, long id) {
                      //listItemWidgets_constr newsData = (listItemWidgets_constr) lv1.getItemAtPosition(position);
                      Toast.makeText(resultsActivity.this, "Selected :" + spidy_iTN_construct.results(position).name, Toast.LENGTH_LONG).show();
  
                      Intent i = new Intent(resultsActivity.this, listingsActivity.class);
                      i.putExtra("itemId", spidy_iTN_construct.results(position).data_id);
                      startActivity(i);
                  }
              });
          }
  </code></pre>
  
  <p>EDIT 3: error log</p>
  
  <pre><code>05-01 07:17:39.828    3620-3620/com.example.krijn.gw2TP_androidMobile E/AndroidRuntime﹕ FATAL EXCEPTION: main
      Process: com.example.krijn.gw2TP_androidMobile, PID: 3620
      java.lang.NullPointerException: Attempt to invoke interface method 'void com.example.krijn.gw2TP_androidMobile.AsyncTasks.itemsByInput_AsyncTask$itemListToListings.itemListToListings(com.example.krijn.gw2TP_androidMobile.constructors.itemRoot_api_constr)' on a null object reference
              at com.example.krijn.gw2TP_androidMobile.AsyncTasks.itemsByInput_AsyncTask.onProgressUpdate(itemsByInput_AsyncTask.java:88)
              at com.example.krijn.gw2TP_androidMobile.AsyncTasks.itemsByInput_AsyncTask.onProgressUpdate(itemsByInput_AsyncTask.java:27)
  </code></pre>
  
  <p>After I get this error in the Logcat I still see the Log updating with the following in <code>doInBackground</code></p>
  
  <pre><code>for (int n = 1; n &lt; nPage; n++){
              Log.i("gw2Log", "n: " + n);
              publishProgress(JoToJO_constructor(spidyHttpGetRequest(makeUrl(n))));
          }
  </code></pre>
  
  <p>After that is done looping the application crashes.</p>