예제 #1
0
파일: Home.java 프로젝트: csexton/tacosftw
    @Override
    protected ArrayList<com.districttaco.android.Status> doInBackground(URL... params) {
      ArrayList<com.districttaco.android.Status> newStatuses =
          new ArrayList<com.districttaco.android.Status>();
      // make the http call to retrieve the status
      HttpClient client = new DefaultHttpClient();
      HttpGet request;
      try {
        request = new HttpGet(params[0].toURI());
      } catch (URISyntaxException e1) {
        e1.printStackTrace();
        return null;
      }
      try {
        HttpResponse resp = client.execute(request);
        if (resp.getStatusLine().getStatusCode() != 200) return null;
        String body = EntityUtils.toString(resp.getEntity());
        try {
          // extract the contents from the json feed
          JSONObject json = new JSONObject(body);
          SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
          int len = json.getJSONArray("statuses").length();
          for (int i = 0; i < len; i++) {
            // load new values
            JSONObject currentStatus = json.getJSONArray("statuses").getJSONObject(i);
            com.districttaco.android.Status status = new com.districttaco.android.Status();
            try {
              status.setLastUpdate(formatter.parse(currentStatus.getString("updated_at")));
            } catch (ParseException e) {
              status.setLastUpdate(new Date());
              e.printStackTrace();
            }
            JSONObject info = currentStatus.getJSONObject("info");
            status.setInfoTitle(info.getString("title"));
            status.setInfoHeader(info.getString("header"));
            status.setInfoBody(info.getString("body"));
            JSONObject location = currentStatus.getJSONObject("location");
            status.setLocationName(location.getString("name"));
            status.setLocationDescription(location.getString("description"));
            status.setLatitude(location.getDouble("latitude"));
            status.setLongitude(location.getDouble("longitude"));
            status.setStatusText(currentStatus.getString("body"));

            // status object is fully populated, add it to the list
            newStatuses.add(status);
          }
        } catch (JSONException e) {
          e.printStackTrace();
          return null;
        }
      } catch (IOException e) {
        e.printStackTrace();
        return null;
      }
      return newStatuses;
    }