今天学的,就是okhttp3, *** 信息的传递方式,一共四种,同步get和post,异步get和post
首先,在这个文件里选择build.gradle
然后在最后添加 implementation 'com.squareup.okhttp3:okhttp:4.9.0',然后在
AndroidMainfest.xml里添加一句<uses-permission android:name="android.permission.INTERNET"/>。
剩下的,就是修改activity_main.xml,添加4个按钮。
<?xml version="1.0" encoding="utf-8"?> <LinearLayout 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" android:orientation="vertical" tools:context=".MainActivity"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Hello World!" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent" app:layout_constraintTop_toTopOf="parent" /> <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="getsync" android:text="get同步请求" /> <Button android:id="@+id/button2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="getasync" android:text="get异步请求" /> <Button android:id="@+id/button3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="postsync" android:text="post同步请求" /> <Button android:id="@+id/button4" android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="postasync" android:text="post异步请求" /> </LinearLayout>
再修改MainActivity,把4个onClick事件补全。
public class MainActivity extends AppCompatActivity { private static final String TAG = "hehe"; private OkHttpClient okHttpClient; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); okHttpClient=new OkHttpClient(); } //同步 *** 请求,有可能因为 *** 延时而阻塞,所以必须要new一个新线程 public void getsync(View view) { new Thread() { @Override public void run(){ //创建一个请求 Request request = new Request.Builder().url("https://www.httpbin.org/get?a=1&b=2").build(); //创建一个呼叫 Call call=okHttpClient.newCall(request); try { //如果有反应就得到一个呼叫的反应 Response response=call.execute(); Log.i(TAG, "getsync: "+response.body().string() ); } catch (IOException e) {//如果没有反应就报错 e.printStackTrace(); } } }.start(); } //异步 *** 请求,传出了数据,就都交给系统去处理了,所以不用重新开个线程 public void getasync(View view) { //创建一个请求 Request request = new Request.Builder().url("https://www.httpbin.org/get?a=1&b=2").build(); //创建一个呼叫 Call call=okHttpClient.newCall(request); //让这个呼叫开始在系统里排队,当系统有反应时,通过一个回调函数来执行,这里call对象会在内部创建一个子线程 //专门等待系统反应 call.enqueue(new Callback() { @Override//这个函数是当无法连接到服务器以及之前的步骤 public void onFailure(@NonNull Call call, @NonNull IOException e) { } @Override//当连接到服务器之后,就会回调这个函数,但是,这个函数只是指连接上服务器了,并不代表程序运行成功。 //当参数错了,或者其它原因,连上服务器,结果什么事都没有做,也是回调这个函数。 public void onResponse(@NonNull Call call, @NonNull Response response) throws IOException { //所以这里如判断一下,响应是否成功! if(response.isSuccessful()){ Log.e(TAG, "onResponse: "+response.body().string()); } } }); } public void postsync(View view) { new Thread(){ @Override public void run() { super.run(); FormBody formBody=new FormBody.Builder() .add("a","1") .add("b","2") .build(); //创建一个请求 Request request = new Request.Builder().url("https://www.httpbin.org/post") .post(formBody).build(); //创建一个呼叫 Call call=okHttpClient.newCall(request); try { //如果有反应就得到一个呼叫的反应 Response response=call.execute(); Log.i(TAG, "postsync: "+response.body().string() ); } catch (IOException e) {//如果没有反应就报错 e.printStackTrace(); } } }.start(); } public void postasync(View view) { FormBody formBody=new FormBody.Builder() .add("a","1") .add("b","2") .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(@NonNull Call call, @NonNull IOException e) { } @Override public void onResponse(@NonNull Call call, @NonNull Response response) throws IOException { //这里如判断一下,响应是否成功! if(response.isSuccessful()){ Log.e(TAG, "Asynchronous+onResponse : "+response.body().string()); } } }); } }