sean_2013
sean_2013

Reputation: 3

passing json data to a custom object - android

I'm having trouble with my application. I'm trying to pass json data from my database in a custom class in android, and then display this in a list. When i run my app nothing happens, no errors, no list displayed. if anyone can help i would be very grateful!! :)

All the network stuff is done in async, and im trying to return the an array of objects so i suspect that this could be the problem is here or else when i am converting the string from the httphandler class into a JSONArray.

this is my main activity

package com.example.test1;



import java.util.ArrayList; 
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.app.Activity;
import android.net.ParseException;
import android.os.AsyncTask;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class clubpage extends Activity {

class Programme {

    public String name;
    public String event;
    public String price;

}

String clubphp = "http://10.0.2.2/corkgaa/Nemo.php";
String progString;
ArrayList<Programme> Programmedata = new ArrayList<Programme>();
ListView clublistview = (ListView)findViewById(R.id.listview1);

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.clubpage);

    new Dbhandler().execute(clubphp);   

    ArrayAdapter<Programme> adapter = new ArrayAdapter<Programme>(this,
            android.R.layout.simple_list_item_1, Programmedata);

    clublistview.setAdapter(adapter);


}   

public class Dbhandler extends AsyncTask<String, Void, ArrayList<Programme>> {

    protected ArrayList<Programme> doInBackground(String... arg0) {

ArrayList<Programme> arraydata = new ArrayList<Programme>();        
progString = httphandler.HttpGetExec(clubphp);

try{ 

    JSONArray jArray = new JSONArray(progString);
    JSONObject json_data=null; 

    for(int i=0;i<jArray.length();i++){ 

        json_data = jArray.getJSONObject(i); 
        Programme Progresult = new Programme();

        Progresult.name = json_data.getString("Name");
        Progresult.event = json_data.getString("Event");
        Progresult.price = json_data.getString("Price");

        arraydata.add(Progresult);

        }
    }
    catch(JSONException e1){ 

        }
    catch (ParseException e1) { 
        e1.printStackTrace();
        }
return arraydata;
    }

    @Override
    protected void onPostExecute(ArrayList<Programme> result) {

    Programmedata = result;
    }

}




@Override
protected void onPause() {
    // TODO Auto-generated method stub
    super.onPause();
}

}

httphandler class here:

package com.example.test1;

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 android.util.Log;

public class httphandler {

//Main Dev setup

public static String HttpGetExec (String URI) {


    // TODO Auto-generated method stub
                String result = "no response"; 
                InputStream is = null; 
                StringBuilder sb = null;


                //http post 
                try{ 
                    HttpClient httpclient = new DefaultHttpClient(); 
                    HttpPost httppost = new HttpPost("http://10.0.2.2/corkgaa/Nemo.php"); 
                    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); 
                    sb = new StringBuilder(); 
                    sb.append(reader.readLine() + "\n"); 
                    String line="0"; 
                    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()); 
                    }

                return result;

                //aa=new ArrayAdapter<String>(clubpage.this, R.layout.listrow, R.id.title, result);
                //ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,R.layout.listrow, R.id.title, result);
                //listview.setAdapter(aa);

}

}

Upvotes: 0

Views: 700

Answers (1)

sanna
sanna

Reputation: 1165

Move this line to your onCreate after setContentView

clublistview = (ListView)findViewById(R.id.listview1);

And move the following lines to onPostExecute in your async task:

ArrayAdapter<Programme> adapter = new ArrayAdapter<Programme>(this,
            android.R.layout.simple_list_item_1, Programmedata);

clublistview.setAdapter(adapter);

While your async task is executing, consider showing some kind of progress indicator.

Upvotes: 1

Related Questions