gpt4 book ai didi

android - 改造响应在 Android 中返回 null

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:29:52 25 4
gpt4 key购买 nike

我想显示来自 JSON 值的文本。我正在使用 Retrofit 进行 API 调用 我不知道我做的是否正确。不管怎样,这是我的代码。

这是 url 链接:http://api.icndb.com/jokes/random .

网站每次都会显示一个随机笑话。以下是网站输出的示例:

{ "type": "success", "value": { "id": 175, "joke": "When Chuck Norris was a baby, he didn't suck his mother's breast. His mother served him whiskey, straight out of the bottle.", "categories": [] } } 

fragment .java

    String url = "http://api.icndb.com/";

Retrofit.Builder builder = new Retrofit.Builder()
.baseUrl(url)
.addConverterFactory(GsonConverterFactory.create());

Retrofit retrofit = builder.build();

RetroFitHelper client = retrofit.create(RetroFitHelper.class);
Call<Api> call = client.findJoke("random");

call.enqueue(new Callback<Api>() {
@Override
public void onResponse(Response<Api> response, Retrofit retrofit) {

String result = response.body().getJoke();

Toast.makeText(getContext()," The word is: " + result ,Toast.LENGTH_LONG).show();
}

@Override
public void onFailure(Throwable t) {
Toast.makeText(getContext()," Error..." ,Toast.LENGTH_LONG).show();

}
});

RetroFitHelper.java

public interface RetroFitHelper {

@GET("/jokes/{random}")
Call<Api> findJoke(@Path("random") String random);

}

模型类

public class Api {

@SerializedName("joke")
private String joke;

public String getJoke() {
return joke;
}

public void setJoke(String joke){
this.joke = joke;
}
}

最佳答案

提供的 json 响应有一个名为 value 的 json 对象在另一个 json 对象中(它的形式是 {..,"value":{}} )。所以我们需要两个模型类——一个用于 outer json object另一个用于 inner json object (值(value))。

你需要有两个像这样的模型类

public class Api {

@SerializedName("type")
@Expose
private String type;
@SerializedName("value")
@Expose
private Value value;

public String getType() {
return type;
}

public void setType(String type) {
this.type = type;
}

public Value getValue() {
return value;
}

public void setValue(Value value) {
this.value = value;
}

和下面的值对象

public class Value {

@SerializedName("id")
@Expose
private Integer id;
@SerializedName("joke")
@Expose
private String joke;
@SerializedName("categories")
@Expose
private List<Object> categories = null;

public Integer getId() {
return id;
}

public void setId(Integer id) {
this.id = id;
}

public String getJoke() {
return joke;
}

public void setJoke(String joke) {
this.joke = joke;
}

public List<Object> getCategories() {
return categories;
}

public void setCategories(List<Object> categories) {
this.categories = categories;
}

}

现在,response.body()结果为 outer json object(Api)response.body().getValue()将得到 inner json object(Value) 的结果.

现在在你的响应回调中,得到这样的响应

String result = response.body().getValue().getJoke();

Also make sure that you have the necessary internet permission declared in your manifest like this

<uses-permission android:name="android.permission.INTERNET" />

Do ensure you have the latest dependencies setup in your app level build.gradle file

implementation 'com.squareup.retrofit2:retrofit:2.4.0'

implementation 'com.squareup.retrofit2:converter-gson:2.4.0'

关于android - 改造响应在 Android 中返回 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47949874/

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