我是新的,我制作Android应用程序,将显示有关激战2游戏中的项目的信息。这些数据在这里: https://api.guildwars2.com/v2/items 我需要阅读这些项目的全部列表(名称,priece,图标),显示它们像列表视图。 问题是我需要修改url到特定项目,如: https://api.guildwars2.com/v2/items/256 可以访问所有项目一次吗? 我的代码:
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();
}
}
}
答案 0 :(得分: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
因此我认为你可以用这种方式编写代码(其中&#34; n&#34;是所有项目的数量):
String GET_ITEMS_URL = "https://api.guildwars2.com/v2/items?ids=";
for(int i = 1; i <= n; i++){
GET_ITEMS_URL += i + ",";
}
这有点棘手,但它可以作为此API的解决方案。