我有一个返回{"id":1,"family":"mohsen"}
的ASP.NET Web API服务。为什么Retrofit会从中返回null呢?
API接口:
public interface ApiRefrence {
@GET("hello")
Call<List<product>> GetProduct();
}
API客户端类:
public class ApiClinet {
public static final String BasesUrl = "http://185.105.239.32/api/test/";
public static Retrofit ret = null;
return ret;
}
public static Retrofit GetApiClinet() {
if(ret == null) {
ret = new Retrofit.Builder().baseUrl(BasesUrl)
.addConverterFactory(GsonConverterFactory.create())
.build();
产品类别POJO:
public class product {
@SerializedName("id")
@Expose
int id;
@SerializedName("family")
@Expose
String family;
public int getId() {
return id;
}
public String getFamily() {
return family;
}
}
MainActivity:
public class MainActivity extends AppCompatActivity {
EditText txt1,txt2;
List<product> lst;
TextView txt3;
private ApiRefrence ref;
Button btn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
txt3 = (TextView)findViewById(R.id.textView) ;
txt1 = (EditText)findViewById(R.id.editText);
txt2 = (EditText)findViewById(R.id.editText2);
ref = ApiClinet.GetApiClinet().create(ApiRefrence.class);
Call<List<product>> call = ref.GetProduct();
call.enqueue(new Callback<List<product>>() {
@Override
public void onResponse(Call<List<product>> call, Response<List<product>>
response) {
lst = response.body();
txt3.setText(response.body().get(0).family.toString());
}
@Override
public void onFailure(Call<List<product>> call, Throwable t) {
}
});
}
}
答案 0 :(得分:0)
将基本网址更改为:
public static final String BasesUrl="http://185.105.239.32/api/test/";
将ApiInterface更改为:
public interface ApiRefrence {
@GET("hello")
Call<product> GetProduct();
}
更改您的回复代码:
Call<product> call = ref.GetProduct();
call.enqueue(new Callback<product>() {
@Override
public void onResponse(Call<product> call, Response<product>
response) {
lst=response.body();
txt3.setText(response.body().getFamily());
}
@Override
public void onFailure(Call<product> call, Throwable t) {
}
});