Okhtt异步解析
来源:互联网 发布:知乎海贼王回答集锦 编辑:程序博客网 时间:2024/04/30 07:31
package com.example.okhttp2;import android.os.Handler;import android.os.Message;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.util.Log;import android.widget.TextView;import java.io.IOException;import okhttp3.Call;import okhttp3.Callback;import okhttp3.OkHttpClient;import okhttp3.Request;import okhttp3.Response;public class MainActivity extends AppCompatActivity { //通过Handler睡眠方式吧数据展示到控件上 Handler handler = new Handler(){ @Override public void handleMessage(Message msg) { super.handleMessage(msg); //接受下边发上来的数据 Bundle bundle = msg.getData(); //打印数据 tv.setText(bundle.getString("string")); } }; private TextView tv; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); tv = (TextView) findViewById(R.id.tv); //创建okHttpClient方法 OkHttpClient okHttpClient = new OkHttpClient.Builder().build(); //异步请求网络地址 final Request build = new Request.Builder().url("http://www.baidu.com").build(); //异步进行请求 okHttpClient.newCall(build).enqueue(new Callback() { //失败的返回值 @Override public void onFailure(Call call, IOException e) { Log.i("==========", "onFailure: "+e.getMessage()); } //成功的返回值 @Override public void onResponse(Call call, Response response) throws IOException { //response返回的值 String string = response.body().string(); Log.i("---------", "onResponse: "+string); Message message = Message.obtain(); //创建Bundle方法 Bundle bundle = new Bundle(); //吧解析的数据put到bundle方法里面 bundle.putString("string",string); //吧bundle的数据set到message message.setData(bundle); //发送消息 handler.sendMessage(message); } }); }}
阅读全文
0 0
- Okhtt异步解析
- OKHtt自己封装
- 异步方式解析域名
- Ajax异步 解析xml
- ios JSON解析(异步)
- 同步与异步解析
- Android 异步完全解析
- AsyncTask异步任务解析
- AsyncTask 异步任务解析
- 异步和解析,通用
- 异步get解析
- 用异步方式解析域名
- 用异步方式解析域名
- AsyncTask异步任务使用解析
- 使用libevent异步解析dns
- 异步POST请求解析JSON
- 解析异步消息处理机制
- android的异步任务解析
- [LeetCode-Algorithms-17] "Letter Combinations of a Phone Number" (2017.10.12-WEEK6)
- [Mysql必知必会系列]·检索数据
- FTP服务器连接及状态--vb.net
- Java集合之Vector源码分析
- 莫比乌斯函数详解
- Okhtt异步解析
- linux 下vim的使用(学习必看!!重要)
- spring boot 拦截器
- GitHub设置添加SSH
- 数据库初接触
- 关于Microsoft Visual Studio 2013停止运行的解决
- Python networkx 实现网络流
- 基于MAC的SeetaFace安装与测试(二)
- C语言将二进制浮点显示到BMP格式文件(float2bmp.c)