gpt4 book ai didi

android - 一切都很好,但是调用接口(interface)方法 Retrofit 时出现错误,而不是显示折线

转载 作者:行者123 更新时间:2023-12-02 18:54:55 27 4
gpt4 key购买 nike

i think i do some mistake here

Retrofit

import retrofit2.Retrofit;
import retrofit2.converter.scalars.ScalarsConverterFactory;

public class RetrofitClient {
private static Retrofit retrofit =null;

public static Retrofit getClient(String baseURL){
if (retrofit == null){
retrofit= new Retrofit.Builder().baseUrl(baseURL).addConverterFactory(ScalarsConverterFactory.create()).build();
}return retrofit;
}
}

for Retrofit url calling

public class Common {
public static final String baseUrl ="https://maps.googleapis.com";
public static IGoogleApi getGoogleAPI(){
return RetrofitClient.getClient(baseUrl).create(IGoogleApi.class);
}
}

for Retrofit call

package com.google.rishicab.Romote;
import retrofit2.Call;
import retrofit2.http.GET;
import retrofit2.http.Url;

public interface IGoogleApi {
@GET
Call<String> getPath(@Url String url);
}

我认为我在改造时做错了什么,但找不到错误。

logcat

createProgram 0x0000000800000000, binary 0x70f41ba6a3, length 10736, format 37168 within 518ns 
D/Gobinda: karnojora
D/GOBINDA: https://maps.googleapis.com/maps/api/directions/json?mode=driving&transit_routing_preference=less_driving&origin=25.630044,88.1317314&destination=karnojora&key=AIzaSyDa-hkY6VKPVyhqpr********mw
W/System.err: java.lang.NullPointerException: Attempt to invoke interface method 'retrofit2.Call com.google.rishicab.Romote.IGoogleApi.getPath(java.lang.String)' on a null object reference
W/System.err: at com.google.rishicab.MapsActivity.getDirection(MapsActivity.java:225)

最佳答案

就我而言,我在没有 Common 类的情况下运行

public class RetrofitClient {
private static Retrofit retrofit =null;
public static Retrofit getClient() {
if(null == retrofit) {
String baseURL = "https://maps.googleapis.com/";
retrofit = new Retrofit.Builder()
.addConverterFactory(ScalarsConverterFactory.create())
.client(getOkHttpClient())
.baseUrl(baseURL)
.build();
}
//return retrofit object
return retrofit;
}
}

onCreate后初始化

mServices= RetrofitClient.getClient().create(IGoogleApi.class);

关于android - 一切都很好,但是调用接口(interface)方法 Retrofit 时出现错误,而不是显示折线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61412515/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com