I've run in to a problem with my Android application. What I'm trying to do is parsing data from a JSON API and then put the gathered data into a multiple line list using ListActivity. However, I cant seem to get it to work. I've tried Googeling for a solution but I cannot find anyone using the same method as me.
Anyone who can explain to me how I can get this to work?
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);
}
}