Android 开发中使用回调执行网络请求
来源:互联网 发布:iphone赚钱软件排行 编辑:程序博客网 时间:2024/06/17 03:12
在Android的开发中经常使用网络请求,下面就让我们看看在Android中如何通过回调来获取网络数据吧:首先我们需要一个执行回调的类:public class AsyClass { //模拟从服务器获取数据 public void getDataFromSever(String path,final Callback callback){ new Thread(new Runnable() { @Override public void run() { // TODO Auto-generated method stub String msg="通过接口调用数据"; callback.getData(msg); } }).start(); } //用于回调的接口 public interface Callback{ public void getData(String result); } }然后就是在Activity中执行网络请求了:import android.os.Bundle;import android.app.Activity;import android.util.Log;import android.view.Menu;import android.view.View;import android.widget.Button; public class MainActivity extends Activity { private Button button; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); button=(Button)this.findViewById(R.id.button1); button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View arg0) { // TODO Auto-generated method stub AsyClass asy=new AsyClass(); //通过调用接口得到数据 asy.loadString("", new Callback() { @Override public void getData(String result) { // TODO Auto-generated method stub Log.i("TAG", "-------->"+result); } }); } }); } }
0 0
- Android 开发中使用回调执行网络请求
- Android开发--网络请求中GZip的使用
- Android开发--网络请求中GZip的使用(二)
- Android开发中如何执行POST请求
- Android开发中如何执行POST请求
- Android 中通过定时器周期性执行网络请求
- Android中retrofit网络请求框架使用
- android中使用 HttpURLConnection 做网络请求
- android开发网络请求
- Android开发中网络请求的压缩 ── GZip的使用
- Android开发中网络请求的压缩 ── GZip的使用
- Android开发中网络请求的压缩 ── GZip的使用
- Android开发中网络请求的压缩 ── GZip的使用
- Android开发中网络请求的压缩 ── GZip的使用
- Android开发中网络请求的压缩 ── GZip的使用
- Android开发中网络请求的压缩 ── GZip的使用
- kotlin 开发 android 程序中网络http请求和线程的使用
- iOS中使用block进行网络请求回调 - xuym
- locate命令与find命令的区别
- 程矢Axure夜话:中继器系列视频教程之中继器表格分页
- 博客开始
- 程矢Axure夜话:中继器系列视频教程之中继器实现的表格
- Maven之——仓库(下)
- Android 开发中使用回调执行网络请求
- Android实现翻页功能原理
- 修饰符extern修饰变量或者函数(转)
- 程矢Axure夜话:Axure手机原型视频教程之侧滑菜单(抽屉导航)
- 以前的博客
- RabbitMQ学习之spring整合发送异步消息
- Ubuntu下crontab命令的用法
- 程矢Axure夜话: Axure手机原型视频教程汇总贴
- #define 后面跟 \