改造响应在Android中返回null [英] Retrofit response returning null in Android

查看:95
本文介绍了改造响应在Android中返回null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想显示来自 JSON 值的文本.我正在使用 Retrofit 进行 API 调用,我不知道我是否做对了.无论如何,这是我的代码.

I wanted to display a text from a JSON value. I'm using Retrofit to make API call I don't know if I'm doing it right. Anyway here's my code.

以下是网址链接: http://api.icndb.com/jokes/random .

该网站每次都会显示一个随机笑话.这是网站输出的示例:

The website will display a random joke each time. Here's the example of an output from the website:

{ "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响应在另一个json对象中具有一个名为 value 的json对象(格式为 {..,值":{}} ).因此,我们需要两个模型类-一个用于外部json对象,另一个用于内部json对象(值).

The provided json response has a json object named value inside another json object(It is of the form {..,"value":{}}). So we need two model classes - one for the outer json object and another one for the inner json object(value).

您将需要具有两个这样的模型类

You will need to have two model classes like this

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()的结果code>将具有内部json对象(值)的结果.

Now, response.body() will have the result of outer json object(Api) and response.body().getValue() will have the result for inner json object(Value) .

现在在您的响应回调中,像这样获得响应

Now in your response callback, get response like this

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

还要确保您具有在清单中声明的​​必要互联网许可,例如

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

<使用权限android:name ="android.permission.INTERNET"/>

请确保在您的 应用程序级别 build.gradle 文件

实现'com.squareup.retrofit2:retrofit:2.4.0'

实现'com.squareup.retrofit2:converter-gson:2.4.0'

这篇关于改造响应在Android中返回null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆