我们可以为两个不同的asynctask使用两个接口

时间:2018-01-29 13:35:51

标签: java android android-asynctask

AsyncResponse.java:

public interface AsyncResponse {
    void ProcessFinish(String Output);
}

AsyncResponseSecond.java:

public interface AsyncResponseSecond {
    void ProcessFinishSecond(String Output);
}

这是Asynctask OnPostExecute获取结果并将其保存在TextView中。

我想知道我使用的方法是否正确。我没有收到回复

Asyncresponse用于一个按钮,AsyncresponseSecond用于其他按钮

IntimeWorker.java:

public class InTImeWorker extends AsyncTask<String, Void, String> {
Context context;
AlertDialog alertDialog;
public AsyncResponse delegate = null;

InTImeWorker(Context ctx) {
    context = ctx;
}

@Override
protected void onPreExecute() {
    alertDialog = new AlertDialog.Builder(context).create();
    alertDialog.setTitle("In Time Status !");
}

@Override
protected void onPostExecute(String result) {
    delegate.ProcessFinish(result);
}

@Override
protected String doInBackground(String... params) {
    String Emp_id = params[0];
    String in_time_url = "http://192.168.0.107/RTOS/intime.php";
    try {
        URL url = new URL(in_time_url);
        HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
        httpURLConnection.setRequestMethod("POST");
        httpURLConnection.setDoOutput(true);
        httpURLConnection.setDoInput(true);
        OutputStream outputStream = httpURLConnection.getOutputStream();
        BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(outputStream, "UTF-8"));
        String post_data = URLEncoder.encode("emp_id", "UTF-8") + "=" + URLEncoder.encode(Emp_id, "UTF-8");
        bufferedWriter.write(post_data);
        bufferedWriter.flush();
        bufferedWriter.close();
        outputStream.close();
        InputStream inputStream = httpURLConnection.getInputStream();
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream, "iso-8859-1"));
        String result = "";
        String line = "";
        while ((line = bufferedReader.readLine()) != null) {
            result += line;
        }
        bufferedReader.close();
        inputStream.close();
        httpURLConnection.disconnect();
        return result;
    } catch (MalformedURLException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return null;

}
}

OuttimeWorker.java:

public class OutTimeWorker extends AsyncTask<String,Void,String> {
Context context;
AlertDialog alertDialog;
public AsyncResponseSecond delegate = null;

OutTimeWorker(Context ctx){
    context = ctx;
}

@Override
protected void onPreExecute() {
    alertDialog = new AlertDialog.Builder(context).create();
    alertDialog.setTitle("Out Time Status !");
}

@Override
protected void onPostExecute(String result) {
    delegate.ProcessFinishSecond(result);
}

@Override
protected String doInBackground(String... params) {
    String Emp_id = params[0];
    String out_time_url = "http://192.168.0.107/RTOS/outtime.php";
        try {
            URL url = new URL(out_time_url);
            HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
            httpURLConnection.setRequestMethod("POST");
            httpURLConnection.setDoOutput(true);
            httpURLConnection.setDoInput(true);
            OutputStream outputStream = httpURLConnection.getOutputStream();
            BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(outputStream, "UTF-8"));
            String post_data = URLEncoder.encode("emp_id","UTF-8")+"="+URLEncoder.encode(Emp_id,"UTF-8");
            bufferedWriter.write(post_data);
            bufferedWriter.flush();
            bufferedWriter.close();
            outputStream.close();
            InputStream inputStream = httpURLConnection.getInputStream();
            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream,"iso-8859-1"));
            String result = "";
            String line = "";
            while ((line = bufferedReader.readLine())!= null){
                result += line;
            }
            bufferedReader.close();
            inputStream.close();
            httpURLConnection.disconnect();
            return result;
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

    return null;
}
}

EmployeeActivity.java:

public class EmployeeActivity extends AppCompatActivity implements AsyncResponse , AsyncResponseSecond {

TextView name, In_time, Out_time;
SharedPreferences sp,sp1;
Button in_time_button, out_time_button;
private static final String KEY_IN_TIME_TEXTVIEW = "intimetextview_key";
private static final String KEY_OUT_TIME_TEXTVIEW = "outtimetextview_key";
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_employee);

    name = findViewById(R.id.text_name);
    sp = getSharedPreferences("attendlogin", MODE_PRIVATE);
    String emp_name = sp.getString("name", null);
    name.setText(emp_name);


    in_time_button = findViewById(R.id.buttonlogin);
    out_time_button = findViewById(R.id.buttonlogout);
    out_time_button.setEnabled(false);
    In_time = findViewById(R.id.text_in_time);
    Out_time = findViewById(R.id.text_out_time);
public void OnAttendLogin(View view) {
    sp = getSharedPreferences("attendlogin", MODE_PRIVATE);
    String emp_id = sp.getString("emp_id", null);
    InTImeWorker inTImeWorker = new InTImeWorker(this);
    inTImeWorker.delegate = (AsyncResponse) this;
    inTImeWorker.execute(emp_id);

    //shared pref for saving In_time in textview
    sp1 = getSharedPreferences("InTime", MODE_PRIVATE);
    SharedPreferences.Editor editor1 = sp1.edit();
    String in_time_sharedpref = In_time.getText().toString();
    editor1.putString("in_time_sp", in_time_sharedpref);
    editor1.apply();
    editor1.commit();
    out_time_button.setEnabled(true);
    in_time_button.setEnabled(false);
}
public void OnLogout(View view) {
    sp = getSharedPreferences("attendlogin", MODE_PRIVATE);
    String emp_id = sp.getString("emp_id", null);
    OutTimeWorker outTimeWorker = new OutTimeWorker(this);
    outTimeWorker.delegate = (AsyncResponseSecond) this;
    outTimeWorker.execute(emp_id);
    out_time_button.setEnabled(false);
    in_time_button.setEnabled(false);
}

@Override
public void ProcessFinish(String Output) {
    In_time.setText(Output);
}

@Override
public void ProcessFinishSecond(String Output) {
    Out_time.setText(Output);
}}

1 个答案:

答案 0 :(得分:0)

这样做

CallAPI asyncTask = new CallAPI(getContext());
CallAPI asyncTask2 = new CallAPI(getContext());
final chat text = this;
  sendb.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {

            try {
                String data = null;

   //data to send

                CallAPI asyncTask = new CallAPI(getContext());
                asyncTask.delegate = text;
                asyncTask.execute("", data, "sendMsg");
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }


           }
sendb2.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {

            try {
                String data = null;

   //data to send

                CallAPI asyncTask2 = new CallAPI(getContext());
                asyncTask2.delegate = text;
                asyncTask2.execute("", data, "sendMsg");
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }
        }
    });


  @Override
    public void onTaskCompleted(String output, String output2) {
}