在Android中改进返回null的响应
我想显示来自在Android中改进返回null的响应,android,retrofit,Android,Retrofit,我想显示来自JSON值的文本。我正在使用reformation进行API调用,我不知道我是否做对了。不管怎样,这是我的密码 以下是url链接: 网站每次都会随机显示一个笑话。以下是网站输出的示例: { "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, st
JSON
值的文本。我正在使用reformation
进行API
调用,我不知道我是否做对了。不管怎样,这是我的密码
以下是url链接:
网站每次都会随机显示一个笑话。以下是网站输出的示例:
{ "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();
}
});
public interface RetroFitHelper {
@GET("/jokes/{random}")
Call<Api> findJoke(@Path("random") String random);
}
提供的json响应在另一个json对象中有一个名为
value
的json对象(其形式为{..,“value”:{}}
)。因此,我们需要两个模型类—一个用于外部json对象
,另一个用于内部json对象
(值)
您需要有两个这样的模型类
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;
}
String result = response.body().getValue().getJoke();
下面是一个值对象
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;
}
}
还要确保您在清单中声明了必要的internet权限,如下所示
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;
}
String result = response.body().getValue().getJoke();
请确保在应用程序级别build.gradle
文件中设置了最新的依赖项
implementation'com.squareup.reformation2:reformation:2.4.0'
implementation'com.squareup.reformation2:converter gson:2.4.0'
它们是您正在使用的模型类中的一个问题
api的回应是:
{
"type": "success",
"value": {
"id": 429,
"joke": "Chuck Norris has never been accused of murder because his roundhouse kicks are recognized as "acts of God."",
"categories": []
}
}
所以,为了从作为字符串的响应中获得笑话的价值,您的模型类应该如下所示:
public class Api implements Serializable {
@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;
}
}
call1.enqueue(new Callback<Api>() {
@Override
public void onResponse(Call<Api> call, Response<Api> response) {
if (response.isSuccessful()) {
String jokeValue = response.body().getValue().getJoke();
}
}
@Override
public void onFailure(Call<Api> call, Throwable t) {
t.printStackTrace();
}
});
您的价值等级是:
public class Value implements Serializable
{
@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;
}
}
公共类值实现可序列化
{
@序列化名称(“id”)
@暴露
私有整数id;
@序列化名称(“笑话”)
@暴露
私人串笑话;
@序列化名称(“类别”)
@暴露
私有列表类别=空;
公共整数getId(){
返回id;
}
公共无效集合id(整数id){
this.id=id;
}
公共字符串getJoke(){
返回笑话;
}
公共笑话(字符串笑话){
这个笑话;
}
公共列表getCategories(){
退货类别;
}
公共类别(列出类别){
这个。类别=类别;
}
}
现在你可以像这样获得你的价值:
public class Api implements Serializable {
@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;
}
}
call1.enqueue(new Callback<Api>() {
@Override
public void onResponse(Call<Api> call, Response<Api> response) {
if (response.isSuccessful()) {
String jokeValue = response.body().getValue().getJoke();
}
}
@Override
public void onFailure(Call<Api> call, Throwable t) {
t.printStackTrace();
}
});
call1.enqueue(新回调(){
@凌驾
公共void onResponse(调用、响应){
if(response.issusccessful()){
字符串jokeValue=response.body().getValue().getJoke();
}
}
@凌驾
失败时公共无效(调用调用,可丢弃的t){
t、 printStackTrace();
}
});
根据您的要求进行更改
愉快的编码。正如@Aswin建议和@Navneet回答的那样,您在POJO课程中遇到了问题。
我建议你下次使用左右,避免卡这种错误
台阶
1) 去
2) 将您的响应粘贴到那里,然后输入包和类名
3) 选择目标语言为Java或Kotlin(如果您正在使用)
4) 源类型为Json
5) 注释样式为Gson
6) 单击预览
7) 将这些类复制并粘贴到您的应用程序包中在您的AndroidManifest.xml中添加此权限
<uses-permission android:name="android.permission.INTERNET" />
添加此接口
public interface ApiInterface {
public static final String BASE_URL = "http://api.icndb.com";
@GET("/jokes/{random}")
Call<Api> GetApiResponse(@Path("random") String random);
public class ApiClient {
public static ApiInterface apiInterface;
public static ApiInterface getApiInterface() {
if (apiInterface == null) {
Retrofit retrofit = new Retrofit.Builder().baseUrl(BASE_URL).addConverterFactory(GsonConverterFactory.create()).build();
apiInterface = retrofit.create(ApiInterface.class);
return apiInterface;
} else {
return apiInterface;
}
}
}
}
创建值模型类:com.example.app.Value.java
package com.example.app;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
public class Api {
@SerializedName("type")
@Expose
private String type;
@SerializedName("value")
@Expose
private Value value;
public String getType() {
return type;
}
public Value getValue() {
return value;
}
}
package com.example.app;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
public class Value {
@SerializedName("id")
@Expose
private Integer id;
@SerializedName("joke")
@Expose
private String joke;
public int getId() {
return id;
}
public String getJoke() {
return joke;
}
}
将此代码添加到活动onCreate()
方法中
ApiInterface.ApiClient.getApiInterface().GetApiResponse('random').enqueue(new Callback<Api>() {
@Override
public void onResponse(Call<Api> call, Response<Api> response) {
String Joke=response.body.getValue().getJoke();
Toast.makeText(MainActivity.this, Joke, Toast.LENGTH_SHORT).show();
}
@Override
public void onFailure(Call<Api> call, Throwable t) {
}
});
ApiInterface.ApiClient.getApiInterface().GetApiResponse('random').enqueue(新回调(){
@凌驾
公共void onResponse(调用、响应){
String Joke=response.body.getValue().getJoke();
Toast.makeText(MainActivity.this、笑话、Toast.LENGTH_SHORT).show();
}
@凌驾
失败时公共无效(调用调用,可丢弃的t){
}
});
确保您已在中添加了INTERNET权限,但该权限已启用。您的模型类似乎不太正确。看看纳瓦内特的回答:这个网站真的很有帮助,非常感谢。非常有魅力,谢谢。我有一个快速的问题,如何在GET请求发送后输出完整的URL?换句话说,我只想输出完整的URL,以防onResponse出现错误。再次感谢。到底是什么错误?假设您的响应未成功,则在if(!response.issucess()){}
回调中添加消息(或要输出的url)作为toast