同步异步获取发送OkHttp

`package mini.org.mokhttp;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.textclassifier.TextLinks;
import org.jetbrains.annotations.NotNull;
import java.io.IOException;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.FormBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import okhttp3.ResponseBody;

public class MainActivity extends AppCompatActivity {

private static final String TAG = "MainActivity";
private OkHttpClient okHttpClient;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    okHttpClient = new OkHttpClient();
}

public void aGet(View view) {
    new Thread(new Runnable() {
        @Override
        public void run() {
            try {
                Request request = new Request.Builder().url("https://www.httpbin.org/get?a=11&b=22").build();
                Call call = okHttpClient.newCall(request);
                Response response = call.execute();    //同步请求,阻塞线程
                Log.i(TAG,"aGet:"+response.body().string());
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }).start();
}

public void bGet(View view) {
    Request request = new Request.Builder().url("https://www.httpbin.org/get?a=11&b=22").build();
    Call call = okHttpClient.newCall(request);
    call.enqueue(new Callback() {   //异步请求
        @Override
        public void onFailure(@NotNull Call call, @NotNull IOException e) {

        }
        @Override
        public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
             if(response.isSuccessful()){
                 Log.i(TAG,"bGet:"+response.body().string());
             }
        }
    });
}

public void cPost(View view) {
    new Thread(){
        @Override
        public void run() {
            super.run();
            FormBody formBody = new FormBody.Builder().add("a","1").add("b","3").build();
            Request build = new Request.Builder().url("https://www.httpbin.org/post").post(formBody).build();
            Call call = okHttpClient.newCall(build);
            try {
                Response execute = call.execute();
                Log.i(TAG,"同步post:"+execute.body().string());
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }.start();
}

public void dPost(View view) {
    FormBody formBody = new FormBody.Builder().add("a","33").add("b","99").build();
    Request request = new Request.Builder().url("https://www.httpbin.org/post").post(formBody).build();
    Call call = okHttpClient.newCall(request);
     call.enqueue(new Callback() {
        @Override
        public void onFailure(@NotNull Call call, @NotNull IOException e) {

        }

        @Override
        public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
            if(response.isSuccessful()){
                Log.i(TAG,"异步post:"+response.body().string());
            }
        }
    });

}

}`

记得添加网络权限

上一篇:linux常用命令


下一篇:ActiveJ框架学习——Async I/O之promise