入门学习Retrofit2 碰到重复请求两次的问题

Linker_com 2017-07-28 03:40:01
代码如下:
ApiSerice:

public interface ApiService {

@GET("test.do")
Call<String> getString(@Query("phoneNum")String phoneNum);
}


执行模块:

public void onClick(View view) {
switch (view.getId()) {
case R.id.buttonOK:
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(LocalPropertyUtils.serverUrl(MainActivity.this))
.addConverterFactory(FastJsonConverterFactory.create())
.build();
ApiService apiService = retrofit.create(ApiService.class);
String phoneNum = "13948756432";
Call<String> call = apiService.getString(phoneNum);
call.enqueue(new Callback<String>() {
@Override
public void onResponse(Call<String> call, Response<String> response) {
if (response.isSuccessful()) {
Message message = new Message();
message.obj = response;
mHandler.sendMessage(message);
}
}
@Override
public void onFailure(Call<String> call, Throwable t) {
t.printStackTrace();
}
});
break;
default:
break;
}
}


Handler

Handler mHandler = new Handler() {
Integer times = 0;
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
times++;
//这里会弹出两次
Toast.makeText(MainActivity.this, "Times:" + times.toString(), Toast.LENGTH_LONG).show();
}
};


Debug调试,发现其中onResponse()会执行两次,在短时间内发送了两次请求。
...全文
1491 3 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
Hanigone 2018-10-31
  • 打赏
  • 举报
回复
顶一下 求原因
Sunny_Captain 2018-09-12
  • 打赏
  • 举报
回复
兄弟我今天也发现了,还以为是bug,你知道为什么了吗?
Linker_com 2017-07-28
  • 打赏
  • 举报
回复
没人回复,自己顶一下

80,473

社区成员

发帖
与我相关
我的任务
社区描述
移动平台 Android
androidandroid-studioandroidx 技术论坛(原bbs)
社区管理员
  • Android
  • yechaoa
  • 失落夏天
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧