我有一个场景,我必须将作为变量的userType传递给asyncTask。该变量包含以下两者之一: - 来自服务器的教师或学生。在AsyncTask中,如果userType = TEACHER,则会触发URL,如果userType = STUDENT,则会触发新的URL。
new GetIncidentsTask(IncidentsActivity.this).execute(userType);
从这里发送usertype。
现在在asyncTask我必须检查 但不幸的是我不能
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);
}
如何检查? 我坚持了很长一段时间。请帮助。
答案 0 :(得分:0)
@Override
protected Integer doInBackground(Object... params) {
Object obj= params[0];
if(obj instance of Teacher)
{
// Teacher URL
}
else
{
// Student URL
}
}
答案 1 :(得分: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);
}
}
答案 2 :(得分:0)
enum userType{
student,
teacher;
}
class MyData {
userType type;
//other data
}
//现在我假设“json”字符串包含您的预过滤数据
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);
}
答案 3 :(得分:0)
将userType
视为int
或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);