I'm still pretty new to Android development, am doing an app/project which involves loading this JSON (listed below) into a ListView with the name, description, and image, but it has been coming up blank on the emulator. Please tell me what I'm doing wrong - any help would be greatly appreciated! (The more specific the better as I am such a newbie..)
JSON:
{
"success":true,
"games":[
{
"id":"1",
"name":"Name of the Game",
"description":"This is what it is about",
"image":"IMAGE URL"
}
]
}
AndroidJSONParsingActivity:
// URL to make request
private static final String url = "URL of API";
// JSON Node names
private static final String TAG_GAMES = "games";
private static final String TAG_ID = "id";
private static final String TAG_NAME = "name";
private static final String TAG_DESCRIPTION = "description";
private static final String TAG_IMAGE = "image";
// games JSONArray
JSONArray games = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// Hash map for ListView
ArrayList<HashMap<String, String>> gameList = new ArrayList<HashMap<String, String>>();
// Creating JSON Parser instance
JSONParser jParser = new JSONParser();
// getting JSON string from URL
JSONObject json = jParser.getJSONFromUrl(url);
try {
// Getting Array of Games
games = json.getJSONArray(TAG_GAMES);
// looping through All Games
for(int i = 0; i < games.length(); i++){
JSONObject c = games.getJSONObject(i);
// Storing each JSON item in variable
String id = c.getString(TAG_ID);
String image = c.getString(TAG_IMAGE);
// is again JSON Object
JSONObject author = c.getJSONObject(TAG_DESCRIPTION);
String name = author.getString(TAG_NAME);
// creating new HashMap
HashMap<String, String> map = new HashMap<String, String>();
// adding each child node to HashMap key => value
map.put(TAG_ID, id);
map.put(TAG_NAME, name);
map.put( TAG_IMAGE, image);
// adding HashList to ArrayList
gameList.add(map);
}
} catch (JSONException e) {
e.printStackTrace();
}
/**
* Updating parsed JSON data into ListView
* */
ListAdapter adapter = new SimpleAdapter(this, gameList,
R.layout.list_item,
new String[] { TAG_NAME, TAG_DESCRIPTION }, new int[] {
R.id.name, R.id.description });
setListAdapter(adapter);
// selecting single ListView item
ListView lv = getListView();
// Launching new screen on Selecting Single ListItem
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// getting values from selected ListItem
String name = ((TextView) view.findViewById(R.id.name)).getText().toString();
String description = ((TextView) view.findViewById(R.id.description)).getText().toString();
// Starting new intent
Intent in = new Intent(getApplicationContext(), SingleMenuItemActivity.class);
in.putExtra(TAG_NAME, name);
in.putExtra(TAG_DESCRIPTION, description);
startActivity(in);
}
});
}
JSON Parser:
public class JSONParser {
static InputStream is = null;
static JSONObject jObj = null;
static String json = "";
// constructor
public JSONParser() {
}
public JSONObject getJSONFromUrl(String url) {
// Making HTTP request
try {
// defaultHttpClient
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
is = httpEntity.getContent();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(
is, "iso-8859-1"), 8);
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());
}
// try parse the string to a JSON object
try {
jObj = new JSONObject(json);
} catch (JSONException e) {
Log.e("JSON Parser", "Error parsing data " + e.toString());
}
// return JSON String
return jObj;
}
}
main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<!-- Main ListView
Always give id value as list(@android:id/list)
-->
<ListView
android:id="@android:id/list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
list_item.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<!-- Name Label -->
<TextView
android:id="@+id/name"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textColor="#43bd00"
android:textSize="16sp"
android:textStyle="bold"
android:paddingTop="6dip"
android:paddingBottom="2dip" />
<TextView
android:id="@+id/description"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textColor="#acacac"
android:paddingBottom="2dip" />
</LinearLayout>
Adding LogCat info:
01-03 06:16:15.254: E/ActivityThread(616): Service com.android.exchange.ExchangeService has leaked ServiceConnection com.android.emailcommon.service.ServiceProxy$ProxyConnection@40d47b40 that was originally bound here
01-03 06:16:15.254: E/ActivityThread(616): android.app.ServiceConnectionLeaked: Service com.android.exchange.ExchangeService has leaked ServiceConnection com.android.emailcommon.service.ServiceProxy$ProxyConnection@40d47b40 that was originally bound here
01-03 06:16:15.254: E/ActivityThread(616): at android.app.LoadedApk$ServiceDispatcher.<init>(LoadedApk.java:969)
01-03 06:16:15.254: E/ActivityThread(616): at android.app.LoadedApk.getServiceDispatcher(LoadedApk.java:863)
01-03 06:16:15.254: E/ActivityThread(616): at android.app.ContextImpl.bindService(ContextImpl.java:1418)
01-03 06:16:15.254: E/ActivityThread(616): at android.app.ContextImpl.bindService(ContextImpl.java:1407)
01-03 06:16:15.254: E/ActivityThread(616): at android.content.ContextWrapper.bindService(ContextWrapper.java:473)
01-03 06:16:15.254: E/ActivityThread(616): at com.android.emailcommon.service.ServiceProxy.setTask(ServiceProxy.java:157)
01-03 06:16:15.254: E/ActivityThread(616): at com.android.emailcommon.service.ServiceProxy.setTask(ServiceProxy.java:145)
01-03 06:16:15.254: E/ActivityThread(616): at com.android.emailcommon.service.ServiceProxy.test(ServiceProxy.java:191)
01-03 06:16:15.254: E/ActivityThread(616): at com.android.exchange.ExchangeService$7.run(ExchangeService.java:1850)
01-03 06:16:15.254: E/ActivityThread(616): at com.android.emailcommon.utility.Utility$2.doInBackground(Utility.java:551)
01-03 06:16:15.254: E/ActivityThread(616): at com.android.emailcommon.utility.Utility$2.doInBackground(Utility.java:549)
01-03 06:16:15.254: E/ActivityThread(616): at android.os.AsyncTask$2.call(AsyncTask.java:287)
01-03 06:16:15.254: E/ActivityThread(616): at java.util.concurrent.FutureTask.run(FutureTask.java:234)
01-03 06:16:15.254: E/ActivityThread(616): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
01-03 06:16:15.254: E/ActivityThread(616): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
01-03 06:16:15.254: E/ActivityThread(616): at java.lang.Thread.run(Thread.java:856)
01-03 06:16:15.365: E/StrictMode(616): null
01-03 06:16:15.365: E/StrictMode(616): android.app.ServiceConnectionLeaked: Service com.android.exchange.ExchangeService has leaked ServiceConnection com.android.emailcommon.service.ServiceProxy$ProxyConnection@40d47b40 that was originally bound here
01-03 06:16:15.365: E/StrictMode(616): at android.app.LoadedApk$ServiceDispatcher.<init>(LoadedApk.java:969)
01-03 06:16:15.365: E/StrictMode(616): at android.app.LoadedApk.getServiceDispatcher(LoadedApk.java:863)
01-03 06:16:15.365: E/StrictMode(616): at android.app.ContextImpl.bindService(ContextImpl.java:1418)
01-03 06:16:15.365: E/StrictMode(616): at android.app.ContextImpl.bindService(ContextImpl.java:1407)
01-03 06:16:15.365: E/StrictMode(616): at android.content.ContextWrapper.bindService(ContextWrapper.java:473)
01-03 06:16:15.365: E/StrictMode(616): at com.android.emailcommon.service.ServiceProxy.setTask(ServiceProxy.java:157)
01-03 06:16:15.365: E/StrictMode(616): at com.android.emailcommon.service.ServiceProxy.setTask(ServiceProxy.java:145)
01-03 06:16:15.365: E/StrictMode(616): at com.android.emailcommon.service.ServiceProxy.test(ServiceProxy.java:191)
01-03 06:16:15.365: E/StrictMode(616): at com.android.exchange.ExchangeService$7.run(ExchangeService.java:1850)
01-03 06:16:15.365: E/StrictMode(616): at com.android.emailcommon.utility.Utility$2.doInBackground(Utility.java:551)
01-03 06:16:15.365: E/StrictMode(616): at com.android.emailcommon.utility.Utility$2.doInBackground(Utility.java:549)
01-03 06:16:15.365: E/StrictMode(616): at android.os.AsyncTask$2.call(AsyncTask.java:287)
gameList. if only images is not appearing then you can not directly set web url to ImangeView. for setting weburl to imageview you will need to first download it then use it as imageview src .