gpt4 book ai didi

java - 应为 BEGIN_OBJECT,但在第 13 行第 1 列路径 $ 处为 STRING

转载 作者:太空宇宙 更新时间:2023-11-04 11:21:46 24 4
gpt4 key购买 nike

这个问题我见过很多次了,但还是不明白。看来我在网站上发送了请求,但其正文不正确。但为什么?可能我不太明白改造是如何工作的,但我不只是收集请求的链接并等待服务器的答复吗?这是链接:here

带有请求的接口(interface)

public interface NService {
@GET("/computers?p=2")
Call<Model> getItems();
}

以及带有基本 URL 的类

public class APIUtils {

public static final String BASE_URL = "http://testwork.nsd.naumen.ru/rest/";

public static NService getMService() {
return RetrofitClient.getClient(BASE_URL).create(NService.class);
}
}

改造构建类

public class RetrofitClient {

private static Retrofit retrofit = null;


public static Retrofit getClient(String baseURL) {
if (retrofit==null) {
Gson gson = new GsonBuilder()
.setLenient()
.create();
retrofit = new Retrofit.Builder()
.baseUrl(baseURL)
.addConverterFactory(GsonConverterFactory.create(gson))
.build();
}
return retrofit;
}
}

JSON 已被解析 this

MainActivity 是:

public class MainActivity extends AppCompatActivity {
private List<Item> responseModel;
private NService mService;
private TextView textView;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.d("testcrap", "before loading set");
setContentView(R.layout.activity_main);
mService = APIUtils.getMService(); // Строим retrofit объект, собираем ссылку
Log.d("testcrap", "before loading");
loadSomeCrap();
Log.d("testcrap", "after loading");
}

public void loadSomeCrap() {
Log.d("testcrap", "started parsing");
mService.getItems().enqueue(new Callback<Model>() {

@Override
public void onResponse(Call<Model> call, Response<Model> response) {
Log.d("testcrap", "started onResponse");

if(response.isSuccessful()) {
Log.d("testcrap", "posts loaded from API");
}else {
Log.d("testcrap", "posts not loaded from API");
}
}

@Override
public void onFailure(Call<Model> call, Throwable t) {
//showErrorMes sage();
Log.d("testcrap", t.toString());

}
});
}
}

日志:

before loading set
before loading
started parsing
after loading
com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was STRING at line 13 column 1 path $

那么,如果请求错误,我应该如何更改才能正常工作?

最佳答案

进行这些更改..

BASE_URL = "http://testwork.nsd.naumen.ru/"

然后在界面上..

@GET("rest/computers?p=2")
.............

关于java - 应为 BEGIN_OBJECT,但在第 13 行第 1 列路径 $ 处为 STRING,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44821867/

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