search

Home  >  Q&A  >  body text

android - How to make a function return the return value of an asynchronous request?

The network requests a mobile phone number, and the result returns null, because the function executes return without waiting for the network request to come back. How to modify the following code?

public String getPhone(String id) {
    String url = "http://www.163.net/";

    final String[] phone = new String[1];
    OkHttpUtils
            .get()
            .url(url)
            .addParams("username", "abc")
            .addParams("password", "123")
            .build()
            .execute(new StringCallback() {

                @Override
                public void onError(Call call, Exception e, int id) {

                }

                @Override
                public void onResponse(String response, int id) {
                    phone[0] = response;
                }
            });
    return phone[0];
}
扔个三星炸死你扔个三星炸死你2732 days ago1282

reply all(2)I'll reply

  • 黄舟

    黄舟2017-06-26 10:51:53

    Of course you need to use CountDownLatch to convert asynchronous requests into blocking synchronous requests

    public String getPhone(String id) {
        String url = "http://www.163.net/";
        final CountDownLatch latch = new CountDownLatch(1); 
        final String[] phone = new String[1];
        OkHttpUtils
                .get()
                .url(url)
                .addParams("username", "abc")
                .addParams("password", "123")
                .build()
                .execute(new StringCallback() {
    
                    @Override
                    public void onError(Call call, Exception e, int id) {
                         latch.countDown();  
                    }
    
                    @Override
                    public void onResponse(String response, int id) {
                        phone[0] = response;
                        latch.countDown();  
                    }
                });
                try {  
               latch.await();  
           } catch (InterruptedException e) {  
           }  
        return phone[0];
    }

    reply
    0
  • 世界只因有你

    世界只因有你2017-06-26 10:51:53

    I think there is something wrong with your function. The formal parameter id is not used in it, which is very strange. If you want to obtain asynchronous data, the most commonly used one is asynchronous callback. You can try RXJava in the future and you will be surprised.

    Rewrite this function as follows:

    public static void getPhone(String id,StringCallback cb) {
        String url = "http://www.163.net/";
    
        final String[] phone = new String[1];
        OkHttpUtils
                .get()
                .url(url)
                .addParams("username", "abc")
                .addParams("password", "123")
                .build()
                .execute(cb);
    }
    

    When calling, it can be

     XXutil.getPhone("1234566",new StringCallback(){
    
                    @Override
                    public void onError(Call call, Exception e, int id) {
                        //do something
                    }
    
                    @Override
                    public void onResponse(String response, int id) {
                        //do something
                    }
    
    });

    reply
    0
  • Cancelreply