当我从服务器胖数据时,它不工作: 这是我的ApiClass类
public class ApiClient {
public static final String BASE_URl="https://simplifiedcoding.net/demos/";
public static ApiInterface apiInterface= null;
public static ApiInterface getApiClient(){
if (apiInterface == null){
Retrofit retrofit=new Retrofit.Builder().baseUrl(BASE_URl)
.addConverterFactory(GsonConverterFactory.create())
.build();
apiInterface = retrofit.create(ApiInterface.class); // this line is good
}
return apiInterface;
}
public interface ApiInterface {
@GET("marvel/")
Call<data> getcontex();
}
}
这是我的主要课程
Call<data> listcall = ApiClient.getApiClient().getcontex();
listcall.enqueue(new Callback<data>() {
@Override
public void onResponse(Call<data> call, Response<data> response) {
data d=response.body();
Toast.makeText(MainActivity.this, "Done ", Toast.LENGTH_SHORT).show();
}
@Override
public void onFailure(Call<data> call, Throwable t) {
Toast.makeText(MainActivity.this, "Faield", Toast.LENGTH_SHORT).show();
}
});
我严重无法理解什么是错的。你能帮帮我们吗?
答案 0 :(得分:0)
我假设你的api用一组对象做出响应,你想得到一个回调。
@GET('your url')
void getData(Callback<ArrayList<Data>> callback);
答案 1 :(得分:0)
您有一个json数组而不是json对象,请尝试更改您的代码
Call<List<data>> listcall = ApiClient.getApiClient().getcontex();
listcall.enqueue(new Callback<List<data>>() {
@Override
public void onResponse(Call<List<data>> call, Response<List<data>> response) {
data d=response.body();
Toast.makeText(MainActivity.this, "Done ", Toast.LENGTH_SHORT).show();
}
@Override
public void onFailure(Call<List<data>> call, Throwable t) {
Toast.makeText(MainActivity.this, "Faield", Toast.LENGTH_SHORT).show();
}
});
并在您的界面中
public interface ApiInterface {
@GET("marvel/")
Call<List<data>> getcontex();
}
答案 2 :(得分:0)
试试这个: -
这是你的模型类
<强> Data.java 强>
public class Data {
@SerializedName("name")
@Expose
private String name;
@SerializedName("realname")
@Expose
private String realname;
@SerializedName("team")
@Expose
private String team;
@SerializedName("firstappearance")
@Expose
private String firstappearance;
@SerializedName("createdby")
@Expose
private String createdby;
@SerializedName("publisher")
@Expose
private String publisher;
@SerializedName("imageurl")
@Expose
private String imageurl;
@SerializedName("bio")
@Expose
private String bio;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getRealname() {
return realname;
}
public void setRealname(String realname) {
this.realname = realname;
}
public String getTeam() {
return team;
}
public void setTeam(String team) {
this.team = team;
}
public String getFirstappearance() {
return firstappearance;
}
public void setFirstappearance(String firstappearance) {
this.firstappearance = firstappearance;
}
public String getCreatedby() {
return createdby;
}
public void setCreatedby(String createdby) {
this.createdby = createdby;
}
public String getPublisher() {
return publisher;
}
public void setPublisher(String publisher) {
this.publisher = publisher;
}
public String getImageurl() {
return imageurl;
}
public void setImageurl(String imageurl) {
this.imageurl = imageurl;
}
public String getBio() {
return bio;
}
public void setBio(String bio) {
this.bio = bio;
}
}
和api服务的接口
<强> ApiService.java 强>
public interface ApiService {
@GET("marvel/")
Call<List<Data>> getHero();
}
这是你的MainActivity.java
<强> MainActivity.java 强>
public class MainActivity extends AppCompatActivity {
String BaseUrl = "https://simplifiedcoding.net/demos/";
List<Data> data = new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ApiService retrofitService = new Retrofit.Builder()
.baseUrl(BaseUrl)
.addConverterFactory(GsonConverterFactory.create())
.build().create(ApiService.class);
Call<List<Data>> call = retrofitService.getHero();
call.enqueue(new Callback<List<Data>>() {
@Override
public void onResponse(@NonNull Call<List<Data>> call, @NonNull Response<List<Data>> response) {
data = response.body();
for (int i = 0; i < data.size(); i++) {
//you can get your all data like name,bio,realname,team etc...
Toast.makeText(MainActivity.this, "Done", Toast.LENGTH_SHORT).show();
}
}
@Override
public void onFailure(@NonNull Call<List<Data>> call, @NonNull Throwable t) {
Toast.makeText(MainActivity.this, "Faield", Toast.LENGTH_SHORT).show();
}
});
}
}
最后不要忘记在AndroidManifest.xml中添加互联网权限
答案 3 :(得分:0)
这里我已经为您的问题编写了工作示例。我发现问题在于解析没有键的arraylist。请参阅以下代码段:
您的模型类是
public class data {
@SerializedName("name") @Expose private String name;
@SerializedName("realname") @Expose private String realname;
@SerializedName("team") @Expose private String team;
@SerializedName("firstappearance") @Expose private String firstappearance;
@SerializedName("createdby") @Expose private String createdby;
@SerializedName("publisher") @Expose private String publisher;
@SerializedName("imageurl") @Expose private String imageurl;
@SerializedName("bio") @Expose private String bio;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getRealname() {
return realname;
}
public void setRealname(String realname) {
this.realname = realname;
}
public String getTeam() {
return team;
}
public void setTeam(String team) {
this.team = team;
}
public String getFirstappearance() {
return firstappearance;
}
public void setFirstappearance(String firstappearance) {
this.firstappearance = firstappearance;
}
public String getCreatedby() {
return createdby;
}
public void setCreatedby(String createdby) {
this.createdby = createdby;
}
public String getPublisher() {
return publisher;
}
public void setPublisher(String publisher) {
this.publisher = publisher;
}
public String getImageurl() {
return imageurl;
}
public void setImageurl(String imageurl) {
this.imageurl = imageurl;
}
public String getBio() {
return bio;
}
public void setBio(String bio) {
this.bio = bio;
}
}
然后您的webservice调用者类是:
公共类WebServiceCaller {
private static WebServiceApiInterface webApiInterface;
public static final String BASE_URl = "https://simplifiedcoding.net/demos/";
public static WebServiceApiInterface getClient() {
if (webApiInterface == null) {
HttpLoggingInterceptor logging = new HttpLoggingInterceptor();
logging.setLevel(HttpLoggingInterceptor.Level.BODY);
OkHttpClient okclient = new OkHttpClient.Builder().addInterceptor(logging).build();
Gson gson = new GsonBuilder()
.setDateFormat("yyyy-MM-dd'T'HH:mm:ssZ")
.create();
Retrofit client = new Retrofit.Builder() // .client(okClient)
.baseUrl(BASE_URl)
.client(okclient.newBuilder().connectTimeout(10, TimeUnit.MINUTES).readTimeout(100, TimeUnit.SECONDS).writeTimeout(100, TimeUnit.SECONDS).addInterceptor(logging).build())
.addConverterFactory(GsonConverterFactory.create(gson))
.build();
webApiInterface = client.create(WebServiceApiInterface.class);
}
return webApiInterface;
}
public interface WebServiceApiInterface {
@GET("marvel/")
Call < ArrayList < data >> getcontex();
}
}
现在在主要活动中调用如下所示的服务。
public void getData(){try {
final ProgressDialog dialog = ProgressDialog.show(this, "", "loading...");
WebServiceCaller.WebServiceApiInterface service = WebServiceCaller.getClient();
// Call<data> call = service.getStatusMenu("6a10c6fd0a021e8b5beda2948790d9f7FYnx", "com.r3app.wthrcomplete");
Call<ArrayList<data>> call = service.getcontex();
call.enqueue(new Callback<ArrayList<data>>() {
@Override
public void onResponse(Call<ArrayList<data>> call, Response<ArrayList<data>> response) {
dialog.dismiss();
if (response.isSuccessful()) {
ArrayList<data> result = response.body();
Toast.makeText(ScrollingActivity.this, "Done "+result.get(0).getName(), Toast.LENGTH_SHORT).show();
} else {
dialog.dismiss();
}
}
@Override
public void onFailure(Call<ArrayList<data>> call, Throwable t) {
t.printStackTrace();
Toast.makeText(ScrollingActivity.this, " "+t.getMessage(), Toast.LENGTH_SHORT).show();
Log.v("onFailure", "onFailure");
dialog.dismiss();
}
});
} catch (Exception e) {
e.printStackTrace();
}
}