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
原创粉丝点击