我遇到了Android应用程序的问题。我要做的是从JSON API解析数据,然后使用ListActivity将收集的数据放入多行列表中。但是,我似乎无法让它工作。我已经尝试过Googeling的解决方案,但我找不到任何人使用与我相同的方法。
任何可以向我解释如何让它发挥作用的人?
PubsActivity.java:
public class PubsActivity extends ListActivity implements OnItemClickListener, MyCallbackInterface{
private static String url = "http://api.fatkoll.se/json/1.0/getPlaces.json?api_key=fd6950b1499b71037ec3c5a5e01081d6&city_id=";
static String TAG_LIST = "list";
static String TAG_ID = "id";
static String TAG_NAME = "name";
static String TAG_ADDRESS = "address";
static String TAG_OPEN_HOURS = "openhours";
static String TAG_LAT = "lat";
static String TAG_LNG = "lng";
private static String address;
private static String openhours;
private static String lat;
private static String lng;
private ArrayList<String> info = new ArrayList<String>();
int[] toId = new int[] {R.id.listName ,R.id.listAddress};
private ArrayList<String> pubIds = new ArrayList<String>();
JSONArray list = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_pubs);
Intent getCityId = getIntent();
String cityId = getCityId.getExtras().getString("cityId");
JSONparser parser = new JSONparser(this);
parser.execute(url);
}
@Override
public void onRequestComplete(JSONObject result) {
JSONObject json = result;
try {
list = json.getJSONArray(TAG_LIST);
Log.e("onRequestComplete", "Fått JSONArray");
for (int i = 0; i < list.length(); i++){
JSONObject c = list.getJSONObject(i);
Log.e("onRequestComplete", list.getString(i));
pubIds.add(c.getString(TAG_ID));
info.add(c.getString(TAG_NAME));
info.add(c.getString(TAG_ADDRESS));
openhours = c.getString(TAG_OPEN_HOURS);
lat = c.getString(TAG_LAT);
lng = c.getString(TAG_LNG);
}
} catch (JSONException e) {
e.printStackTrace();
}
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, info);
Log.e("onRequestComplete", "Skapar ArrayAdapter");
setListAdapter(adapter);
Log.e("onRequestComplete", "Sätter ListAdapter");
ListView lv = getListView();
lv.setOnItemClickListener(this);
JSONparser.java:
package se.mima.jeda.frsa.krogrunda;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONException;
import org.json.JSONObject;
import android.os.AsyncTask;
import android.util.Log;
public class JSONparser extends AsyncTask<String, Void, JSONObject> {
// Initializing variables
static InputStream is = null;
static JSONObject jObj = null;
static String json = "";
public interface MyCallbackInterface {
public void onRequestComplete(JSONObject result);
}
private MyCallbackInterface mCallback;
public JSONparser(MyCallbackInterface callback) {
mCallback = callback;
}
public JSONObject getJSONFromUrl(String url) {
// Make a HTTP request
try {
// Creating a DefaultHttpClient
DefaultHttpClient httpClient = new DefaultHttpClient();
// skapar en HttpPost
HttpPost httpPost = new HttpPost(url);
HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
is = httpEntity.getContent();
// Catching exceptions
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
// BufferedReader reads the data gathered from the API
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(
is, "UTF-8"), 8000);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
is.close();
json = sb.toString();
} catch (Exception e) {
Log.e("Buffer Error", "Error converting result " + e.toString());
}
// Trying to parse a JSONObject from a String
try {
jObj = new JSONObject(json);
} catch (JSONException e) {
Log.e("JSON Parser", "Error parsing data " + e.toString());
}
// Return JSONObject
return jObj;
}
@Override
protected JSONObject doInBackground(String... params) {
String url = params[0];
return getJSONFromUrl(url);
}
@Override
protected void onPostExecute(JSONObject result) {
mCallback.onRequestComplete(result);
}
}
答案 0 :(得分:0)
我认为您应该使用自己的BaseAdapter(请参阅此处的Google示例List 14)获取您的数据。目前,您在列表中交替显示每个列表条目的“名称”和“地址”,因为您将此数据存储到一个ArrayList中。使用BaseAdapter,您可以将自己的自定义数据对象放入列表适配器,并且可以自由地将其显示在列表中。