0

I have a scenario where I have to pass userType that is the variable to the asyncTask. The variable contains either of the two:- teacher or student that is coming from the server. In the AsyncTask if the userType = TEACHER then a URL is fired and if userType = STUDENT the a new URL is fired.

new GetIncidentsTask(IncidentsActivity.this).execute(userType);

From here I am sending the usertype.

Now in the asyncTask I have to check But unfornately I cant

if (){
            String json = jsonParser.makeHttpRequest(CC.GETINCIDENTS_TEACHER, "GET",
                    null);
            String details = json.toString();
            Log.d("List of Incidents", details);
            }else {
                String json = jsonParser.makeHttpRequest(CC.GETINCIDENTS_STUDENT, "GET",
                        null);
                String details = json.toString();
                Log.d("List of Incidents", details);
            }

How to check? I am stuck into this for quite sometime.Please help.

Priyanka
  • 47
  • 7
  • doinbackground method having params parameter, just write params[0], you will get your value – Ravi Mar 24 '15 at 10:06
  • Parse the json to get the string value, then check that string value. – Tushar Gogna Mar 24 '15 at 10:06
  • @Tushar: Already in this line new GetIncidentsTask(IncidentsActivity.this).execute(userType); The userType contains the value whether it is a teacher or student – Priyanka Mar 24 '15 at 10:10

4 Answers4

0
@Override
    protected Integer doInBackground(Object... params) {
      Object obj= params[0];
      if(obj instance of Teacher)
      {
          // Teacher URL
      }
      else
      {
          // Student URL
      }

    }
Hardik Trivedi
  • 5,395
  • 3
  • 28
  • 53
0
    @Override
    protected String doInBackground(String... params) 
    {
          if (params[0].equals("TEACHER"))
          {
                String json = jsonParser.makeHttpRequest(CC.GETINCIDENTS_TEACHER, "GET",
                null); 
                String details = json.toString();
                Log.d("List of Incidents", details);
          }
          else
          {
                String json = jsonParser.makeHttpRequest(CC.GETINCIDENTS_STUDENT, "GET",
                    null); 
                String details = json.toString();
                Log.d("List of Incidents", details);
          }
    }
Ravi
  • 30,808
  • 18
  • 108
  • 168
0
enum userType{
    student,
    teacher;
}

class MyData {
    userType type;
    //other data
}

// now here i assume that "json" string contains your pre-filter data

Gson gson = new Gson()
    MyData data = gson.fromJson(json, MyData.class);

    if (data.type==userType.teacher){
        String json = jsonParser.makeHttpRequest(CC.GETINCIDENTS_TEACHER, "GET",
                null);
        String details = json.toString();
        Log.d("List of Incidents", details);
        }
    else {
            String json = jsonParser.makeHttpRequest(CC.GETINCIDENTS_STUDENT, "GET",
                    null);
            String details = json.toString();
            Log.d("List of Incidents", details);
        }
Manan Sharma
  • 634
  • 2
  • 14
  • 26
0

Considering userType as int or Integer

AsyncTask<Integer,Void,String> task = new AsyncTask<Integer, Void, String>() {
    @Override
    protected String doInBackground(Integer... params) {
          int userType = params[0];
          if(userType == STUDENT){

          }else{

          }
          return null;
    }

    @Override
    protected void onPostExecute(String s) {
        super.onPostExecute(s);
    }
};
task.execute(STUDENT);
Sreejith B Naick
  • 1,195
  • 6
  • 12