改造:回调不访问成功和失败方法
Retrofit: Callback doesn't access succes and failure methods
我正在尝试使用 Retrofit 执行 HTTP-POST。
我在其他 类 中成功完成了此操作,但现在不起作用。也许是因为我正在扩展 BaseAdapter 而不是像以前那样扩展 Activity 或 Fragment,但是当我调用 POST 方法时,它甚至不访问成功 and/or 失败方法
public class AsmReactionsAdapter extends BaseAdapter implements Callback<Like> {
@Override
public View getView(int position, View convertView, ViewGroup parent) {
final AnswersAsm answer = getItem(position);
ViewHolder viewHolder;
if (convertView != null) {
viewHolder = (ViewHolder) convertView.getTag();
} else {
convertView = from(context).inflate(R.layout.asm_reaction_item, parent, false);
viewHolder = new ViewHolder(convertView);
convertView.setTag(viewHolder);
}
viewHolder.likeBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
like(answer.getAnswerId());
}
});
return convertView;
}
private void like(int id)
{
Like like=new Like();
like.setGebruikerId(3);
like.setAntwoordDossierId(id);
like.setProvider("LOCAL");
JppApplication.getService().likeAntwoord(like, this);
}
@Override
public void success(Like like, Response response) {
System.out.println("werkt");
}
@Override
public void failure(RetrofitError retrofitError) {
System.out.println("werkt niet");
}
static class ViewHolder {
Button likeBtn;
public ViewHolder(View view) {
likeBtn=(Button) view.findViewById(R.id.asm_reaction_item_btnAsmLike);
}
}
}
我找到问题了。这就是解决方案
public class AsmReactionsAdapter extends BaseAdapter{
@Override
public View getView(int position, View convertView, ViewGroup parent) {
final AnswersAsm answer = getItem(position);
ViewHolder viewHolder;
if (convertView != null) {
viewHolder = (ViewHolder) convertView.getTag();
} else {
convertView = from(context).inflate(R.layout.asm_reaction_item, parent, false);
viewHolder = new ViewHolder(convertView);
convertView.setTag(viewHolder);
}
viewHolder.likeBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Like like=new Like();
like.setId(3);
like.setDossierId(answer.getAnswerId());
like.setProvider("LOCAL");
JppApplication.getService().likeAntwoord(like, new Callback<Like(){
@Override
public void success(Like like, Response response) {
System.out.println("LIKE OK");
}
@Override
public void failure(RetrofitError retrofitError) {
System.out.println("LIKE FAILURE");
}
});
}
});
return convertView;
}
}
static class ViewHolder {
Button likeBtn;
public ViewHolder(View view) {
likeBtn=(Button) view.findViewById(R.id.asm_reaction_item_btnAsmLike);
}
}
我正在尝试使用 Retrofit 执行 HTTP-POST。 我在其他 类 中成功完成了此操作,但现在不起作用。也许是因为我正在扩展 BaseAdapter 而不是像以前那样扩展 Activity 或 Fragment,但是当我调用 POST 方法时,它甚至不访问成功 and/or 失败方法
public class AsmReactionsAdapter extends BaseAdapter implements Callback<Like> {
@Override
public View getView(int position, View convertView, ViewGroup parent) {
final AnswersAsm answer = getItem(position);
ViewHolder viewHolder;
if (convertView != null) {
viewHolder = (ViewHolder) convertView.getTag();
} else {
convertView = from(context).inflate(R.layout.asm_reaction_item, parent, false);
viewHolder = new ViewHolder(convertView);
convertView.setTag(viewHolder);
}
viewHolder.likeBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
like(answer.getAnswerId());
}
});
return convertView;
}
private void like(int id)
{
Like like=new Like();
like.setGebruikerId(3);
like.setAntwoordDossierId(id);
like.setProvider("LOCAL");
JppApplication.getService().likeAntwoord(like, this);
}
@Override
public void success(Like like, Response response) {
System.out.println("werkt");
}
@Override
public void failure(RetrofitError retrofitError) {
System.out.println("werkt niet");
}
static class ViewHolder {
Button likeBtn;
public ViewHolder(View view) {
likeBtn=(Button) view.findViewById(R.id.asm_reaction_item_btnAsmLike);
}
}
}
我找到问题了。这就是解决方案
public class AsmReactionsAdapter extends BaseAdapter{
@Override
public View getView(int position, View convertView, ViewGroup parent) {
final AnswersAsm answer = getItem(position);
ViewHolder viewHolder;
if (convertView != null) {
viewHolder = (ViewHolder) convertView.getTag();
} else {
convertView = from(context).inflate(R.layout.asm_reaction_item, parent, false);
viewHolder = new ViewHolder(convertView);
convertView.setTag(viewHolder);
}
viewHolder.likeBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Like like=new Like();
like.setId(3);
like.setDossierId(answer.getAnswerId());
like.setProvider("LOCAL");
JppApplication.getService().likeAntwoord(like, new Callback<Like(){
@Override
public void success(Like like, Response response) {
System.out.println("LIKE OK");
}
@Override
public void failure(RetrofitError retrofitError) {
System.out.println("LIKE FAILURE");
}
});
}
});
return convertView;
}
}
static class ViewHolder {
Button likeBtn;
public ViewHolder(View view) {
likeBtn=(Button) view.findViewById(R.id.asm_reaction_item_btnAsmLike);
}
}