接口回调(可以用接口回调来实现线程之间数据传递)

代码如下所示:

public class MainActivity extends AppCompatActivity {
    private Button button;
    private TextView textView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        button = (Button) findViewById(R.id.button);
        textView = (TextView) findViewById(R.id.textView2);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                HttpUtil.getResponse(new huidiao() {
                    @Override
                    public void onSuccess(final String s) {
                        Log.i("xinxi",s);
                       /* 输出结果为:
                        
                         贴吧 
//
*/ runOnUiThread(new Runnable() { @Override public void run() { textView.setText(s); } }); } @Override public void onFailure(Exception e) { } }); } }); } }

在HttpUtil.java文件中

public class HttpUtil {
    public static void getResponse(final huidiao huidiao){
        new Thread(new Runnable() {
            @Override
            public void run() {
                OkHttpClient okHttpClient = new OkHttpClient();
                Request request = new Request.Builder()
                        .url("http://www.baidu.com")
                        .build();
                Response response = null;
                try {
                    response = okHttpClient.newCall(request).execute();
                    final String responsedata = response.body().string();//responsedata这个栈的地址指向了response.body().string()所在的堆的地址
                    huidiao.onSuccess(responsedata);//这一步将responsedata传到了onSuccess方法中,相当于将MainActivity中的onSuccess方法中参数s指向了responsedata所指向的堆的地址
                } catch (IOException e) {
                    huidiao.onFailure(e);
                    e.printStackTrace();
                }
            }
        }).start();

    }
}

在huidiao.java文件中

public interface huidiao {
    void onSuccess(String s);
    void onFailure(Exception e);
}

再来一个例子,在okhttp中的接口回调,可以进行二者的对比

public class MainActivity extends AppCompatActivity {
    private Button button;
    private TextView textView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        button = (Button) findViewById(R.id.button);
        textView = (TextView) findViewById(R.id.textView2);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                HttpUtil.getResponse(new Callback() {
                    @Override
                    public void onFailure(Call call, IOException e) {

                    }

                    @Override
                    public void onResponse(Call call, final Response response) throws IOException {
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                try {
                                    textView.setText(response.body().string());
                                } catch (IOException e) {
                                    e.printStackTrace();
                                }
                            }
                        });
                    }
                });
            }
        });
    }
}

在HttpUtil.java文件中

public class HttpUtil {
    public static void getResponse(Callback callback){
        OkHttpClient okHttpClient = new OkHttpClient();
        Request request = new Request.Builder()
                .url("http://www.baidu.com")
                .build();
        okHttpClient.newCall(request).enqueue(callback);
    }
}

你可能感兴趣的:(接口回调(可以用接口回调来实现线程之间数据传递))