我有以下代码来获取整数数据。getData(NewsSettings)
方法返回3.当我点击按钮时,应用会显示No new news found
。
为什么它在getData之前运行toast?
已更新
我添加了完整的onCreate
和getData
方法。当我跑步时,它会显示No new news found
然后Inside Response
然后Data count = 3
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_profile);
assert getSupportActionBar() != null;
getSupportActionBar().setTitle(R.string.action_profile);
pref = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
Button button = (Button) findViewById(R.id.buttonBuilder);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
newsSettings = new NewsSettings();
newsSettings.setPreferredCity(pref.getString("prefCity", ""));
int dataCount = getData(newsSettings);
if(dataCount > 0)
Toast.makeText(getApplicationContext(), dataCount + " new news found", Toast.LENGTH_SHORT).show();
else
Toast.makeText(getApplicationContext(), "No new news found", Toast.LENGTH_SHORT).show();
}
});
}
private int getData(NewsSettings newsSettings) {
final int[] data = {0};
RequestInterface requestInterface = RequestHelper.getInstance().getRequest();
ServerRequest request = new ServerRequest();
request.setOperation("dataCount");
request.setNewsSettings(newsSettings);
Call <ServerResponse> response = requestInterface.operation(request);
response.enqueue(new Callback <ServerResponse>() {
@Override
public void onResponse(Call <ServerResponse> call,
retrofit2.Response <ServerResponse> response) {
ServerResponse resp = response.body();
Toast.makeText(getApplicationContext(), "Inside Response", Toast.LENGTH_SHORT).show();
if(resp.getResult().equals(Constants.SUCCESS)) {
data[0] = resp.getNewsSettings().getDataCount();
Toast.makeText(getApplicationContext(), "Data count = " + data[0], Toast.LENGTH_LONG).show();
}
}
@Override
public void onFailure(Call <ServerResponse> call, Throwable t) {
Log.d(Constants.TAG,"failed");
Toast.makeText(getApplicationContext(), t.getLocalizedMessage(), Toast.LENGTH_LONG).show();
}
});
return data[0];
}
答案 0 :(得分:0)
确保返回值dataCount
为greater than 0
。调试它或删除if-else,然后使用单个makeToast方法显示dataCount
。
答案 1 :(得分:0)
它返回正确的值,即小于或等于0.为什么?因为,当您单击按钮时,它会调用getData()
方法,该方法包含Retrofit的异步网络调用方法。在调用get data[0]
方法之后,onResponse()
变量填充了该值。
因此,同时程序光标完成第一次执行,当您单击某个按钮时,该执行开始。
您可以像这样修改代码。
@Override
protected void onCreate(Bundle savedInstanceState) {
....
....
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
newsSettings = new NewsSettings();
newsSettings.setPreferredCity(pref.getString("prefCity", ""));
/** here is the modification **/
getData(newsSettings);
}
}
在您的getData()
方法中,移除int
返回类型并使用void
private void getData(NewsSettings newsSettings) { // remove int return type
.....
.....
response.enqueue(new Callback <ServerResponse>() {
@Override
public void onResponse(Call <ServerResponse> call,
retrofit2.Response <ServerResponse> response) {
ServerResponse resp = response.body();
Toast.makeText(getApplicationContext(), "Inside Response", Toast.LENGTH_SHORT).show();
if(resp.getResult().equals(Constants.SUCCESS)) {
data[0] = resp.getNewsSettings().getDataCount();
/** here is the modification ***/
displayDataCount(data[0]); // call to displayCount() method
Toast.makeText(getApplicationContext(), "Data count = " + data[0], Toast.LENGTH_LONG).show();
}
}
@Override
public void onFailure(Call <ServerResponse> call, Throwable t) {
Log.d(Constants.TAG,"failed");
Toast.makeText(getApplicationContext(), t.getLocalizedMessage(), Toast.LENGTH_LONG).show();
}
});
}
并创建另一个名为displayDataCount()
的方法,您可以在其中显示数据计数
private void displayDataCount(int dataCount) {
if(dataCount > 0)
Toast.makeText(getApplicationContext(), dataCount + " new news found", Toast.LENGTH_SHORT).show();
else
Toast.makeText(getApplicationContext(), "No new news found", Toast.LENGTH_SHORT).show();
}