OkHttp get请求 和post请求

来源:互联网 发布:mac双系统移除win8后 编辑:程序博客网 时间:2024/05/20 05:44

//依赖

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

//网络请求

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



//布局

<Button    android:id="@+id/btn_okhttp_get"    android:text="Get请求"    android:layout_width="match_parent"    android:layout_height="wrap_content" /><Button    android:id="@+id/btn_okhttp_post"    android:text="Post请求"    android:layout_width="match_parent"    android:layout_height="wrap_content" />




.//

MainActivity

package com.example.ok;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.util.Log;import android.view.View;import java.io.IOException;import okhttp3.Call;import okhttp3.Callback;import okhttp3.FormBody;import okhttp3.OkHttpClient;import okhttp3.Request;import okhttp3.Response;public class MainActivity extends AppCompatActivity implements View.OnClickListener {    private static final String TAG = "MainActivity";    private View btnGet;    private View btnPost;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        initview();        setListener();    }    //监听事件    private void setListener(){        btnGet.setOnClickListener(this);        btnPost.setOnClickListener(this);    }    private void initview() {        btnGet =findViewById(R.id.btn_okhttp_get);        btnPost =findViewById(R.id.btn_okhttp_post);    }    @Override    public void onClick(View v) {        switch (v.getId()){            case R.id.btn_okhttp_get:                //1OkHttpClient对象  这两种都可以                //OkHttpClient client=new OkHttpClient();                OkHttpClient client=new OkHttpClient.Builder().build();                //2Request对象                Request request=new Request.Builder()                        .get()//不加get  默认是get                        .url("http://www.wuxirui.com/")                        .build();               //3Call对象                Call call = client.newCall(request);                //4网络请求                call.enqueue(new Callback() {                    @Override                    public void onFailure(Call call, IOException e) {                        Log.e(TAG, "onFailure: "+e.getMessage());                    }                    @Override                    public void onResponse(Call call, Response response) throws IOException {                        Log.e(TAG, "onResponse:"+response.body().string() );                    }                });                break;            //post请求            case R.id.btn_okhttp_post:                //1OkHttpClient对象  这两种都可以                OkHttpClient client2=new OkHttpClient();                //提供post请求需要的body对象                FormBody body=new FormBody.Builder()                        .add("mobile","15910907758")                        .add("password","123456")                        .build();                //Request对象                Request request2=new Request.Builder()                        .post(body)                        .url("http://120.27.23.105/user/login")                        .build();                //4call对象                Call call2 = client2.newCall(request2);                //5进行网络请求                call2.enqueue(new Callback() {                    @Override                    public void onFailure(Call call, IOException e) {                        Log.e(TAG, "onFailure: "+e.getMessage());                    }                    @Override                    public void onResponse(Call call, Response response) throws IOException {                        Log.i(TAG, "onResponse: "+response.body().string());                    }                });                break;        }    }}