2015-07-01 109 views
0

即時通訊新和我製作的Android應用程序,這將顯示有關在激戰2遊戲項目的信息。該數據在這裏: https://api.guildwars2.com/v2/items 我需要閱讀這些項目(名稱,priece,圖標)顯示他們像列表視圖的整個列表。 問題是我需要修改網址到特定項目,例如: https://api.guildwars2.com/v2/items/256 是否可以訪問所有項目一次? 我的代碼:Android JSON數組通過URL

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

你可以此API之前,它的文檔中使用批量擴張。 API documentation

但是有一個問題,你不能使用類似https://api.guildwars2.com/v2/colors?ids=all這樣的項目。但是你可以通過ID列表獲得項目。這是10個項目,例如:https://api.guildwars2.com/v2/items?ids=1,2,3,4,5,6,7,8,9,10

所以,我覺得你可以寫你的代碼,這種方式(其中「n」是一個數字的所有項目):

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

這是一個有點棘手,但它可以作爲這個API的解決方案。