OkHttp get请求 和post请求

//依赖

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;
        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值