Search in sources :

Example 96 with AsyncTask

use of android.os.AsyncTask in project classify-system by anverliedoit.

the class ClassServiceImpl method updateClassById.

@Override
public Class updateClassById(final long id, final Class newClass, final long teacherId, final long subjectId, final long sectionId) throws ClassException {
    try {
        return new AsyncTask<String, Class, Class>() {

            @Override
            protected Class doInBackground(String... args) {
                try {
                    String link = "".concat(domain).concat("/").concat(baseUri).concat("/").concat(payload).concat("/").concat(String.valueOf(id)).concat("?teacherId=").concat(String.valueOf(teacherId)).concat("&subjectId=").concat(String.valueOf(subjectId)).concat("&sectionId=").concat(String.valueOf(sectionId));
                    Gson gson = new Gson();
                    URL url = new URL(link);
                    HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
                    httpURLConnection.setRequestMethod("PUT");
                    httpURLConnection.setRequestProperty("Content-Type", "application/json");
                    httpURLConnection.setRequestProperty("Accept", "application/json");
                    httpURLConnection.setDoOutput(true);
                    httpURLConnection.setDoInput(true);
                    OutputStream os = httpURLConnection.getOutputStream();
                    BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(os, "UTF-8"));
                    writer.write(gson.toJson(newClass));
                    writer.flush();
                    writer.close();
                    httpURLConnection.connect();
                    if (httpURLConnection.getResponseCode() == 200) {
                        InputStream inputStream = httpURLConnection.getInputStream();
                        String jsonData = "";
                        int data;
                        while ((data = inputStream.read()) != -1) {
                            jsonData += (char) data;
                        }
                        return gson.fromJson(jsonData, Class.class);
                    } else if (httpURLConnection.getResponseCode() == 400) {
                        InputStream inputStream = httpURLConnection.getInputStream();
                        String jsonData = "";
                        int data;
                        while ((data = inputStream.read()) != -1) {
                            jsonData += (char) data;
                        }
                        Message message = gson.fromJson(jsonData, Message.class);
                        Log.i("ServiceTAG", "Service : Class");
                        Log.i("ServiceTAG", "Status : " + message.getStatus());
                        Log.i("ServiceTAG", "Type : " + message.getType());
                        Log.i("ServiceTAG", "Message : " + message.getMessage());
                        return null;
                    } else
                        throw new ClassException("Server Error");
                } catch (ClassException e) {
                    e.printStackTrace();
                    return null;
                } catch (IOException e) {
                    e.printStackTrace();
                    return null;
                }
            }
        }.execute((String) null).get();
    } catch (InterruptedException e) {
        e.printStackTrace();
        return null;
    } catch (ExecutionException e) {
        e.printStackTrace();
        return null;
    }
}
Also used : Message(com.remswork.project.alice.model.support.Message) InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) AsyncTask(android.os.AsyncTask) Gson(com.google.gson.Gson) ClassException(com.remswork.project.alice.exception.ClassException) IOException(java.io.IOException) URL(java.net.URL) BufferedWriter(java.io.BufferedWriter) HttpURLConnection(java.net.HttpURLConnection) OutputStreamWriter(java.io.OutputStreamWriter) ExecutionException(java.util.concurrent.ExecutionException)

Example 97 with AsyncTask

use of android.os.AsyncTask in project classify-system by anverliedoit.

the class ClassServiceImpl method deleteScheduleById.

@Override
public Schedule deleteScheduleById(final long classId, final long id) throws ClassException {
    try {
        return new AsyncTask<String, Schedule, Schedule>() {

            @Override
            protected Schedule doInBackground(String... args) {
                try {
                    String link = "".concat(domain).concat("/").concat(baseUri).concat("/").concat(payload).concat("/").concat(String.valueOf(classId)).concat("/").concat("schedule").concat("/").concat(String.valueOf(id));
                    URL url = new URL(link);
                    Gson gson = new Gson();
                    HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
                    httpURLConnection.setRequestMethod("DELETE");
                    httpURLConnection.setRequestProperty("Content-Type", "application/json");
                    httpURLConnection.setRequestProperty("Accept", "application/json");
                    httpURLConnection.connect();
                    if (httpURLConnection.getResponseCode() == 200) {
                        InputStream inputStream = httpURLConnection.getInputStream();
                        String jsonData = "";
                        int data;
                        while ((data = inputStream.read()) != -1) {
                            jsonData += (char) data;
                        }
                        return gson.fromJson(jsonData, Schedule.class);
                    } else if (httpURLConnection.getResponseCode() == 400) {
                        InputStream inputStream = httpURLConnection.getInputStream();
                        String jsonData = "";
                        int data;
                        while ((data = inputStream.read()) != -1) {
                            jsonData += (char) data;
                        }
                        Message message = gson.fromJson(jsonData, Message.class);
                        Log.i("ServiceTAG", "Service : Class");
                        Log.i("ServiceTAG", "Status : " + message.getStatus());
                        Log.i("ServiceTAG", "Type : " + message.getType());
                        Log.i("ServiceTAG", "Message : " + message.getMessage());
                        return null;
                    } else
                        throw new ClassException("Server Error");
                } catch (ClassException e) {
                    e.printStackTrace();
                    return null;
                } catch (IOException e) {
                    e.printStackTrace();
                    return null;
                }
            }
        }.execute((String) null).get();
    } catch (InterruptedException e) {
        e.printStackTrace();
        return null;
    } catch (ExecutionException e) {
        e.printStackTrace();
        return null;
    }
}
Also used : HttpURLConnection(java.net.HttpURLConnection) Message(com.remswork.project.alice.model.support.Message) InputStream(java.io.InputStream) AsyncTask(android.os.AsyncTask) Gson(com.google.gson.Gson) ClassException(com.remswork.project.alice.exception.ClassException) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) URL(java.net.URL)

Example 98 with AsyncTask

use of android.os.AsyncTask in project classify-system by anverliedoit.

the class ClassServiceImpl method getClassList.

@Override
public List<Class> getClassList() throws ClassException {
    final List<Class> _classList = new ArrayList<>();
    try {
        return new AsyncTask<String, List<Class>, List<Class>>() {

            @Override
            protected List<Class> doInBackground(String... args) {
                try {
                    String link = "".concat(domain).concat("/").concat(baseUri).concat("/").concat(payload);
                    URL url = new URL(link);
                    Gson gson = new Gson();
                    HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
                    httpURLConnection.setRequestMethod("GET");
                    httpURLConnection.setRequestProperty("Content-Type", "application/json");
                    httpURLConnection.setRequestProperty("Accept", "application/json");
                    httpURLConnection.connect();
                    if (httpURLConnection.getResponseCode() == 200) {
                        InputStream inputStream = httpURLConnection.getInputStream();
                        String jsonData = "";
                        int data;
                        while ((data = inputStream.read()) != -1) {
                            jsonData += (char) data;
                        }
                        JSONArray jsonArray = new JSONArray(jsonData);
                        for (int ctr = 0; ctr < jsonArray.length(); ctr++) {
                            _classList.add(gson.fromJson(jsonArray.get(ctr).toString(), Class.class));
                        }
                        return _classList;
                    } else if (httpURLConnection.getResponseCode() == 404) {
                        InputStream inputStream = httpURLConnection.getInputStream();
                        String jsonData = "";
                        int data;
                        while ((data = inputStream.read()) != -1) {
                            jsonData += (char) data;
                        }
                        Message message = gson.fromJson(jsonData, Message.class);
                        Log.i("ServiceTAG", "Service : Class");
                        Log.i("ServiceTAG", "Status : " + message.getStatus());
                        Log.i("ServiceTAG", "Type : " + message.getType());
                        Log.i("ServiceTAG", "Message : " + message.getMessage());
                        return _classList;
                    } else
                        throw new ClassException("Server Error");
                } catch (ClassException e) {
                    e.printStackTrace();
                    return null;
                } catch (IOException e) {
                    e.printStackTrace();
                    return null;
                } catch (JSONException e) {
                    e.printStackTrace();
                    return null;
                }
            }
        }.execute((String) null).get();
    } catch (InterruptedException e) {
        e.printStackTrace();
        return null;
    } catch (ExecutionException e) {
        e.printStackTrace();
        return null;
    }
}
Also used : Message(com.remswork.project.alice.model.support.Message) InputStream(java.io.InputStream) ArrayList(java.util.ArrayList) AsyncTask(android.os.AsyncTask) JSONArray(org.json.JSONArray) Gson(com.google.gson.Gson) ClassException(com.remswork.project.alice.exception.ClassException) JSONException(org.json.JSONException) IOException(java.io.IOException) URL(java.net.URL) HttpURLConnection(java.net.HttpURLConnection) Class(com.remswork.project.alice.model.Class) ExecutionException(java.util.concurrent.ExecutionException)

Example 99 with AsyncTask

use of android.os.AsyncTask in project classify-system by anverliedoit.

the class ClassServiceImpl method getClassListByTeacherId.

@Override
public List<Class> getClassListByTeacherId(final long teacherId) throws ClassException {
    final List<Class> _classList = new ArrayList<>();
    try {
        return new AsyncTask<String, List<Class>, List<Class>>() {

            @Override
            protected List<Class> doInBackground(String... args) {
                try {
                    String link = "".concat(domain).concat("/").concat(baseUri).concat("/").concat(payload).concat("?teacherId=").concat(String.valueOf(teacherId));
                    URL url = new URL(link);
                    Gson gson = new Gson();
                    HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
                    httpURLConnection.setRequestMethod("GET");
                    httpURLConnection.setRequestProperty("Content-Type", "application/json");
                    httpURLConnection.setRequestProperty("Accept", "application/json");
                    httpURLConnection.connect();
                    if (httpURLConnection.getResponseCode() == 200) {
                        InputStream inputStream = httpURLConnection.getInputStream();
                        String jsonData = "";
                        int data;
                        while ((data = inputStream.read()) != -1) {
                            jsonData += (char) data;
                        }
                        JSONArray jsonArray = new JSONArray(jsonData);
                        for (int ctr = 0; ctr < jsonArray.length(); ctr++) {
                            _classList.add(gson.fromJson(jsonArray.get(ctr).toString(), Class.class));
                        }
                        return _classList;
                    } else if (httpURLConnection.getResponseCode() == 404) {
                        InputStream inputStream = httpURLConnection.getInputStream();
                        String jsonData = "";
                        int data;
                        while ((data = inputStream.read()) != -1) {
                            jsonData += (char) data;
                        }
                        Message message = gson.fromJson(jsonData, Message.class);
                        Log.i("ServiceTAG", "Service : Class");
                        Log.i("ServiceTAG", "Status : " + message.getStatus());
                        Log.i("ServiceTAG", "Type : " + message.getType());
                        Log.i("ServiceTAG", "Message : " + message.getMessage());
                        return _classList;
                    } else
                        throw new ClassException("Server Error");
                } catch (ClassException e) {
                    e.printStackTrace();
                    return null;
                } catch (IOException e) {
                    e.printStackTrace();
                    return null;
                } catch (JSONException e) {
                    e.printStackTrace();
                    return null;
                }
            }
        }.execute((String) null).get();
    } catch (InterruptedException e) {
        e.printStackTrace();
        return null;
    } catch (ExecutionException e) {
        e.printStackTrace();
        return null;
    }
}
Also used : Message(com.remswork.project.alice.model.support.Message) InputStream(java.io.InputStream) ArrayList(java.util.ArrayList) AsyncTask(android.os.AsyncTask) JSONArray(org.json.JSONArray) Gson(com.google.gson.Gson) ClassException(com.remswork.project.alice.exception.ClassException) JSONException(org.json.JSONException) IOException(java.io.IOException) URL(java.net.URL) HttpURLConnection(java.net.HttpURLConnection) Class(com.remswork.project.alice.model.Class) ExecutionException(java.util.concurrent.ExecutionException)

Example 100 with AsyncTask

use of android.os.AsyncTask in project classify-system by anverliedoit.

the class ClassServiceImpl method getClassListByStudentId.

@Override
public List<Class> getClassListByStudentId(final long studentId) throws ClassException {
    final List<Class> _classList = new ArrayList<>();
    try {
        return new AsyncTask<String, List<Class>, List<Class>>() {

            @Override
            protected List<Class> doInBackground(String... args) {
                try {
                    String link = "".concat(domain).concat("/").concat(baseUri).concat("/").concat(payload).concat("?studentId=").concat(String.valueOf(studentId));
                    URL url = new URL(link);
                    Gson gson = new Gson();
                    HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
                    httpURLConnection.setRequestMethod("GET");
                    httpURLConnection.setRequestProperty("Content-Type", "application/json");
                    httpURLConnection.setRequestProperty("Accept", "application/json");
                    httpURLConnection.connect();
                    if (httpURLConnection.getResponseCode() == 200) {
                        InputStream inputStream = httpURLConnection.getInputStream();
                        String jsonData = "";
                        int data;
                        while ((data = inputStream.read()) != -1) {
                            jsonData += (char) data;
                        }
                        JSONArray jsonArray = new JSONArray(jsonData);
                        for (int ctr = 0; ctr < jsonArray.length(); ctr++) {
                            _classList.add(gson.fromJson(jsonArray.get(ctr).toString(), Class.class));
                        }
                        return _classList;
                    } else if (httpURLConnection.getResponseCode() == 404) {
                        InputStream inputStream = httpURLConnection.getInputStream();
                        String jsonData = "";
                        int data;
                        while ((data = inputStream.read()) != -1) {
                            jsonData += (char) data;
                        }
                        Message message = gson.fromJson(jsonData, Message.class);
                        Log.i("ServiceTAG", "Service : Class");
                        Log.i("ServiceTAG", "Status : " + message.getStatus());
                        Log.i("ServiceTAG", "Type : " + message.getType());
                        Log.i("ServiceTAG", "Message : " + message.getMessage());
                        return _classList;
                    } else
                        throw new ClassException("Server Error");
                } catch (ClassException e) {
                    e.printStackTrace();
                    return null;
                } catch (IOException e) {
                    e.printStackTrace();
                    return null;
                } catch (JSONException e) {
                    e.printStackTrace();
                    return null;
                }
            }
        }.execute((String) null).get();
    } catch (InterruptedException e) {
        e.printStackTrace();
        return null;
    } catch (ExecutionException e) {
        e.printStackTrace();
        return null;
    }
}
Also used : Message(com.remswork.project.alice.model.support.Message) InputStream(java.io.InputStream) ArrayList(java.util.ArrayList) AsyncTask(android.os.AsyncTask) JSONArray(org.json.JSONArray) Gson(com.google.gson.Gson) ClassException(com.remswork.project.alice.exception.ClassException) JSONException(org.json.JSONException) IOException(java.io.IOException) URL(java.net.URL) HttpURLConnection(java.net.HttpURLConnection) Class(com.remswork.project.alice.model.Class) ExecutionException(java.util.concurrent.ExecutionException)

Aggregations

AsyncTask (android.os.AsyncTask)394 IOException (java.io.IOException)188 InputStream (java.io.InputStream)159 URL (java.net.URL)159 HttpURLConnection (java.net.HttpURLConnection)158 ExecutionException (java.util.concurrent.ExecutionException)158 Gson (com.google.gson.Gson)155 Message (com.remswork.project.alice.model.support.Message)153 GradingFactorException (com.remswork.project.alice.exception.GradingFactorException)102 ArrayList (java.util.ArrayList)93 View (android.view.View)54 Intent (android.content.Intent)52 TextView (android.widget.TextView)52 JSONException (org.json.JSONException)51 JSONArray (org.json.JSONArray)50 DialogInterface (android.content.DialogInterface)40 OutputStream (java.io.OutputStream)37 BufferedWriter (java.io.BufferedWriter)35 OutputStreamWriter (java.io.OutputStreamWriter)35 ImageView (android.widget.ImageView)33