retrofit网络使用

第一步:在脚本文件module.app文件中添加以下代碼:
implementation 'com.squareup.retrofit2:retrofit:2.4.0'     //Retrofit2所需要的包
implementation 'com.squareup.retrofit2:converter-gson:2.4.0    //ConverterFactory的Gson依赖包

 

第二歩设计布局文件:


    android:layout_width="match_parent"
    android:layout_height="match_parent"
    androidrientation="vertical">

   
        android:id="@+id/et01"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

   
        android:id="@+id/btn01"
        android:text="tijiao"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

   
        android:id="@+id/tv01"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

 

第三歩在代码文件中关联布局文件中的控件:
package com.lxm.bfdl;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import java.io.IOException;

import okhttp3.ResponseBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;

public class Main extends AppCompatActivity {

    EditText et02;
    Button btn02;
    TextView tv02;
    String str01;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        et02 = findViewById(R.id.et01);
        btn02 = findViewById(R.id.btn01);
        tv02 = findViewById(R.id.tv01);

        btn02.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //str01 = et02.getText().toString();
                //tv02.setText(str01);
                Retrofit retrofit = new Retrofit.Builder()
                        .baseUrl("https://api.douban.com/v2/")
                        .build();
                Tomcat01 tomcat01 = retrofit.create(Tomcat01.class);
                Call call = tomcat01.getStr();
                call.enqueue(new Callback() {
                    @Override
                    public void onResponse(Call call, Response response) {
                        try {
                            tv02.setText(response.body().string());
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }

                    @Override
                    public void onFailure(Call call, Throwable t) {
                        tv02.setText("出错了!");
                    }
                });
            }
        });

    }
}

 

第四歩写业务代码,接口文件
package com.lxm.bfdl;

import okhttp3.ResponseBody;
import retrofit2.Call;
import retrofit2.http.GET;

public interface Tomcat01 {
@GET("book/1220562")
    Call getStr();
}

 

第五步检察清单文件的权限


    package="com.lxm.bfdl">
   
   
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        
            
               

               
            
        
   

你可能感兴趣的:(安卓)