Слушатель buttonOnClick устанавливает результат API в текстовое представление, однако этот результат должен меняться и показывать разные шутки при каждом нажатии кнопки. Нужно ли хранить данные из API в список? Не уверен, как это будет работать.
Спасибо :)
Основной класс активности
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;
public class MainActivity extends AppCompatActivity {
private RetroFitHelper service;
private String calls;
private Button btnJoke;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Retrofit retrofit = new Retrofit.Builder()
.baseUrl("https://api.chucknorris.io/")
.addConverterFactory(GsonConverterFactory.create())
.build();
service = retrofit.create(RetroFitHelper.class);
getJokes();
}
private void getJokes(){
Call<Joke> call = service.findJoke("religion");
call.enqueue(new Callback<Joke>() {
@Override
public void onResponse(Call<Joke> call, Response<Joke> response) {
if (response.isSuccessful()) {
String result = response.body().getValue().getJoke();
btnJoke = findViewById(R.id.btnJoke);
TextView joke = findViewById(R.id.textView);
btnJoke.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
joke.setText(result);
}
});
}
}
@Override
public void onFailure(Call<Joke> call, Throwable t) {
Toast.makeText(MainActivity.this, " Error 404 no response found" ,Toast.LENGTH_LONG).show();
}
});
}
}
Шутка и класс значения
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
import java.io.Serializable;
import java.util.List;
public class Joke {
@SerializedName("type")
@Expose
private String type;
@SerializedName("value")
@Expose
private Value value;
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public Value getValue() {
return value;
}
public void setValue(Value value) {
this.value = value;
}
}
class Value {
@SerializedName("id")
@Expose
private Integer id;
@SerializedName("joke")
@Expose
private String joke;
@SerializedName("categories")
@Expose
private List<Object> categories = null;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getJoke() {
return joke;
}
public void setJoke(String joke) {
this.joke = joke;
}
public List<Object> getCategories() {
return categories;
}
public void setCategories(List<Object> categories) {
this.categories = categories;
}}
Класс интерфейса
import retrofit2.Call;
import retrofit2.http.GET;
import retrofit2.http.Path;
interface RetroFitHelper {
@GET("/jokes/random/category=religion")
Call<Joke> findJoke(@Path("category") String random);
}