// Создаем адаптер с базовым адресом
RestAdapter restAdapter = new RestAdapter.Builder().setEndpoint(SOURCE).build();
// Создаём сервис
API api = restAdapter.create(API.class);
api.getCinema(API_TOKEN, new Callback() {
@Override
public void success(KinoModel kinoModel, Response response) {
// Получаем ввиде json и конвертируем
cinemas.add(new Cinema(kinoModel.getTitleRU()));
}
@Override
public void failure(RetrofitError error) {
Log.d("myLogs", "Failed!!!");
}
});
public interface API
{
@GET("/serial_updates.json")
void getCinema(@Query("api_token") String api_token, Callback response);
}
is not abstract and does not override abstract method success(Object,Response) in Callback
RestAdapter restAdapter2 = new RestAdapter.Builder().setEndpoint(SOURCE_KP).build();
APIKp apiKp= restAdapter2.create(APIKp.class);
apiKp.getKp(film_ID, new Callback<KinoModelKp>() {
@Override
public void success(KinoModelKp kinoModelKp, Response response) {
Log.d("myLogs", "s=" + String.valueOf(kinoModelKp.getKPYear()));
}
@Override
public void failure(RetrofitError error) { Log.d("myLogs", "KP! Failed!!!"); }
});
public class KinoModelKp
{
@Expose
private List<KpRatUpd> ratingData;
@Expose
private int year;
public List<KpRatUpd> getRatUpdates() {return ratingData;}
public int getKPYear() {
return year;
}
public class KpRatUpd {
@SerializedName("rating")
private float rating;
@SerializedName("ratingVoteCount")
private int ratingVoteCount;
@SerializedName("ratingIMDb")
private float ratingIMDb;
@SerializedName("ratingIMDbVoteCount")
private int ratingIMDbVoteCount;
public float getRatKP() {
return rating;
}
public int getRatKPVote() {
return ratingVoteCount;
}
public float getRatImdb() {
return ratingIMDb;
}
public int getRatImdbVote() {
return ratingIMDbVoteCount;
}
}
}
http://api.kinopoisk.cf/getFilm?filmID=840623
error.getResponse().toString()
retrofit.client.Response@27668984
error.getResponse().getReason()
выдает OKerror.getResponse().getUrl()
выдал http://api.kinopoisk.cf/getFilm?filmID=840623
т.е. сформировалось все нормально.private List<KpRatUpd> ratingData;
"ratingData": {
"ratingVoteCount": 47,
"ratingAwait": "92%",
"ratingAwaitCount": "110",
"ratingIMDb": "6.9",
"ratingIMDbVoteCount": "816",
"ratingFilmCritics": "33%",
"ratingFilmCriticsVoteCount": "42"
}
public String getKP (int film_ID)
{
apiKp.getKp(film_ID, new Callback<KinoModelKp>() {
@Override
public void success(KinoModelKp kinoModelKp, Response response) {
rat = "KP=" + String.valueOf(kinoModelKp.ratingData.rating) + "(" + kinoModelKp.ratingData.ratingVoteCount + "); IMDB=" + String.valueOf(kinoModelKp.ratingData.ratingIMDb) + "(" + kinoModelKp.ratingData.ratingIMDbVoteCount + ")";
Log.d("myLogs", "rat1=" + rat);
}
@Override
public void failure(RetrofitError error) {
Log.d("myLogs", "KP! Failed!!! = " + error.getResponse());
}
});
Log.d("myLogs", "rat2=" + rat);
return rat;
}
String rat="";
rat=""
. Как задержать выполнение return пока не отработает retrofit? public void getCinemaFrag()
{
// Создаем адаптер с базовым адресом
RestAdapter restAdapter = new RestAdapter.Builder().setEndpoint(SOURCE_MOON).build();
RestAdapter restAdapter2 = new RestAdapter.Builder().setEndpoint(SOURCE_KP).build();
// Создаём сервисы API
apiMoon = restAdapter.create(APIMoon.class);
apiKp= restAdapter2.create(APIKp.class);
Thread t = new Thread(new Runnable(){
@Override
public void run(){
mProgressBar.setVisibility(View.VISIBLE);
KinoModel kinoModel = apiMoon.getCinema(API_TOKEN);
// Получаем ввиде json и конвертируем
cinemas.clear();
for (int k = 0; k < kinoModel.getNumUpdates(); k++) {
film_ID = kinoModel.getUpdates().get(k).getKP();
if (film_ID != 0) {
KinoModelKp kinoModelKp = apiKp.getKp(film_ID);
cinemas.add(new Cinema(kinoModel.getUpdates().get(k).getTitleRU(), kinoModel.getUpdates().get(k).getTitleEN(),
kinoModel.getUpdates().get(k).getKP(), kinoModel.getUpdates().get(k).getToken(), kinoModel.getUpdates().get(k).getType(),
kinoModel.getUpdates().get(k).getCAM(), kinoModel.getUpdates().get(k).getIframe(), kinoModel.getUpdates().get(k).getTranslate(),
kinoModel.getUpdates().get(k).getTranslateID(), kinoModel.getUpdates().get(k).getAdded(), "http://st.kp.yandex.net/images/film_big/"+String.valueOf(film_ID)+".jpg", kinoModelKp.getKPYear()));
//http://st.kp.yandex.net/images/film_big/"+film_ID+".jpg
} else {
cinemas.add(new Cinema(kinoModel.getUpdates().get(k).getTitleRU(), kinoModel.getUpdates().get(k).getTitleEN(),
kinoModel.getUpdates().get(k).getKP(), kinoModel.getUpdates().get(k).getToken(), kinoModel.getUpdates().get(k).getType(),
kinoModel.getUpdates().get(k).getCAM(), kinoModel.getUpdates().get(k).getIframe(), kinoModel.getUpdates().get(k).getTranslate(),
kinoModel.getUpdates().get(k).getTranslateID(), kinoModel.getUpdates().get(k).getAdded(), "", 0));
}
}
}
});
t.start();
try {
t.join();
mProgressBar.setVisibility(View.GONE);
cinemaAdapter = new CinemaAdapter(getActivity(), cinemas);
cinemaAdapter.notifyDataSetChanged();
// настраиваем список
lvMain.setAdapter(cinemaAdapter);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
getCinemaFrag()
вызываю из onCreateView
при этом пока грузятся данные висит интерфес. то не так сделал? public interface APIKp
{
@GET("/getFilm")
Response getKp(@Query("filmID") int film_ID);
}
public void getCinemaFrag()
{
// Создаем адаптер с базовым адресом
Retrofit.Builder builder = new Retrofit.Builder();
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(SOURCE_MOON)
.addConverterFactory(GsonConverterFactory.create())
.build();
Retrofit retrofit2 = new Retrofit.Builder()
.baseUrl(SOURCE_KP)
.addConverterFactory(GsonConverterFactory.create())
.build();
// Создаём сервисы API
apiMoon = retrofit.create(APIMoon.class);
apiKp= retrofit2.create(APIKp.class);
Thread t = new Thread(new Runnable(){
@Override
public void run(){
mProgressBar.setVisibility(View.VISIBLE);
// Получаем данные
cinemas.clear();
Call<KinoModel> call = apiMoon.getCinema(API_TOKEN);
call.enqueue(new Callback<KinoModel>() {
@Override
public void onResponse(Call<KinoModel> call, Response<KinoModel> response) {
try {
kinoModel = call.execute().body();
} catch (IOException e) {
e.printStackTrace();
}
if(response.isSuccess())
{
for (int k = 0; k < kinoModel.getNumUpdates(); k++) {
film_ID = kinoModel.getUpdates().get(k).getKP();
if (film_ID != 0) {
Call<KinoModelKp> call2 = apiKp.getKp(film_ID);
try {
kinoModelKp = call2.execute().body();
} catch (IOException e) {
e.printStackTrace();
}
if (film_ID != 0) {
cinemas.add(new Cinema(kinoModel.getUpdates().get(k).getTitleRU(), kinoModel.getUpdates().get(k).getTitleEN(),
kinoModel.getUpdates().get(k).getKP(), kinoModel.getUpdates().get(k).getToken(), kinoModel.getUpdates().get(k).getType(),
kinoModel.getUpdates().get(k).getCAM(), kinoModel.getUpdates().get(k).getIframe(), kinoModel.getUpdates().get(k).getTranslate(),
kinoModel.getUpdates().get(k).getTranslateID(), kinoModel.getUpdates().get(k).getAdded(), "http://st.kp.yandex.net/images/" + kinoModelKp.getPoster(), kinoModelKp.getKPYear(), kinoModelKp.getDescription()));
//http://st.kp.yandex.net/images/film_big/"+film_ID+".jpg
} else {
cinemas.add(new Cinema(kinoModel.getUpdates().get(k).getTitleRU(), kinoModel.getUpdates().get(k).getTitleEN(),
kinoModel.getUpdates().get(k).getKP(), kinoModel.getUpdates().get(k).getToken(), kinoModel.getUpdates().get(k).getType(),
kinoModel.getUpdates().get(k).getCAM(), kinoModel.getUpdates().get(k).getIframe(), kinoModel.getUpdates().get(k).getTranslate(),
kinoModel.getUpdates().get(k).getTranslateID(), kinoModel.getUpdates().get(k).getAdded(), "", 0, "описание отсутствует..."));
}
call2.enqueue(new Callback<KinoModelKp>() {
@Override
public void onResponse(Call<KinoModelKp> call2, Response<KinoModelKp> response) {
if (response.isSuccess()) {
Log.d("myLogs", "OK!");
mProgressBar.setVisibility(View.GONE);
cinemaAdapter = new CinemaAdapter(getActivity(), cinemas);
cinemaAdapter.notifyDataSetChanged();
// настраиваем список
lvMain.setAdapter(cinemaAdapter);
}
}
@Override
public void onFailure(Call<KinoModelKp> call, Throwable t) {
Log.d("myLogs", "NOT OK!");
}
});
}
}
}
}
@Override
public void onFailure(Call<KinoModel> call, Throwable t) {
Log.d("myLogs", "NOT OK!");
}
});
}
});
t.start();
}
}
kinoModel = call.execute().body();
java.lang.IllegalStateException: Already executed.
public void getCinemaFrag()
{
// Создаем адаптер с базовым адресом
Retrofit.Builder builder = new Retrofit.Builder();
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(SOURCE_MOON)
.addConverterFactory(GsonConverterFactory.create())
.build();
Retrofit retrofit2 = new Retrofit.Builder()
.baseUrl(SOURCE_KP)
.addConverterFactory(GsonConverterFactory.create())
.build();
// Создаём сервисы API
apiMoon = retrofit.create(APIMoon.class);
apiKp= retrofit2.create(APIKp.class);
mProgressBar.setVisibility(View.VISIBLE);
// Получаем данные
cinemas.clear();
Call<KinoModel> call = apiMoon.getCinema(API_TOKEN);
call.enqueue(new Callback<KinoModel>() {
@Override
public void onResponse(Call<KinoModel> call, Response<KinoModel> response) {
if (!call.isExecuted()) {
try {
kinoModel = call.execute().body();
} catch (IOException e) {
e.printStackTrace();
}
for (int k = 0; k < kinoModel.getNumUpdates(); k++) {
film_ID = kinoModel.getUpdates().get(k).getKP();
if (film_ID != 0) {
Call<KinoModelKp> call2 = apiKp.getKp(film_ID);
try {
if (!call2.isExecuted()) {
kinoModelKp = call2.execute().body();
}
} catch (IOException e) {
e.printStackTrace();
}
if (film_ID != 0) {
cinemas.add(new Cinema(kinoModel.getUpdates().get(k).getTitleRU(), kinoModel.getUpdates().get(k).getTitleEN(),
kinoModel.getUpdates().get(k).getKP(), kinoModel.getUpdates().get(k).getToken(), kinoModel.getUpdates().get(k).getType(),
kinoModel.getUpdates().get(k).getCAM(), kinoModel.getUpdates().get(k).getIframe(), kinoModel.getUpdates().get(k).getTranslate(),
kinoModel.getUpdates().get(k).getTranslateID(), kinoModel.getUpdates().get(k).getAdded(), "http://st.kp.yandex.net/images/" + kinoModelKp.getPoster(), kinoModelKp.getKPYear(), kinoModelKp.getDescription()));
//http://st.kp.yandex.net/images/film_big/"+film_ID+".jpg
} else {
cinemas.add(new Cinema(kinoModel.getUpdates().get(k).getTitleRU(), kinoModel.getUpdates().get(k).getTitleEN(),
kinoModel.getUpdates().get(k).getKP(), kinoModel.getUpdates().get(k).getToken(), kinoModel.getUpdates().get(k).getType(),
kinoModel.getUpdates().get(k).getCAM(), kinoModel.getUpdates().get(k).getIframe(), kinoModel.getUpdates().get(k).getTranslate(),
kinoModel.getUpdates().get(k).getTranslateID(), kinoModel.getUpdates().get(k).getAdded(), "", 0, "описание отсутствует..."));
}
call2.enqueue(new Callback<KinoModelKp>() {
@Override
public void onResponse(Call<KinoModelKp> call2, Response<KinoModelKp> response2) {
Log.d("myLogs", "OK!");
mProgressBar.setVisibility(View.GONE);
cinemaAdapter = new CinemaAdapter(getActivity(), cinemas);
cinemaAdapter.notifyDataSetChanged();
// настраиваем список
lvMain.setAdapter(cinemaAdapter);
}
@Override
public void onFailure(Call<KinoModelKp> call, Throwable t) {
Log.d("myLogs", "NOT OK!");
}
});
}
}
}
}
@Override
public void onFailure(Call<KinoModel> call, Throwable t) {
Log.d("myLogs", "NOT OK!");
}
});
}
/**
* Synchronously send the request and return its response.
*
* @throws IOException if a problem occurred talking to the server.
* @throws RuntimeException (and subclasses) if an unexpected error occurs creating the request
* or decoding the response.
*/
Response<T> execute() throws IOException;
/**
* Asynchronously send the request and notify {@code callback} of its response or if an error
* occurred talking to the server, creating the request, or processing the response.
*/
void enqueue(Callback<T> callback);
public void getCinemaFrag()
{
// Создаем адаптер с базовым адресом
Retrofit.Builder builder = new Retrofit.Builder();
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(SOURCE_MOON)
.addConverterFactory(GsonConverterFactory.create())
.build();
Retrofit retrofit2 = new Retrofit.Builder()
.baseUrl(SOURCE_KP)
.addConverterFactory(GsonConverterFactory.create())
.build();
// Создаём сервисы API
apiMoon = retrofit.create(APIMoon.class);
apiKp= retrofit2.create(APIKp.class);
Thread t = new Thread(new Runnable(){
@Override
public void run(){
mProgressBar.setVisibility(View.VISIBLE);
// Получаем данные
cinemas.clear();
Call<KinoModel> call = apiMoon.getCinema(API_TOKEN);
try {
kinoModel = call.execute().body();
} catch (IOException e) {
e.printStackTrace();
}
if (call.isExecuted()) {
for (int k = 0; k < kinoModel.getNumUpdates(); k++) {
film_ID = kinoModel.getUpdates().get(k).getKP();
if (film_ID != 0) {
Call<KinoModelKp> call2 = apiKp.getKp(film_ID);
try {
kinoModelKp = call2.execute().body();
} catch (IOException e) {
e.printStackTrace();
}
cinemas.add(new Cinema(kinoModel.getUpdates().get(k).getTitleRU(), kinoModel.getUpdates().get(k).getTitleEN(),
kinoModel.getUpdates().get(k).getKP(), kinoModel.getUpdates().get(k).getToken(), kinoModel.getUpdates().get(k).getType(),
kinoModel.getUpdates().get(k).getCAM(), kinoModel.getUpdates().get(k).getIframe(), kinoModel.getUpdates().get(k).getTranslate(),
kinoModel.getUpdates().get(k).getTranslateID(), kinoModel.getUpdates().get(k).getAdded(), "http://st.kp.yandex.net/images/" + kinoModelKp.getPoster(), kinoModelKp.getKPYear(), kinoModelKp.getDescription()));
}
else
{
cinemas.add(new Cinema(kinoModel.getUpdates().get(k).getTitleRU(), kinoModel.getUpdates().get(k).getTitleEN(),
kinoModel.getUpdates().get(k).getKP(), kinoModel.getUpdates().get(k).getToken(), kinoModel.getUpdates().get(k).getType(),
kinoModel.getUpdates().get(k).getCAM(), kinoModel.getUpdates().get(k).getIframe(), kinoModel.getUpdates().get(k).getTranslate(),
kinoModel.getUpdates().get(k).getTranslateID(), kinoModel.getUpdates().get(k).getAdded(), "", 0, "описание отсутствует..."));
}
}
}
}
});
t.start();
try {
t.join();
mProgressBar.setVisibility(View.GONE);
cinemaAdapter = new CinemaAdapter(getActivity(), cinemas);
cinemaAdapter.notifyDataSetChanged();
// настраиваем список
lvMain.setAdapter(cinemaAdapter);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
public void getCinemaFrag()
{
// Создаем адаптер с базовым адресом
Retrofit.Builder builder = new Retrofit.Builder();
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(SOURCE_MOON)
.addConverterFactory(GsonConverterFactory.create())
.build();
Retrofit retrofit2 = new Retrofit.Builder()
.baseUrl(SOURCE_KP)
.addConverterFactory(GsonConverterFactory.create())
.build();
// Создаём сервисы API
apiMoon = retrofit.create(APIMoon.class);
apiKp= retrofit2.create(APIKp.class);
mProgressBar.setVisibility(View.VISIBLE);
// Получаем данные
cinemas.clear();
Call<KinoModel> call = apiMoon.getCinema(API_TOKEN);
call.enqueue(new Callback<KinoModel>() {
@Override
public void onResponse(Call<KinoModel> call, Response<KinoModel> response)
{
for (int k = 0; k < response.body().getNumUpdates(); k++) {
film_ID = response.body().getUpdates().get(k).getKP();
if (film_ID != 0) {
Call<KinoModelKp> call2 = apiKp.getKp(film_ID);
models[0]=response.body().getUpdates().get(k).getTitleRU();
models[1]=response.body().getUpdates().get(k).getTitleEN();
models[2]=String.valueOf(response.body().getUpdates().get(k).getKP());
models[3]=response.body().getUpdates().get(k).getToken();
models[4]=response.body().getUpdates().get(k).getType();
models[5]=String.valueOf(response.body().getUpdates().get(k).getCAM());
models[6]=response.body().getUpdates().get(k).getIframe();
models[7]=response.body().getUpdates().get(k).getTranslate();
models[8]=response.body().getUpdates().get(k).getTranslateID();
models[9]=response.body().getUpdates().get(k).getAdded();
call2.enqueue(new Callback<KinoModelKp>() {
@Override
public void onResponse(Call<KinoModelKp> call2, Response<KinoModelKp> response2) {
cinemas.add(new Cinema(models[0], models[1],
models[2], models[3], models[4],
models[5], models[6], models[7],
models[8], models[9], "http://kinopoisk.ru/images/" + response2.body().getPoster(), String.valueOf(response2.body().getKPYear()), response2.body().getDescription()));
}
@Override
public void onFailure(Call<KinoModelKp> call2, Throwable t) {
Log.d("myLogs", "Failed!!! KP ");
}
});
}
else
{
cinemas.add(new Cinema(response.body().getUpdates().get(k).getTitleRU(), response.body().getUpdates().get(k).getTitleEN(),
String.valueOf(response.body().getUpdates().get(k).getKP()), response.body().getUpdates().get(k).getToken(), response.body().getUpdates().get(k).getType(),
String.valueOf(response.body().getUpdates().get(k).getCAM()), response.body().getUpdates().get(k).getIframe(), response.body().getUpdates().get(k).getTranslate(),
response.body().getUpdates().get(k).getTranslateID(), response.body().getUpdates().get(k).getAdded(), "", "0", "описание отсутствует..."));
}
if(k==response.body().getNumUpdates()-1)
{
mProgressBar.setVisibility(View.GONE);
cinemaAdapter = new CinemaAdapter(getActivity(), cinemas);
cinemaAdapter.notifyDataSetChanged();
// настраиваем список
lvMain.setAdapter(cinemaAdapter);
}
}
}
@Override
public void onFailure(Call<KinoModel> call, Throwable t) {
Log.d("myLogs", "Failed!!!");
}
});
}
@Expose
private List<Upd> updates;
public List<Upd> getUpdates()
{
return updates;
}
public class Upd {
@SerializedName("title_ru")
private String title_ru;
}