posts - 296,comments - 1,views - 2995

一.所花时间

2h

二.代码量

50行

三.博客量

1篇

四.了解到的知识点

今天学习了android连接数据库的第二种方式:通过web中间层连接。

需要用到的工具是retrofit

首先需要引入retrofit的依赖:

在app目录下的bulid.gradle.kts的dependencies代码块中添加一句话

implementation 'com.squareup.retrofit2:retrofit:2.0.2'
然后就是代码部分
package com.leap.homework2.util;

import android.util.Log;

import androidx.annotation.NonNull;

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

public class RetrofitUtil {
    private static final String IP = "10.99.118.64";
    private static final String URL ="http://"+IP+":8080" ;

    public static Retrofit getRetrofit() {

        //日志显示级别
        HttpLoggingInterceptor.Level level= HttpLoggingInterceptor.Level.BODY;
        //新建log拦截器
        HttpLoggingInterceptor loggingInterceptor=new HttpLoggingInterceptor(new HttpLoggingInterceptor.Logger() {
            @Override
            public void log(@NonNull String message) {
                Log.d("RetrofitMessage","OkHttp====Message:"+message);
            }
        });
        loggingInterceptor.setLevel(level);
        //定制OkHttp
        OkHttpClient.Builder httpClientBuilder = new OkHttpClient.Builder();
        //OkHttp进行添加拦截器loggingInterceptor
        httpClientBuilder.addInterceptor(loggingInterceptor);

       return  new Retrofit.Builder()
                .baseUrl(URL)
                .addConverterFactory(GsonConverterFactory.create())
                .client(httpClientBuilder.build())
                .build();

    }

}
package com.leap.homework2.api;

import com.leap.homework2.util.Result;
import com.leap.homework2.entity.*;

import java.util.List;

import retrofit2.Call;
import retrofit2.http.GET;
import retrofit2.http.Query;

public interface TransitTracer {


    @GET("/transitTracer/getAllLines")
    Call<Result<List<Station>>> getAllLines();

    @GET("/transitTracer/getAllStationOnLine")
    Call<Result<List<Station>>> getAllStationOnLine(@Query("lineName") String lineName);

    @GET("/transitTracer/getLine")
    Call<Result<List<Line>>> getLine(@Query("stationName") String stationName);

    @GET("/transitTracer/getShortestPath")
    Call<Result<List<Station>>> getShortestPath(@Query("startStationName") String startStationName, @Query("endStationName") String endStationName);


}
TransitTracer transitTracer = RetrofitUtil.getRetrofit().create(TransitTracer.class);

transitTracer.getAllStationOnLine(lineName).enqueue(new Callback<Result<List<Station>>>() {
    @Override
    public void onResponse(@NonNull Call<Result<List<Station>>> call, @NonNull Response<Result<List<Station>>> response) {
        assert response.body() != null; //断言语句,确保response.body() != null 条件为真。如果条件为假,则会抛出一个 AssertionError 异常.
        stations = response.body().getData();
        List<String> list = new ArrayList<>();
        for(int i=0;i<stations.size();i++){
            list.add(stations.get(i).toString());
        }
        ArrayAdapter<String> arrayAdapter= new ArrayAdapter<>(QueryLineActivity.this, android.R.layout.simple_list_item_1,list);

        lv_QueryLine.setAdapter(arrayAdapter);
    }

    @Override
    public void onFailure(@NonNull Call<Result<List<Station>>> call, @NonNull Throwable t) {
        System.out.println("请求失败");
        //返回错误信息
        System.out.println(t.getMessage());
    }
});
posted on   leapss  阅读(4)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 零经验选手,Compose 一天开发一款小游戏!
· 因为Apifox不支持离线,我果断选择了Apipost!
· 通过 API 将Deepseek响应流式内容输出到前端
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

点击右上角即可分享
微信分享提示