Android网络通讯库之Okhttp3
来源:互联网 发布:百慕达网络超市好不好 编辑:程序博客网 时间:2024/06/15 12:04
Android原生的网络通讯都是用
HttpURLConnection和HttpClient,由于HttpURLConnection编写步骤繁琐、HttpClient在android6.0系统中被完全移除。进而有出现了多样的网络通讯库,而本人觉得最好用的当属okhttp,使用简单。
使用步骤:
1.首先引入库
//添加网络通讯库okhttpcompile 'com.squareup.okhttp3:okhttp:3.4.1'
如果项目中使用频繁的话,可以进行适当的封装
OkhttpUtils.class 封装的工具类
public class OkhttpUtils { public static String sendHttpRequest(String address) { String responseData = null; try { //创建OkhhtpClient对象 OkHttpClient client = new OkHttpClient(); //创建请求的地址 Request request = new Request.Builder() .url(address)//传入请求地址 .build(); //响应结果 Response response = client.newCall(request).execute(); //将响应结果转化成字符串 responseData = response.body().string(); } catch (IOException e) { e.printStackTrace(); } return responseData; }}
MainActivity.class
public class MainActivity extends AppCompatActivity { private Button bt_get; private TextView textView; private Handler handler = new Handler() { @Override public void handleMessage(Message msg) { super.handleMessage(msg); List list = (List) msg.obj; switch (msg.what) { case 1: textView.setText(list.get(0).toString()); break; } } }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); ActionBar actionBar = getSupportActionBar(); actionBar.setTitle("网络通讯库之OKhttp"); bt_get = (Button) findViewById(R.id.bt_get); textView = (TextView) findViewById(R.id.tv); bt_get.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { final String url = "http://blog.csdn.net/qq_32895969"; Toast.makeText(MainActivity.this, "发送请求", Toast.LENGTH_SHORT).show(); new Thread(new Runnable() { @Override public void run() { //将Okhttp封装使用 String responesData = OkhttpUtils.sendHttpRequest(url); Message message = handler.obtainMessage(); message.what = 1; List list = new ArrayList(); list.add(responesData); message.obj = list; handler.sendMessage(message); } }).start(); } }); }}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context="com.example.okhttplearn.MainActivity"> <Button android:id="@+id/bt_get" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="GET请求" /> <ScrollView android:layout_width="match_parent" android:layout_height="match_parent" android:scrollbars="none"> <TextView android:id="@+id/tv" android:layout_width="match_parent" android:layout_height="match_parent" /> </ScrollView></LinearLayout>
1 0
- Android网络通讯库之Okhttp3
- Android网络通讯之资讯篇
- Android开发之网络通讯详解
- Android开发之网络通讯详解
- Android开发之网络通讯详解
- Android开发之网络通讯详解
- android 网络框架之okhttp3
- android网络通讯之socket(上)
- Android 网络通讯之HTTP请求通信
- Android网络通讯之HTTP请求通信(一)
- Android网络通讯之HTTP请求通信(二)
- Android网络通讯之HTTP请求通信(三)
- Android网络通讯之HTTP请求通信(四)
- android网络通讯数据封装之 json
- android网络通讯数据封装之 json
- Android 网络通讯之HTTP请求通信
- Android知识点之网络通讯(上)
- Android知识点之网络通讯(下)
- B. Sherlock and his girlfriend
- PLSQL的介绍
- 部署ftp文件共享服务
- hdu 5728 (欧拉函数性质的应用)
- matplotlib绘图案例
- Android网络通讯库之Okhttp3
- UVALive 4625 Garlands(dp+二分)
- unity JS调用C#出现BCE0019报错的情况
- oracle -plsqly语句
- Sleeping会话导致阻塞原理(下)
- 括号匹配
- intellij IDEA怎样打war包
- 关于android studio错误:gradle project sync failed.please fix your project and try again的解决方案
- kinect Unity开发文章链接