OKhttp网络获取框架使用

来源:互联网 发布:淘宝神笔用处是什么 编辑:程序博客网 时间:2024/05/20 08:24

1.获取网络权限和导包

compile 'com.squareup.okhttp3:okhttp:3.9.0'



<uses-permission android:name="android.permission.INTERNET"></uses-permission>


导包



2.主函数

public class MainActivity extends AppCompatActivity {    private ImageView iv;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        iv=(ImageView)findViewById(R.id.iv);    }    public void cc(View view) {        switch (view.getId()){            case R.id.but1:                get();                break;            case R.id.but2:                post();                break;            case R.id.but3:                getImag();                break;        }    }    private void getImag() {        OkHttpClient client = new OkHttpClient();        Request request = new Request.Builder()                .url("https://ss0.bdstatic.com/5aV1bjqh_Q23odCf/static/superman/img/logo/logo_redBlue.png")                .build();        Call call = client.newCall(request);        call.enqueue(new Callback() {            @Override            public void onFailure(Call call, IOException e) {            }            @Override            public void onResponse(Call call, Response response) throws IOException {                byte[] bytes = response.body().bytes();                final Bitmap bmp = BitmapFactory.decodeByteArray(bytes, 0, bytes.length);                //相当于Handler.post(Runnable对象),此方法在Activity中调用                runOnUiThread(new Runnable() {                    @Override                    public void run() {                        iv.setImageBitmap(bmp);                    }                });            }        });    }    private void post() {        OkHttpClient client =new OkHttpClient();        //提交给服务器的请求体内容        RequestBody body =new FormBody.Builder()                .add("pageNo","1")                .add("pageSize","20")                .add("serialIds","2143,3404")                .add("v","4.0.0")                .build();        Request request                = new Request.Builder()                .url("http://mrobot.pcauto.com.cn/v2/cms/channels/1?")                .post(body)                .build();        Call call = client.newCall(request);        call.enqueue(new Callback() {            @Override            public void onFailure(Call call, IOException e) {            }            @Override            public void onResponse(Call call, Response response) throws IOException {                Log.e("aa",response.body().string());            }        });}    private void get() {        OkHttpClient ok=new OkHttpClient();        Request re=new Request.Builder().url("http://a121.baopiqi.com/api/mh/getVideoClassification.php?&appname=%E7%88%B1%E6%83%85%E6%BC%AB%E7%94%BB%E7%B2%BE%E9%80%89&pkgname=com.platform.cartoonl&imei=863191020203140&versionname=1.2.7&page=0&limit=12")               .get().build();        Call call = ok.newCall(re);        //同步:同一时刻只有一个任务在进行  execute()        //异步:同一时刻多个任务在执行  enqueue()        call.enqueue(new Callback() {            @Override            public void onFailure(Call call, IOException e) {            }            @Override            public void onResponse(Call call, Response response) throws IOException {                Log.d("aa",response.body().string());            }        });    }}



3.主布局

<?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:id="@+id/activity_main"    android:layout_width="match_parent" android:layout_height="match_parent"    tools:context="com.example.myapplication.MainActivity"    android:orientation="vertical">    <Button        android:id="@+id/but1"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="get"        android:onClick="cc"/>    <Button        android:id="@+id/but2"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="post"        android:onClick="cc"/>    <Button        android:id="@+id/but3"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="img"        android:onClick="cc"/>    <ImageView        android:id="@+id/iv"        android:layout_width="match_parent"        android:layout_height="wrap_content" /></LinearLayout>