开发者

Android: JSON and ListView's quickest refresh options?

I have the following class:

public class RandomDrunkQuotes extends Activity {
/** Called when the activity is first created. */

   TextView txt;

@Override
public void onCreate(Bundle savedInstanceState) {
    Log.i("uDrew Debug", "Made it into onCreate");

    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    Log.i("uDrew Debug", "main Layout Loaded");
    //Add AdMob viewer
    AdView adViewer = (AdView)this.findViewById(R.id.adViewer);
    adViewer.loadAd(new AdRequest());

    Log.i("uDrew Debug", "Calling getServerData");
    //Get Server Data and handle
    getServerData();
}
    public static final String KEY_121 = "http://www.url.com/android.php"; //i use my real ip here


    private void getServerData() {

        //Declare variables
        InputStream is = null;
        String result = "";
        String strQuote = "";
        String strID = "";
        String strFName = "";
        String strLInitial = "";
        String strCity = "";
        String strState = "";
        String strDate = "";
        Integer intLikes = 0;
        Integer intHates = 0;
        String strFNameSaid = "";
        String strLInitialSaid = "";
        Integer intBuzz = 0;
        String strBuzzed = "";
        // prepare the list of all records
        List<HashMap<String, String>> fillMaps = new ArrayList<HashMap<String, String>>();

        Log.i("uDrew Debug", "Declared variables");

        //Declare inflater in order to inflate a layout for each quote
        LinearLayout l = (LinearLayout) findViewById(R.id.myMainLayout);
        LayoutInflater linflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        //http post
        try{
            HttpClient httpclient = new DefaultHttpClient();
            HttpPost httppost = new HttpPost("http://www.url.com/android.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);
            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());
        }
        //parse json data
        try{
            Log.i("uDrew Debug", "Trying to Parse JSON Data");
            //Place JSON data into array one item at a time
            JSONArray jArray = new JSONArray(result);
            //Loop through each record in the database
            //Get ListView
            ListView lv= (ListView)findViewById(R.id.listview);
            // create the grid item mapping
            String[] from = new String[] {"lblQuote", "lblBuzzed", "lblShared", "lblSaid", "lblLikes", "lblHates", "lblLocation", "lblDate"};
            int[] to = new int[] { R.id.lblQuote, R.id.lblBuzzed, R.id.lblShared, R.id.lblSaid, R.id.lblLikes, R.id.lblHates, R.id.lblLocation, R.id.lblDate };

            for(int i=0;i<jArray.length();i++){
                Log.i("uDrew Debug", "Made it into JSONArray Loop");

                //Get this record
                JSONObject json_data = jArray.getJSONObject(i);

                //Put each result into variables for later handling
                strFName = json_data.getString("FName");
                strLInitial = json_data.getString("LInitial");
                strCity = json_data.getString("City");
                strState = json_data.getString("State");
                strDate = json_data.getString("Date");
                strQuote = json_data.getString("Quote");
                intLikes = Integer.parseInt(json_data.getString("Likes"));
                intHates = Integer.parseInt(json_data.getString("Hates"));
                strFNameSaid = json_data.getString("FNameSaid");
                strLInitialSaid = json_data.getString("LInitialSaid");
                intBuzz = Integer.parseInt(json_data.getString("Buzz"));


                Log.i("uDrew Debug", "Made it past JSON Parsing");


                switch(intBuzz){
                    case 1: 
                        strBuzzed = ("One Beer\nSyndrome");
                        break;
                    case 2: 
                        strBuzzed = ("Buzzed");
                        break;
                     case 3: 
                         strBuzzed = ("Drunk");
                        break;
                     case 4: 
                         strBuzzed = ("Trashed");
                        break;
                     case 5: 
                        strBuzzed = "Retarded";
                        break;
                }                       
                HashMap<String, String> map = new HashMap<String, String>();
                map.put("lblQuote", strQuote);
                map.put("lblBuzzed", strBuzzed);
                map.put("lblShared", strFName + " " + strLInitial);
                map.put("lblSaid",strFNameSaid + " " + strLInitialSaid);
                map.put("lblDate", strDate);
                map.put("lblLocation", strCity + ", " + strState);
                map.put("lblLikes", intLikes.toString());
   开发者_StackOverflow社区             map.put("lblHates", intHates.toString());
                fillMaps.add(map);



            }//End For loop
         // fill in the grid_item layout
         SimpleAdapter adapter = new SimpleAdapter(this, fillMaps, R.layout.myviews, from, to);
         lv.setAdapter(adapter);
        }catch(JSONException e){
                Log.e("log_tag", "Error parsing data "+e.toString());
        }
    }

I'd like to know how to refresh the data retrieved from the server in the quickest fashion. Right now I have an Refresh button in the options button that calls getServerData(); but this seems to take a very long time. Any thoughts are welcome.


You should create an AsyncTask and call getServerData() method from its doInBackground() method. This method must return the data from the server. Then in onPostExecute() method you should populate the ListView with this data.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜