本文介绍了Retrofit2:ClassCastException:无法将java.util.ArrayList强制转换为retrofit2.Call的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请与Retrofit2帮助。我是Retrofit的新手。
我创建了简单的服务器应用程序。
应用程序管理日记列表:在内存列表中添加日记,并按ID返回日记。
有一个Journal.java:

please help with Retrofit2. I'm very new in Retrofit.I create simple server application.The application manage list of Journals: add Journal in the in-memory list and return Journal by id.There is an Journal.java:

public class Journal {
private AtomicInteger getNewId = new AtomicInteger(0);
@SerializedName("id")
@Expose
private Integer id;
@SerializedName("name")
@Expose
private String name;

public Journal(String name) {
    this.id = getNewId.incrementAndGet();
    this.name = name;}

public Integer getId() {return id;}
public String getName() {return name;}
}

有一个控制器接口:

public interface JournalController {

@GET("journal")
Call<List<Journal>> getJournalList();

@GET("journal/{id}")
Call<Journal> getJournal(@Path("id") Integer id);

@POST("journal")
Call<Journal>  addJournal(@Body Journal journal);
}

这是接口实现:

@Controller
public class JournalControllerImpl implements JournalController {
// An in-memory list that the controller uses to store the Journals
private List<Journal> journals = new ArrayList<>();

@RequestMapping(value= "journal", method=RequestMethod.GET)
public @ResponseBody Call<List<Journal>> getJournalList() {
    return (Call<List<Journal>>) journals;}

@RequestMapping(value= "journal/{id}", method= RequestMethod.GET)
public @ResponseBody Call<Journal> getJournal(@Path("id") Integer id) {
    Journal journal = journals.get(id);
    return (Call<Journal>) journal;}

@RequestMapping(value= "journal", method= RequestMethod.POST)
public @ResponseBody Call<Journal> addJournal(@Body Journal journal) {
    journals.add(journal);
    return (Call<Journal> ) journal; }
}

应用程序成功启动。应用程序启动期间的控制台输出:

The application started succesesully. Console output during application's startup:

将 {[/ journal],methods = [POST]}映射到公共Retro2上。调用main.JournalControllerImpl.addJournal(main.Journal )

Mapped "{[/journal],methods=[POST]}" onto public retrofit2.Call main.JournalControllerImpl.addJournal(main.Journal)

将 {[/ journal {/ id}],methods = [GET]}映射到公共Retrofit2。调用main.JournalControllerImpl.getJournal(java.lang。整数)

Mapped "{[/journal{/id}],methods=[GET]}" onto public retrofit2.Call main.JournalControllerImpl.getJournal(java.lang.Integer)

比我尝试运行URL (或GET HttpRequest )。
应用程序输出中存在错误:

Than I try to run URL http://localhost:8080/journal on browser (or GET HttpRequest http://localhost:8080/journal).There is an error in application output:

... java.lang.ClassCastException:java.util.ArrayList无法转换为retrofit2.Call。 ..

"... java.lang.ClassCastException: java.util.ArrayList cannot be cast to retrofit2.Call..."

您能猜出是什么问题吗?
将java.util.ArrayList转换为Call真的有问题吗? (我尝试过CopyOnWriteArrayList,但这无济于事。

Could you guess what is wrong?Is it really problems with conversion java.util.ArrayList to Call ? (I've tryed to CopyOnWriteArrayList but this doesn't help.

预先谢谢您。

推荐答案

使用服务接口(服务)的方法是通过回调(onResponse,onFailure)对服务实施逻辑所需的细节,如下所示。

The way to use the service interface (service) is to the service with a callback (onResponse, onFailure)implement the detail the logic need as highlighted below.

//在创建
JournalController journalService时传递构造函数;
//返回日志列表

//Pass in a constructor upon creating JournalController journalService; //Return journal list

Call<List<Journal>> call = journalService.getJournalList();
call.enqueue(new Callback<>() {
    @Override
    public void onResponse(Call<List<Journal>> call, Response<List<Journal>>
       response) {
       int statusCode = response.code();
       List<Journal> journal = response.body();

   }

   @Override
   public void onFailure(Call<Journal> call, Throwable t) {
      // Log error here since request failed
  }
});

//Return a journal

Call<Journal> call = journalService.getJournal(userId);
call.enqueue(new Callback<Journal>() {
    @Override
    public void onResponse(Call<Journal> call, Response<Journal> response) {
       int statusCode = response.code();
       Journal journal = response.body();
   }

   @Override
   public void onFailure(Call<Journal> call, Throwable t) {
      // Log error here since request failed
  }
});

//Add journal

Call<Journal> call = journalService.addJournal(new Journal("username");
call.enqueue(new Callback<Journal>() {
    @Override
    public void onResponse(Call<Journal> call, Response<Journal> response) {
       int statusCode = response.code();
       Journal journal = response.body();
   }

   @Override
   public void onFailure(Call<Journal> call, Throwable t) {
      // Log error here since request failed
  }
});

这篇关于Retrofit2:ClassCastException:无法将java.util.ArrayList强制转换为retrofit2.Call的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-08 20:57