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();
}
}
}