I am working on an android app. And it has to read in some feed from the internet based on json. Well i've figured that part out by now. But when I test my App, the app gets stuck on the part where it's downloading the feed. The UI get's stuck for a couple of seconds.
I've figurred out already that I need to make use of the AsyncTask class in android to run the connection on the background. I've read so many on this topic, that I can almost dream the theory. Now to put it in practice, it gives me a little bit of a problem.
The app has a bunch of classes now, but the class(activity) that handles the downloading of the feed and puts the retreived data into a listView. the class is called KVONieuws ( Dutch for KVO - NEWS ) here's the source:
package com.appsoweb.kvodeventer;
import java.util.ArrayList;
import java.util.HashMap;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import com.appsoweb.kvodeventer.JSONfunctions;
import com.appsoweb.kvodeventer.KVONieuws;
import com.appsoweb.kvodeventer.R;
import android.app.ListActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;
public class KVONieuws extends ListActivity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.listplaceholder);
ArrayList<HashMap<String, String>> mylist = new ArrayList<HashMap<String, String>>();
JSONObject json = JSONfunctions.getJSONfromURL("http://crossalertdeventer.nl/api/news.json");
try{
JSONArray earthquakes = json.getJSONArray("items");
for(int i=0;i<earthquakes.length();i++){
HashMap<String, String> map = new HashMap<String, String>();
JSONObject e = earthquakes.getJSONObject(i);
map.put("id", String.valueOf(i));
map.put("name", "Titel:" + e.getString("title"));
map.put("image", "Image: " + e.getString("image"));
mylist.add(map);
}
}catch(JSONException e) {
Log.e("log_tag", "Parsing error "+e.toString());
}
ListAdapter adapter = new SimpleAdapter(this, mylist , R.layout.singlelistitem,
new String[] { "name", "image" },
new int[] { R.id.item_title, R.id.item_subtitle });
setListAdapter(adapter);
final ListView lv = getListView();
lv.setTextFilterEnabled(true);
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
@SuppressWarnings("unchecked")
HashMap<String, String> o = (HashMap<String, String>) lv.getItemAtPosition(position);
Toast.makeText(KVONieuws.this, "ID '" + o.get("id") + "' was clicked.", Toast.LENGTH_SHORT).show();
}
});
}
}
Now I have created another class Called JSONfunctions.java here that handles the JSON part of the story:
package com.appsoweb.kvodeventer;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONException;
import org.json.JSONObject;
import android.util.Log;
public class JSONfunctions {
public static JSONObject getJSONfromURL(String url){
InputStream is = null;
String result = "";
JSONObject jArray = null;
//http post
try{
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(url);
httppost.setHeader("User-Agent", "9fb01091b51527555d1d3fc87709918f");
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
is = entity.getContent();
}catch(Exception e){
Log.e("log_tag", "Error in http connection "+e.toString());
}
//convert response to string
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();
result=sb.toString();
}catch(Exception e){
Log.e("log_tag", "Error converting result "+e.toString());
}
try{
jArray = new JSONObject(result);
}catch(JSONException e){
Log.e("log_tag", "Error parsing data "+e.toString());
}
return jArray;
}
}
Now i've tried so much with this code to make the (heavy part) run as a thread on the background, and I understand the theory of it, I just can't get it to work. And I don't know WHY....
Is there anybody out there that can maybe simply adjust my code or give pointers to where to implement an inner class maybe to extend the AsyncTask class, and to give an idea about where to put what code in the run in background method and the onpostexecute...
Thanks to anyone that will help me!