retrofit2使用

文章目录

      • 请求参数:
      • 演示:

Retrofit 是一个 RESTful 的 HTTP 网络请求框架的封装

请求参数:

@FormUrlEncoded
表示发送form-encoded的数据,每个键值对需要用@Filed来注解键名,随后的对象需要提供值

@Field
发送 Post请求 时提交请求的表单字段,与 @FormUrlEncoded 注解配合使用

@Query
用于 @GET 方法的查询参数(Query = Url 中 ‘?’ 后面的 key-value)

演示:

retrofit依赖:

implementation 'com.squareup.retrofit2:retrofit:2.9.0'
// gson转换器
implementation 'com.squareup.retrofit2:converter-gson:2.9.0'

添加网络权限:

<uses-permission android:name="android.permission.INTERNET" />

接口WeatherApi:

public interface WeatherApi {
     
    @POST("/v3/weather/weatherInfo")
    @FormUrlEncoded // 表示发送form-encoded的数据,每个键值对需要用@Filed来注解键名,随后的对象需要提供值
    Call<ResponseBody> postWeather(@Field("city") String city, @Field("key") String key);

    @GET("/v3/weather/weatherInfo")
    Call<BeanWeatherInfo> getWeather(@Query("city") String city, @Query("key") String key);
}

get和post按钮点击触发:

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="get请求"
        android:onClick="get"
        />
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="post请求"
        android:onClick="post"
        />

MainActivity:

public class MainActivity extends AppCompatActivity {
     

    WeatherApi weatherApi;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
     
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl("https://restapi.amap.com") // 设置网络请求的url地址
                .addConverterFactory(GsonConverterFactory.create()) // 设置使用Gson解析
                .build();
        weatherApi = retrofit.create(WeatherApi.class);
    }

    public void get(View view){
     
        //  返回的body类型 将返回的内容转成了BeanWeatherInfo
        Call<BeanWeatherInfo> call = weatherApi.getWeather("110101", "xxxxxxxxxxxxxx");
        call.enqueue(new Callback<BeanWeatherInfo>() {
     
            @Override
            public void onResponse(Call<BeanWeatherInfo> call, Response<BeanWeatherInfo> response) {
     
                if (response.isSuccessful()){
     
                    BeanWeatherInfo body = response.body();
                    String info = body.getInfo();
                    Log.d("AAAAAAAAAAAAA", "onResponse: info:"+info);
                }
            }
            @Override
            public void onFailure(Call<BeanWeatherInfo> call, Throwable t) {
     
                Writer result = new StringWriter();
                PrintWriter printWriter = new PrintWriter(result);
                t.printStackTrace(printWriter);
                Log.e("AAAAAAAAAAAAA", "onFailure: "+result.toString());
            }
        });
    }

    public void post(View view){
     
        Call<ResponseBody> call = weatherApi.postWeather("110101", "xxxxxxxxxxxxxx");
        call.enqueue(new Callback<ResponseBody>() {
     
            @Override
            public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
     
                if (response.isSuccessful()){
     
                    ResponseBody body = response.body();
                    try {
     
                        String string = body.string();
                        Log.d("AAAAAAAAAAAAA", "onResponse: "+string);
                    } catch (IOException e) {
     
                        e.printStackTrace();
                    } finally {
     
                        body.close();
                    }
                }
            }
            @Override
            public void onFailure(Call<ResponseBody> call, Throwable t) {
     
            }
        });
    }
}

你可能感兴趣的:(Android)