简体   繁体   English

android从线程内返回结果

[英]android return result from within the thread

I have an upload(String base_code) method that takes a file in form of base64 encoded string, this method makes a post request with the base64 string to an api endpoint to upload the file in the server, and if the upload is successful, the api endpoint will return a 200 status code. 我有一个upload(String base_code)方法,该文件采用以base64编码的字符串形式的文件,该方法向api端点发出带有base64字符串的发布请求,以将文件上传到服务器中,如果上传成功,则api端点将返回200状态代码。

 public static void upload(final String base_code) {

    Thread thread = new Thread(new Runnable() {

        @Override
        public void run() {
            try  {

                Request request;
                RequestBody request_body;
                Response response;
                MultipartBody.Builder body = new MultipartBody.Builder();

                body.setType(MultipartBody.FORM);
                body.addFormDataPart("img", base_code);

                request_body = body.build();
                request = new Request.Builder().url("https://api/endpoint/upload").post(request_body).build();
                response = client.newCall(request).execute();

            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    });

    thread.start();

}

I would like for the method upload() to return a true or false whether the upload is successful, however I can't do that since the code executes in a its own thread. 我希望方法upload()返回true或false是否上传成功,但是我不能这样做,因为代码在其自己的线程中执行。 How would I do this? 我该怎么做?

The post request is done via the okhttp3 lib and I can determine whether the upload was successful by checking response.code() . 发布请求是通过okhttp3 lib完成的,我可以通过检查response.code()来确定上传是否成功。

Thanks 谢谢

Use AsyncTask . 使用AsyncTask You can execute the thread in doInBackground method and pass the data back to the uiThread via onPostExecute method. 您可以执行threaddoInBackground方法和数据传递回uiThread通过onPostExecute方法。

Check this out. 检查这个出来。

I prefer use ExecutorService or AsyncTask . 我更喜欢使用ExecutorServiceAsyncTask Use thread immediately is hard to manage. 立即使用线程很难管理。

Use ExecutorService to monitor the status of task: 使用ExecutorService监视任务状态:

ExecutorService es = Executors.newCachedThreadPool();
Future future = es.submit(new Runnable() {
    @Override
    public void run() {
       // Http post here
    }
});

Then you can call method of Future to check the execute status. 然后可以调用Future方法来检查执行状态。

Use AsyncTask: 使用AsyncTask:

private class postTask extends AsyncTask<String, Integer, Boolean> {

    @Override
    protected Boolean doInBackground(String... strings) {
        // Http post here
        return result == 200;
    }

    @Override
    protected void onPostExecute(Boolean success) {
        super.onPostExecute(success);
        // Do something according to `success`
    }
}

Then call execute(url) or executeOnExecutor(es, url) to start; 然后调用execute(url)executeOnExecutor(es, url)开始;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM