写上在说
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
依赖
compile 'com.squareup.okhttp3:okhttp:3.9.1'
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.disanfangdenglu.MainActivity">
<Button
android:id="@+id/button_send"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="发送"/>
</android.support.constraint.ConstraintLayout>
MainActivity
package com.example.disanfangdenglu;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import java.io.IOException;
import java.util.concurrent.TimeUnit;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.FormBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private Button send;
private OkHttpClient okHttpClient;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
//应用拦截器
okHttpClient = new OkHttpClient.Builder()
.readTimeout(15, TimeUnit.SECONDS)
.writeTimeout(20, TimeUnit.SECONDS)
.connectTimeout(20, TimeUnit.SECONDS)
.addInterceptor(new MyInterceptor())
.addInterceptor(new LoggingInterceptor())//应用拦截器
//.addNetworkInterceptor(new LoggingInterceptor())//网络拦截器
.build();
}
private void initView() {
//找到控件
send = findViewById(R.id.button_send);
send.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
default:
break;
case R.id.button_send:
//加参数
Request request = new Request.Builder()
.url("https://www.zhaoapi.cn/user/login")
.build();
okHttpClient.newCall(request).enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
}
@Override
public void onResponse(Call call, Response response) throws IOException {
String string = response.body().string();
}
});
break;
}
}
private void method1() {
Request request = new Request.Builder()
.url("http://www.publicobject.com/helloworld.txt")
.header("User-Agent", "OkHttp Example")
.build();
okHttpClient.newCall(request).enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
}
@Override
public void onResponse(Call call, Response response) throws IOException {
}
});
}
}
MyInterceptor
package com.example.disanfangdenglu;
import java.io.IOException;
import okhttp3.FormBody;
import okhttp3.Interceptor;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;
/**
* 姓名:${user}
* 时间:${date}
*/
public class MyInterceptor implements Interceptor {
@Override
public Response intercept(Chain chain) throws IOException {
//获取原始的Request
Request orginRequest = chain.request();
//获取原始的地址
String url = orginRequest.url().toString();
//拼接地址,添加参数
String resultUrl = url + "?mobile=18310830365&password=123456";
//把拼接好的地址发送请求
Request request = new Request.Builder().url(resultUrl).build();
Response response = chain.proceed(request);
return response;
}
}
LoggingInterceptor
package com.example.disanfangdenglu;
import android.util.Log;
import java.io.IOException;
import okhttp3.Interceptor;
import okhttp3.Request;
import okhttp3.Response;
/**
* 姓名:${user}
* 时间:${date}
*/
public class LoggingInterceptor implements Interceptor {
@Override
public Response intercept(Chain chain) throws IOException {
Request request = chain.request();
long t1 = System.nanoTime();
Log.i("LoggingInterceptor", String.format("Sending request %s on %s%n%s",
request.url(), chain.connection(), request.headers()));
Response response = chain.proceed(request);
long t2 = System.nanoTime();
Log.i("LoggingInterceptor", String.format("Received response for %s in %.1fms%n%s",
response.request().url(), (t2 - t1) / 1e6d, response.headers()));
return response;
}
}