Android Open Source - AndroIUT Schooling Data Service Call






From Project

Back to project page AndroIUT.

License

The source code is released under:

GNU General Public License

If you think the Android project AndroIUT listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.iutdijon.androiut2.schooling.service;
//from  w  w w.  j a  va  2 s .  c  o m
import java.io.IOException;
import java.io.InputStream;
import java.security.InvalidParameterException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map.Entry;
import java.util.Set;

import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.message.BasicNameValuePair;

import android.app.Activity;
import android.app.ListActivity;
import android.text.Html;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

import com.iutdijon.androiut2.R;
import com.iutdijon.androiut2.schooling.adapters.SectionedAdapter;
import com.iutdijon.androiut2.schooling.data.Mark;
import com.iutdijon.androiut2.schooling.data.SchoolingData;
import com.iutdijon.androiut2.util.adapters.AbsencesAdapter;
import com.iutdijon.androiut2.util.adapters.MarksAdapter;
import com.iutdijon.androiut2.util.loaders.AndroIUTHTTPConnection;
import com.iutdijon.androiut2.util.loaders.GetAsyncServiceCall;

/**
 * Classe permettant de d?charger toutes les requ?tes r?seaux sur les serveurs de l'IUT de l'IHM
 * @author Morgan Funtowicz
 *
 */
public class SchoolingDataServiceCall extends GetAsyncServiceCall<Void, Void, HashMap<String, ArrayList<SchoolingData>>> {

  public static final String FUNCTION_GET_MARKS = "getmarks";
  public static final String FUNCTION_GET_ABSENCES = "getabsences";
  
  private final String studentNum;
  private final String function;
  private final SectionedAdapter adapter;
  
  public SchoolingDataServiceCall(final ListActivity context, String id, String function) {
    super(context);

    adapter = new SectionedAdapter() {
      @Override
      protected View getHeaderView(String caption, int index, View convertView, ViewGroup parent) {
        TextView result = (TextView) convertView;
        
        if (convertView == null) {
          result = (TextView) context.getLayoutInflater().inflate(R.layout.header, null);
        }
//        Adapter adapter =  (Adapter) context.getListAdapter().getItem(index);
//        int average = 0;
        //+ " ("+String.valueOf(average)+")"
        result.setText(caption);
        
        return (result);
      }
    };
    
    if(!function.equals(FUNCTION_GET_ABSENCES) && !function.equals(FUNCTION_GET_MARKS)){
      throw new InvalidParameterException("The parameter function must be FUNCTION_GET_MARKS ou FUNCTION_GET_ABSENCES");
    }
    
    this.function = function;
    this.studentNum = id;
  }

  @Override
  protected HashMap<String, ArrayList<SchoolingData>> run(Void... params) {  
    HashMap schoolingData  = null;
    
    HttpPost post_request = new HttpPost(AndroIUTConstantes.WEBAPI_URL);
    
    ArrayList<NameValuePair> post_params = new ArrayList<NameValuePair>();
    post_params.add(new BasicNameValuePair("function", function));
    post_params.add(new BasicNameValuePair("id", studentNum));
    

    InputStream stream = null;
    try {
      post_request.setEntity(new UrlEncodedFormEntity(post_params));
      HttpClient client = AndroIUTHTTPConnection.getHTTPClient();
      HttpResponse response = client.execute(post_request);
      
      stream = response.getEntity().getContent();
      
      if(function == FUNCTION_GET_MARKS){
        schoolingData = new MarksAdapter().parse(stream);
      }else {
        schoolingData = new AbsencesAdapter().parse(stream);
      }
    } catch (Exception e){
      return null;
    }finally{
      try {
        stream.close();
      } catch (IOException e) {
        e.printStackTrace();
      }
    }
    return schoolingData;
  }
  @Override
  protected void onPostRun(HashMap<String, ArrayList<SchoolingData>> result) {
    super.onPostRun(result);
    if(getContext() instanceof ListActivity && result != null){
      Set<Entry<String, ArrayList<SchoolingData>>> entries = result.entrySet();
      
      for (Iterator<Entry<String, ArrayList<SchoolingData>>> iterator = entries.iterator(); iterator.hasNext();) {
        final Entry<String, ArrayList<SchoolingData>> entry = iterator.next();
        
        ArrayAdapter<SchoolingData> aAdapter = new ArrayAdapter<SchoolingData>(getContext(), android.R.layout.simple_list_item_1, entry.getValue()){
          @Override
          public View getView(int position, View convertView, ViewGroup parent) {
            View row;

                      if (null == convertView) {
                        row = ((Activity) getContext()).getLayoutInflater().inflate(R.layout.marks_list_item, null);
                      } else {
                        row = convertView;
                      }

                      TextView tv = (TextView) row.findViewById(R.id.schooling_item_title);
                      tv.setText(Html.fromHtml(getItem(position).toString()));
                                            
                      return row;
          }
        };
        
        //Calcul de la moyenne pond?r? [somme(note*coef) / somme coef] pour la mati?re
        float average = 0.0f;
        float coef = 0.0f;
        final ArrayList<SchoolingData> datas = entry.getValue();
        
        for (Iterator<SchoolingData> it = datas.iterator(); it.hasNext();) {
          SchoolingData schoolingData = it.next();
          if(!(schoolingData instanceof Mark)){
            break;
          }else{
            Mark m = (Mark) schoolingData;
            average += Float.parseFloat(m.getMark())*Float.parseFloat(m.getCoef());
            coef += Float.parseFloat(m.getCoef());
          }
          
        }
        if(average != 0.0f){
          average /= coef;
          adapter.addSection(entry.getKey()+" ("+String.format("%.2f", average)+")", aAdapter);
        }else{
          adapter.addSection(entry.getKey(), aAdapter);
        }
      }
      
      ((ListActivity) getContext()).setListAdapter(adapter);
    }
  }

}




Java Source Code List

.ADEResourcesDataTree.java
com.iutdijon.androiut2.ade.activities.ADEActivity.java
com.iutdijon.androiut2.ade.activities.ADEListResourcesChooser.java
com.iutdijon.androiut2.ade.activities.OnImageDownloadedCallback.java
com.iutdijon.androiut2.ade.service.ADEICalendarDownloader.java
com.iutdijon.androiut2.ftp.activities.FTPActivity.java
com.iutdijon.androiut2.ftp.adapters.FTPFileListAdapter.java
com.iutdijon.androiut2.ftp.data.FTPCommandResult.java
com.iutdijon.androiut2.ftp.data.FTPCommandWrapper.java
com.iutdijon.androiut2.ftp.data.FTPCommand.java
com.iutdijon.androiut2.ftp.services.FTPDownloadMonitor.java
com.iutdijon.androiut2.ftp.services.FTPServiceCall.java
com.iutdijon.androiut2.ftp.services.ProgressDownload.java
com.iutdijon.androiut2.global.AndroIUTApplication.java
com.iutdijon.androiut2.iut.activities.HomeActivity.java
com.iutdijon.androiut2.iut.activities.LoginActivity.java
com.iutdijon.androiut2.iut.data.Credential.java
com.iutdijon.androiut2.iut.data.UserFactory.java
com.iutdijon.androiut2.iut.data.account.StudentAccount.java
com.iutdijon.androiut2.iut.data.account.TeacherAccount.java
com.iutdijon.androiut2.iut.data.account.UserAccount.java
com.iutdijon.androiut2.iut.services.IUTLoginServiceCall.java
com.iutdijon.androiut2.mail.activities.MailActivity.java
com.iutdijon.androiut2.mail.activities.MailReaderActivity.java
com.iutdijon.androiut2.mail.adapters.MailDisplayAdapter.java
com.iutdijon.androiut2.mail.adapters.MailsListAdapter.java
com.iutdijon.androiut2.mail.services.EmailUtils.java
com.iutdijon.androiut2.mail.services.MailReader.java
com.iutdijon.androiut2.mail.services.MailService.java
com.iutdijon.androiut2.schooling.activities.SchoolingActivity.java
com.iutdijon.androiut2.schooling.activities.SchoolingTabActivity.java
com.iutdijon.androiut2.schooling.adapters.SectionedAdapter.java
com.iutdijon.androiut2.schooling.data.Absence.java
com.iutdijon.androiut2.schooling.data.Mark.java
com.iutdijon.androiut2.schooling.data.SchoolingData.java
com.iutdijon.androiut2.schooling.service.SchoolingDataServiceCall.java
com.iutdijon.androiut2.util.AndroIUTLogger.java
com.iutdijon.androiut2.util.IOUtils.java
com.iutdijon.androiut2.util.PreferencesManager.java
com.iutdijon.androiut2.util.UIUtil.java
com.iutdijon.androiut2.util.adapters.AbsencesAdapter.java
com.iutdijon.androiut2.util.adapters.BitmapAdapter.java
com.iutdijon.androiut2.util.adapters.CalendarAdapter.java
com.iutdijon.androiut2.util.adapters.IAdapter.java
com.iutdijon.androiut2.util.adapters.MarksAdapter.java
com.iutdijon.androiut2.util.adapters.UserAdapter.java
com.iutdijon.androiut2.util.adapters.XmlAdapter.java
com.iutdijon.androiut2.util.bridge.BridgeFinder.java
com.iutdijon.androiut2.util.loaders.AndroIUTHTTPConnection.java
com.iutdijon.androiut2.util.loaders.AsyncServiceCall.java
com.iutdijon.androiut2.util.loaders.BitmapDownloaderTask.java
com.iutdijon.androiut2.util.loaders.GetAsyncServiceCall.java
com.iutdijon.androiut2.util.loaders.ImageDownloader.java
com.iutdijon.androiut2.util.loaders.PostAsyncServiceCall.java
com.iutdijon.androiut2.util.observers.Observable.java
com.iutdijon.androiut2.util.observers.Observer.java
com.iutdijon.androiut2.util.security.SecureCenter.java
com.iutdijon.androiut2.util.view.ImageZoomView.java
com.iutdijon.androiut2.util.zoom.AspectQuotient.java
com.iutdijon.androiut2.util.zoom.DynamicZoomControl.java
com.iutdijon.androiut2.util.zoom.LongPressZoomListener.java
com.iutdijon.androiut2.util.zoom.PinchZoomListener.java
com.iutdijon.androiut2.util.zoom.SimpleZoomListener.java
com.iutdijon.androiut2.util.zoom.ZoomState.java
com.iutdijon.androiut2.util.zoom.util.Dynamics.java
com.iutdijon.androiut2.util.zoom.util.SpringDynamics.java