我想显示一个JSON值的文本.我使用Retrofit,使API呼叫我不知道如果我这样做是正确的.无论如何这是我的代码.
这是网址链接: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": [] } }
Run Code Online (Sandbox Code Playgroud)
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();
}
});
Run Code Online (Sandbox Code Playgroud)
RetroFitHelper.java
public interface RetroFitHelper {
@GET("/jokes/{random}")
Call<Api> findJoke(@Path("random") String random);
}
Run Code Online (Sandbox Code Playgroud)
模型类
public class Api {
@SerializedName("joke")
private String joke;
public String getJoke() {
return joke;
}
public void setJoke(String joke){
this.joke = joke;
}
}
Run Code Online (Sandbox Code Playgroud)
提供的json响应有一个名为value另一个json对象内部的json对象(它的形式{..,"value":{}}).所以我们需要两个模型类 - 一个用于outer json object另一个用于inner json object(值).
你需要有两个这样的模型类
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;
}
Run Code Online (Sandbox Code Playgroud)
以及价值对象的下一个
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;
}
}
Run Code Online (Sandbox Code Playgroud)
现在,response.body()将有结果outer json object(Api),并response.body().getValue()将会有结果inner json object(Value).
现在在你的响应回调中,得到这样的响应
String result = response.body().getValue().getJoke();
Run Code Online (Sandbox Code Playgroud)
还要确保您在清单中声明了必要的互联网权限
<uses-permission android:name="android.permission.INTERNET" />
确保在文件中设置了最新的依赖项
app levelbuild.gradle
implementation 'com.squareup.retrofit2:retrofit:2.4.0'
implementation 'com.squareup.retrofit2:converter-gson:2.4.0'
正如@Aswin 建议和@Navneet 回答的那样,您的 POJO 课程有问题。我建议您使用jsonschema2pojo或RoboPOJOGenerator以便下次避免陷入此类错误。
脚步
1) 访问http://www.jsonschema2pojo.org/
2) 将您的回复粘贴到那里并输入包和类名
3) 选择目标语言为 Java 或 Kotlin(如果您正在使用)
4) 源类型为 Json
5) 注释样式为 Gson
6) 点击预览
7) 将这些类复制并粘贴到您的应用程序包中
| 归档时间: |
|
| 查看次数: |
9178 次 |
| 最近记录: |