前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >androidのretrofit2调用接口

androidのretrofit2调用接口

作者头像
阿超
发布2022-08-16 16:59:12
4310
发布2022-08-16 16:59:12
举报
文章被收录于专栏:快乐阿超

所谓理解,通常不过是误解的总合。——村上春树《斯普特尼克恋人》

安卓调用接口

首先引入依赖

代码语言:javascript
复制
implementation 'com.android.volley:volley:1.1.1'
implementation 'com.squareup.retrofit2:retrofit:2.9.0'
implementation 'com.squareup.retrofit2:converter-gson:2.1.0'
implementation 'com.squareup.okhttp3:logging-interceptor:3.3.0'
implementation 'com.squareup.okhttp3:okhttp-urlconnection:3.3.0'

然后编写主配置类

代码语言:javascript
复制
package com.example.interfacecall.net;

import android.util.Log;

import com.android.volley.BuildConfig;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import java.util.concurrent.TimeUnit;

import javax.net.ssl.SSLContext;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;

import okhttp3.OkHttpClient;
import okhttp3.logging.HttpLoggingInterceptor;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;


public class NetConfig {
    /**
     * url
     */
    public final static String BASEURL = "http://vampireachao.utools.club/";

    private static Retrofit.Builder builder;
    private static Retrofit retrofit;

    public static Retrofit.Builder getRetrofitBuilder() {
        if (builder == null) {
            Gson gson = new GsonBuilder()
                    .setDateFormat("yyyy-MM-dd'T'HH:mm:ssZ")
                    .setLenient()
                    .create();//使用 gson coverter,统一日期请求格式
            HttpLoggingInterceptor logging = new HttpLoggingInterceptor(message -> Log.e("OkHttp", ": " + message));
            logging.setLevel(HttpLoggingInterceptor.Level.BODY);
            OkHttpClient.Builder okHttpBuilder = new OkHttpClient.Builder();
            okHttpBuilder
                    .readTimeout(15000, TimeUnit.MILLISECONDS)
                    .connectTimeout(15000, TimeUnit.MILLISECONDS)
                    .writeTimeout(15000, TimeUnit.MILLISECONDS)
                    .retryOnConnectionFailure(true);
            if (BuildConfig.DEBUG) {
                okHttpBuilder.addInterceptor(logging);
            }
            OkHttpClient httpClient = okHttpBuilder.build();

            builder = new Retrofit.Builder()
                    .addConverterFactory(GsonConverterFactory.create(gson))
                    .client(httpClient)
                    .baseUrl(BASEURL);
        }
        return builder;
    }

    public static Retrofit getRetrofit() {
        if (retrofit == null) {
            retrofit = getRetrofitBuilder().build();
        }
        return retrofit;
    }

    public static <T> T create(Class<T> t) {
        return getRetrofit().create(t);
    }
    

}

以及自定义返回处理

代码语言:javascript
复制
package com.example.interfacecall.net;

import android.util.Log;

import com.google.gson.Gson;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public abstract class CustomCallBack<T> implements Callback<T> {

    @Override
    public void onResponse(Call<T> call, Response<T> response) {
        Log.e("okhttp: content_result ", new Gson().toJson(response.body()));
        if (response.raw().code() == 500) {
            failure(new Throwable("500"));
            return;
        }
        if (response.body() == null) {
            failure(new Throwable("500"));
            return;
        }
        response(response);
    }

    @Override
    public void onFailure(Call<T> call, Throwable t) {
        t.printStackTrace();
        failure(t);
    }

    public abstract void response(Response<T> response);

    public abstract void failure(Throwable t);
}

定义接口

代码语言:javascript
复制
package com.example.interfacecall.net;

import com.example.interfacecall.bean.User;

import retrofit2.Call;
import retrofit2.http.Body;
import retrofit2.http.GET;
import retrofit2.http.POST;
import retrofit2.http.Path;
import retrofit2.http.Query;

public class ProjectApi {
    public interface UserProject {
        @GET("user/shout")
        Call<AjaxJson> shout();

        /**
         * user/say?word=
         * @param word
         * @return
         */
        @GET("user/say")
        Call<AjaxJson> say(@Query("word") String word);

        /**
         * user/say/xxx
         * @param word
         * @return
         */
        @GET("user/say/{word}")
        Call<AjaxJson> speak(@Path("word") String word);

        /**
         * requestBody里{username:"xxx",password:"xxx"}
         * @param user
         * @return
         */
        @POST("user/login")
        Call<AjaxJson> login(@Body User user);

    }
}

然后调用

代码语言:javascript
复制
package com.example.interfacecall;

import android.os.Bundle;

import androidx.appcompat.app.AppCompatActivity;

import com.example.interfacecall.bean.User;
import com.example.interfacecall.net.AjaxJson;
import com.example.interfacecall.net.CustomCallBack;
import com.example.interfacecall.net.NetConfig;
import com.example.interfacecall.net.ProjectApi;
import com.example.interfacecall.utils.ToastUtils;

import java.util.Optional;

import retrofit2.Call;
import retrofit2.Response;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
//        say();
        speak();
//        login();
//        shout();
    }

    private void say() {
        findViewById(R.id.hello).setOnClickListener(v -> {
            Call<AjaxJson> shout = NetConfig.create(ProjectApi.UserProject.class).say("阿巴阿巴阿巴");
            shout.enqueue(new CustomCallBack<AjaxJson>() {
                @Override
                public void response(Response<AjaxJson> response) {
                    ToastUtils.showToast(getApplicationContext(), Optional.ofNullable(response.body()).orElse(new AjaxJson()).getData());
                }

                @Override
                public void failure(Throwable t) {
                    ToastUtils.showToast(getApplicationContext(), "网络异常");
                }
            });
        });
    }

    private void speak() {
        findViewById(R.id.hello).setOnClickListener(v -> {
            Call<AjaxJson> shout = NetConfig.create(ProjectApi.UserProject.class).speak("阿巴阿巴阿巴");
            shout.enqueue(new CustomCallBack<AjaxJson>() {
                @Override
                public void response(Response<AjaxJson> response) {
                    ToastUtils.showToast(getApplicationContext(), Optional.ofNullable(response.body()).orElse(new AjaxJson()).getData());
                }

                @Override
                public void failure(Throwable t) {
                    ToastUtils.showToast(getApplicationContext(), "网络异常");
                }
            });
        });
    }


    private void login() {
        findViewById(R.id.hello).setOnClickListener(v -> {
            User user = new User("rubenHappyAchao", "Ruben8848");
            Call<AjaxJson> shout = NetConfig.create(ProjectApi.UserProject.class).login(user);
            shout.enqueue(new CustomCallBack<AjaxJson>() {
                @Override
                public void response(Response<AjaxJson> response) {
                    ToastUtils.showToast(getApplicationContext(), Optional.ofNullable(response.body()).orElse(new AjaxJson()).getMsg());
                }

                @Override
                public void failure(Throwable t) {
                    ToastUtils.showToast(getApplicationContext(), "网络异常");
                }
            });
        });
    }

    private void shout() {
        findViewById(R.id.hello).setOnClickListener(v -> {
            Call<AjaxJson> shout = NetConfig.create(ProjectApi.UserProject.class).shout();
            shout.enqueue(new CustomCallBack<AjaxJson>() {
                @Override
                public void response(Response<AjaxJson> response) {
                    ToastUtils.showToast(getApplicationContext(), Optional.ofNullable(response.body()).orElse(new AjaxJson()).getMsg());
                }

                @Override
                public void failure(Throwable t) {
                    ToastUtils.showToast(getApplicationContext(), "网络异常");
                }
            });
        });
    }

}

完整安卓代码放到了gitee仓库里,感兴趣的可以自取。。。

接口后端代码

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2020-10-09,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档