大家好,想請問我最近遇到的問題,我在android studio上寫java,為什麼return x的值是0
而不是我在onResponse回傳的 x = response.body() ,已經確定response.body裡面的值是25
這邊是函式
public class GetLong {
public static int x;
public static int ShopLength(){
Retrofit retrofit1 = new Retrofit.Builder()
.baseUrl("http://10.25.7.222:62678/")
.addConverterFactory(GsonConverterFactory.create())
.build();
ApiGet3 apiGet3 = retrofit1.create(ApiGet3.class);
Call<Integer> call = apiGet3.getTotal();
call.enqueue(new Callback<Integer>() {
@Override
public void onResponse(Call<Integer> call, Response<Integer> response) {
x = response.body();
Log.d("Length",""+ x); //顯示25
}
@Override
public void onFailure(Call<Integer> call, Throwable t) {
Log.d("Length","error:"+t.toString());
}
});
return x; //回傳卻是0
}
public interface ApiGet3{
@GET("api/Commodity/GetTotal")
Call<Integer> getTotal();
}
}
我要呼叫ShopLength(),Log的值是0
public void t1(){
int y = ShopLength();
Log.d("Y的值",""+y); //顯示0
}
麻煩各位大大幫忙解決我的問題