AndroidRetrofit

本文介绍如何使用Retrofit结合OkHttp实现RESTful API请求。通过具体示例展示了GET和POST请求的方法,并介绍了动态代理的核心思想及依赖配置。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一个基于 OkHttp 的 RESTful API 请求工具

Retrofit 在使用时其实就充当了一个适配器(Adapter)的角色,主要是将一个 Java 接口翻译成一个 HTTP 请求对象,然后用 OkHttp 去发送这个请求

核心思想:动态代理—通俗来讲,就是你要执行某个操作的前后需要增加一些操作,比如查看用户个人信息前需要判断用户是否登录,用户访问数据库后想清除用户的访问记录等操作

添加依赖:
implementation ‘com.squareup.retrofit2:retrofit:2.2.0’
implementation ‘com.squareup.retrofit2:converter-gson:2.2.0’
1
2
添加网络权限

1
需要调用的接口:
package com.example.workday12;

import java.util.Map;

import retrofit2.Call;
import retrofit2.http.Field;
import retrofit2.http.FormUrlEncoded;
import retrofit2.http.GET;
import retrofit2.http.POST;
import retrofit2.http.Path;
import retrofit2.http.QueryMap;
import retrofit2.http.Url;

public interface NetDataInterFace {
//http://www.qubaobei.com/ios/cf/dish_list.php?stage_id=1&limit=20&page=1
@GET(“ios/cf/{path}”)
Call getData(@Path(“path”)String path, @QueryMap Map<String,String> map);

@FormUrlEncoded
@POST("ios/cf/{path}")
Call<JavaBean> getDataByPOST(@Path("path")String path, @Field("stage_id")String stage_id,@Field("limit")String limit,@Field("page")String page);

}

// An highlighted block
 /**
     * get请求
     */
    private void HttpToGet() {
        //http://www.qubaobei.com/ios/cf/dish_list.php?stage_id=1&limit=20&page=1
        Map<String,String> map = new HashMap<>();
        map.put("stage_id","1");
        map.put("limit","20");
        map.put("page","1");
        new Retrofit.Builder()
                .baseUrl(baseUrl)
                .addConverterFactory(GsonConverterFactory.create())
                .build()
                .create(NetDataInterFace.class)
                .getData("dish_list.php",map)
                .enqueue(new Callback<JavaBean>() {
                    @Override
                    public void onResponse(Call<JavaBean> call, Response<JavaBean> response) {
                        JavaBean body = response.body();
                        for (int i = 0; i < body.getData().size();i++){
                            String s = body.getData().get(i).toString();
                            Log.e("###",s);
                        }
                    }

                    @Override
                    public void onFailure(Call<JavaBean> call, Throwable t) {

                    }
                });
    }

// An highlighted block
post方法获取字符串:
/**
     * post请求
     */
    private void HttpToPost() {
        new Retrofit.Builder()
                .baseUrl(baseUrl)
                .addConverterFactory(GsonConverterFactory.create())
                .build()
                .create(NetDataInterFace.class)
                .getDataByPOST("dish_list.php","1","20","1")
                .enqueue(new Callback<JavaBean>() {
                    @Override
                    public void onResponse(Call<JavaBean> call, Response<JavaBean> response) {
                        JavaBean body = response.body();
                        for (int i = 0; i < body.getData().size();i++){
                            String s = body.getData().get(i).toString();
                            Log.e("###",s);
                        }
                    }

                    @Override
                    public void onFailure(Call<JavaBean> call, Throwable t) {

                    }
                });
    }

在这里插入图片描述

// An highlighted block
package com.example.day11_fresco;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    private Button fresco;
    private Button butter;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
    }

    private void initView() {
        fresco = findViewById(R.id.main_fresco);
        butter = findViewById(R.id.main_butterknife);
        fresco.setOnClickListener(this);
        butter.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        Intent intent = new Intent();
        switch (v.getId()){
            case R.id.main_fresco:
                intent.setClass(this,FrescoActivity.class);
                        break;
            case R.id.main_butterknife:
                intent.setClass(this,ButterknifeActivity.class);
                break;
        }
        startActivity(intent);
    }
}

和

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值