- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
当我调用我的 API 之一时,onRespone 方法返回空 response.body()。我以前多次遵循这种方式并且它有效,但在这种情况下不起作用。此 api 也适用于浏览器。
我检查了所有的东西:SerializedName 标签,从 Serializable 实现所有类,我正在使用的 api 方法,...。我也尝试使用同步调用,但它不起作用。
请注意我是如何从回调中获取 response.body() 值的:我将一个变量声明为 Activity 类字段并将 response.body() 分配给它。
我真的很困惑。有什么想法吗??
这些都是您需要的代码:
Activity :
package com.example.amirmasoud.mgtools_2.activity;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.Toast;
import com.example.amirmasoud.mgtools_2.GetDataService;
import com.example.amirmasoud.mgtools_2.R;
import com.example.amirmasoud.mgtools_2.RetrofitInstance;
import com.example.amirmasoud.mgtools_2.model.ToolsItem;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
public class ToolContentActivity extends AppCompatActivity {
private GetDataService service = RetrofitInstance.getRetrofitInstance().create(GetDataService.class);
private ToolsItem toolItem = new ToolsItem();
private int toolsItemId = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_tool_content);
Bundle bundle = new Bundle(getIntent().getExtras());
toolsItemId = bundle.getInt("toolItemId");
service.getToolItemContent(toolsItemId).enqueue(new Callback<ToolsItem>() {
@Override
public void onResponse(Call<ToolsItem> call, Response<ToolsItem> response) {
toolItem.mConstructor(response.body());
}
@Override
public void onFailure(Call<ToolsItem> call, Throwable t) {
Toast.makeText(getApplicationContext(), t.getMessage(), Toast.LENGTH_LONG).show();
}
});
}
}
型号:
package com.example.amirmasoud.mgtools_2.model;
import com.google.gson.annotations.SerializedName;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
/**
* Created by AmirMasoud on 12/9/2018.
*/
public class ToolsItem implements Serializable {
@SerializedName("MainTypeID")
private int mainTypeID = 0;
@SerializedName("Name")
private String persianName = "";
@SerializedName("ImageURL")
private String imageURL = "";
@SerializedName("EnglishName")
private String englishName = "";
@SerializedName("OtherName")
private String otherName = "";
@SerializedName("CountryID")
private int countryID = 0;
@SerializedName("Year")
private int year = 0;
@SerializedName("ComplexityName")
private String complexityName = "";
@SerializedName("RelatedMainTypeID")
private List<Integer> relatedMainTypeID = new ArrayList<>();
@SerializedName("IntroductionDescription")
private String introductionDescription = "";
@SerializedName("Pros")
private String pros = "";
@SerializedName("Cons")
private String cons = "";
@SerializedName("RequiredTime")
private int requiredTime = 0;
@SerializedName("RequiredEmployee")
private int requiredEmployee = 0;
@SerializedName("RequiredBudget")
private int requiredBudget = 0;
@SerializedName("RequiredKnowledge")
private int requiredKnowledge = 0;
@SerializedName("Applications")
private String applications = "";
@SerializedName("ComplementaryContent")
private List<CompContentToolItem> complementaryContent = new ArrayList<>();
public ToolsItem mConstructor(ToolsItem item) {
this.mainTypeID = item.getMainTypeID();
this.persianName = item.getPersianName();
this.englishName= item.getEnglishName();
this.imageURL = item.getImageURL();
this.otherName = item.getOtherName();
this.countryID = item.getCountryID();
this.year = item.getYear();
this.complexityName = item.getComplexityName();
this.relatedMainTypeID = item.getRelatedMainTypeID();
this.introductionDescription = item.getIntroductionDescription();
this.pros = item.getPros();
this.cons = item.getCons();
this.requiredTime = item.getRequiredTime();
this.requiredBudget = item.getRequiredBudget();
this.requiredEmployee = item.getRequiredEmployee();
this.requiredKnowledge = item.getRequiredKnowledge();
this.applications = item.getApplications();
this.complementaryContent = item.getComplementaryContent();
return this;
}
public String getEnglishName() {
return englishName;
}
public void setEnglishName(String englishName) {
this.englishName = englishName;
}
public String getOtherName() {
return otherName;
}
public void setOtherName(String otherName) {
this.otherName = otherName;
}
public int getCountryID() {
return countryID;
}
public void setCountryID(int countryID) {
this.countryID = countryID;
}
public int getYear() {
return year;
}
public void setYear(int year) {
this.year = year;
}
public String getComplexityName() {
return complexityName;
}
public void setComplexityName(String complexityName) {
this.complexityName = complexityName;
}
public List<Integer> getRelatedMainTypeID() {
return relatedMainTypeID;
}
public void setRelatedMainTypeID(List<Integer> relatedMainTypeID) {
this.relatedMainTypeID = relatedMainTypeID;
}
public String getIntroductionDescription() {
return introductionDescription;
}
public void setIntroductionDescription(String introductionDescription) {
this.introductionDescription = introductionDescription;
}
public String getPros() {
return pros;
}
public void setPros(String pros) {
this.pros = pros;
}
public String getCons() {
return cons;
}
public void setCons(String cons) {
this.cons = cons;
}
public int getRequiredTime() {
return requiredTime;
}
public void setRequiredTime(int requiredTime) {
this.requiredTime = requiredTime;
}
public int getRequiredEmployee() {
return requiredEmployee;
}
public void setRequiredEmployee(int requiredEmployee) {
this.requiredEmployee = requiredEmployee;
}
public int getRequiredBudget() {
return requiredBudget;
}
public void setRequiredBudget(int requiredBudget) {
this.requiredBudget = requiredBudget;
}
public int getRequiredKnowledge() {
return requiredKnowledge;
}
public void setRequiredKnowledge(int requiredKnowledge) {
this.requiredKnowledge = requiredKnowledge;
}
public String getApplications() {
return applications;
}
public void setApplications(String applications) {
this.applications = applications;
}
public List<CompContentToolItem> getComplementaryContent() {
return complementaryContent;
}
public void setComplementaryContent(List<CompContentToolItem> complementaryContent) {
this.complementaryContent = complementaryContent;
}
public int getMainTypeID() {
return mainTypeID;
}
public void setMainTypeID(int mainTypeID) {
this.mainTypeID = mainTypeID;
}
public String getPersianName() {
return persianName;
}
public void setPersianName(String persianName) {
this.persianName = persianName;
}
public String getImageURL() {
return imageURL;
}
public void setImageURL(String imageURL) {
this.imageURL = imageURL;
}
}
子模型:
package com.example.amirmasoud.mgtools_2.model;
import com.google.gson.annotations.SerializedName;
import java.io.Serializable;
/**
* Created by AmirMasoud on 12/12/2018.
*/
public class CompContentToolItem implements Serializable {
@SerializedName("FileURL")
private String fileURL;
@SerializedName("Description")
private String description;
public String getFileURL() {
return fileURL;
}
public void setFileURL(String fileURL) {
this.fileURL = fileURL;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
}
服务:
public interface GetDataService {
@GET("GetMainTypeByID")
Call<ToolsItem> getToolItemContent(@Query("MainTypeID") int toolItemId);
}
改造实例:
public class RetrofitInstance {
private static final String BASE_URL = "https://assess.ir/service.svc/";
private static Retrofit retrofit;
public static Retrofit getRetrofitInstance() {
if (retrofit == null) {
retrofit = new retrofit2.Retrofit.Builder()
.baseUrl(BASE_URL)
.client(UnsafeOkHttpClient.getUnsafeOkHttpClient())
.addConverterFactory(GsonConverterFactory.create(new GsonBuilder().setLenient().create()))
.build();
}
return retrofit;
}
}
最佳答案
首先你必须使用 GSON 转换成 json 让我给你看一个例子
service.getToolItemContent(toolsItemId).enqueue(new Callback<ToolsItem>() {
@Override
public void onResponse(Call<ToolsItem> call, Response<ToolsItem> response) {
Gson gson = new Gson();
String data = gson.toJson(response.body)
ToolsItem toolsItem = gson.fromJson(data,ToolsItem.class);
toolItem.mConstructor(response.body());
}
@Override
public void onFailure(Call<ToolsItem> call, Throwable t) {
Toast.makeText(getApplicationContext(), t.getMessage(), Toast.LENGTH_LONG).show();
}
});
关于android - 入队 onResponse 方法在 android 改造中返回空的 response.body(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53816406/
我有一个包含以下内容的简单服务: import { Injectable } from '@angular/core'; import { Http, Response } from '@angula
在我的 Angular-12 应用程序中,我在服务中有以下代码: constructor(private http: HttpClient, private router: Router) {
我是android领域的新手,我想从响应json数组访问每个结果元素,但我无法做到这一点,我试图获取每个元素,但我只得到一个值“rohit1”是第一个元素。请帮助我。 我是 rohit parmar,
我正在用 java 编写 RESTful 服务,但是当我尝试使用 Resource 类时,显示以下错误:类型 Response.Response 构建器不可见。我不明白问题可能是什么,因为我已经导入了
在 Spring 应用程序中,我正在调用第三方服务,我正在发送 XML 请求并获取 XML 响应,当无法将该响应解析为 Java 对象时,我正确地获得了 XML 响应,我收到以下错误: org.spr
我正在发布一个页面 URL 例如 mysite.com/disclaimer/someinfo 此页面显示协议(protocol),如果用户单击同意按钮,则 PDF 文件将作为附件流式传输。 这样做的
我是 Camel 的新手。我一直在尝试将数据(文件中的 Json)提交到网络服务。这是我的代码: public static void main(String args[]) throws E
我有一个 HTTP 执行器类: Future future = service.apply(request).toJavaFuture(); 现在我想删除 ? extends其中的一部分,因为我不想让
我想将我所有的 http header 响应设置为这样的: response.headers["X-Frame-Options"] = "SAMEORIGIN" 我检查了this question ,
我们有两个 channel ,分别是 channelA 和 channelB。 在 channel A中我们有两个目的地 一个。第一个目的地将使用 XML 数据作为输入调用 channelB,并从 c
以下有什么区别 response.status(200).send('Hello World!'); 和这个 response.writeHead(200, {'content-type':'appl
我试图让Foundation在iPhone的浏览器上响应。我已经在手机上尝试过Safari和Chrome,它们都显示了 table 面布局。 但是,在 table 面上,如果缩小浏览器窗口,则会看到布
您好,当我在云代码中运行此作业时,我收到一条错误日志:Failed with: success/error was not called. 定义功能运行良好,但在作业日志中我有此错误日志。请协助我解决
我正在使用ozeki ng短信网关。我无法将任何短信发送到任何手机。请帮助我通过网络发送短信到手机 从客户端检测到一个潜在危险的Request.Form值(textboxError =“。设置此值之后
今天我在 WordPress 中遇到了问题。当我尝试创建一个新页面并在 WordPress 管理部分上传新图像时,我尝试找出解决方案,但我没有得到它......所以经过一个小时的打磨后我得到了一个解决
我过去常常通过刷新和结束来结束对客户端的传输,如下面的代码所示。 Response.Flush(); Response.End(); 但是,Response.End() 将缓冲内容刷新到客户端让我印象
我正在编写一个在单击按钮时显示对话框窗口的函数:这里是与状态和 statusCode 相关的代码段。 if(response.status>300){
从资源清理的角度,为什么会有Response.Close()和Response.Dispose(),哪个更全面(调用另一个)? 最佳答案 在提供这两种方法的情况下,Dispose 的实现应该调用 Cl
在我注意到我的代码可能在以经典模式设置的服务器上运行之前,我一直在使用 Response.Header.Add()。在这种情况下,异常“此操作需要 IIS 集成管道模式”。被提出。 我切换到 Resp
Response.End() 生成 ThreadAbortException。 使用 HttpContext.Current.ApplicationInstance.CompleteRequest 代
我是一名优秀的程序员,十分优秀!