我无法解决问题。相同的代码提供API 21的onResponse,但提供API 28的onFailure。有什么想法吗?
public class MainActivity extends AppCompatActivity {
ListView listView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView=(ListView)findViewById(R.id.list);
final ArrayList<String> logins = new ArrayList<>();
Retrofit retrofit = new Retrofit.Builder()
.baseUrl("http://172.28.123.182:8080")
.addConverterFactory(GsonConverterFactory.create())
.build();
final TestService testService = retrofit.create(TestService.class);
testService.getUsers().enqueue(new Callback<List<UserDto>>() {
@Override
public void onResponse(Call<List<UserDto>> call, Response<List<UserDto>> response) {
if(response.body() == null) {
onFailure(call, null);
Log.d("tutaj", "blad1");
return;
}
List<UserDto> users = response.body();
while (!users.isEmpty()){
logins.add(users.get(0).getLogin());
users.remove(0);
}
}
@Override
public void onFailure(Call<List<UserDto>> call, Throwable t) {
Toast.makeText(MainActivity.this, "No users", Toast.LENGTH_LONG).show();
Log.d("tutaj", "blad2");
}
});
ArrayAdapter adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, android.R.id.text1, logins);
listView.setAdapter(adapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Intent intent = new Intent(MainActivity.this, SecondActivity.class);
intent.putExtra("KEY", logins.get(position));
startActivity(intent);
}
});
}
@Override
protected void onResume() {
super.onResume();
}
}
我阅读了有关权限的信息,并且在API 23之后INTERNET是正常的,因此应该没有问题。我不知道问题是否出在代码或权限之类的东西上。
如果您需要有关清单文件或gradle构建的任何信息,请告诉我。问题是为什么它可以在爱好者API上工作。