Проблема с API Retrofit 2

Закрыто
Sergey777
Сообщения: 68
Зарегистрирован: 30 мар 2015, 17:16

Проблема с API Retrofit 2

Сообщение Sergey777 » 09 июн 2016, 19:22

Здравствуйте пытаюсь разобраться с API ucoz, но никак не могу получить 200 статус при обращении к сервису. Пример удачного ответа в json-формате:

Код: Выделить всё

{
         "page":1, 
         "pages_list":"1..32", 
         "per_page":"1", 
         "news":[{
         "other3":"a",
         "reads":20,
         "other5":"s",
         "avatar":"d",
         "other1":"g",
         "rate_num":0,
         "author":"Dmitry_DM",
         "comments":"yes",
         "rating":"0.00",
         "add_date":"2016-03-26 16:23",
         "rate_sum":0,
         "category":{
            "url":"http://yoursite.ucoz.ru/category/hello",
            "name":"Категория",
            "id":1,
            "description":"39_e"
         },
         "id":"61",
         "ontop":"no",
         "other4":"q",
         "other2":"f",
         "pending":"no",
         "entry_url":"http://yoursite.ucoz.ru/news/test/2016-03-26-61",
         "message":"аыва",
         "title":"тест",
         "hgu_title":"test" 
          } 
          ], 
          "total_news":32
          }
Или неудачного:

Код: Выделить всё

{"error":{"msg":"Missing required parameter","code":"MISSING_REQUIRED_PARAMETER"}}
POJO-объект такой:

Код: Выделить всё

 public class UcozApi {

    @SerializedName("page")
    @Expose
    private Integer page;
    @SerializedName("pages_list")
    @Expose
    private String pagesList;
    @SerializedName("per_page")
    @Expose
    private String perPage;
    @SerializedName("news")
    @Expose
    private List<News> news = new ArrayList<News>();
    @SerializedName("total_news")
    @Expose
    private Integer totalNews;
    @SerializedName("error")
    @Expose
    private Error error;

    
    public Integer getPage() {
        return page;
    }

    
    public void setPage(Integer page) {
        this.page = page;
    }

    
    public String getPagesList() {
        return pagesList;
    }

    
    public void setPagesList(String pagesList) {
        this.pagesList = pagesList;
    }

    
    public String getPerPage() {
        return perPage;
    }

    
    public void setPerPage(String perPage) {
        this.perPage = perPage;
    }

   
    public List<News> getNews() {
        return news;
    }

    
    public void setNews(List<News> news) {
        this.news = news;
    }

    
    public Integer getTotalNews() {
        return totalNews;
    }

    
    public void setTotalNews(Integer totalNews) {
        this.totalNews = totalNews;
    }

   
    public Error getError() {
        return error;
    }

    
    public void setError(Error error) {
        this.error = error;
    } 
    }


    public class News {
    @SerializedName("other3")
    @Expose
    private String other3;
    @SerializedName("reads")
    @Expose
    private Integer reads;
    @SerializedName("other5")
    @Expose
    private String other5;
    @SerializedName("avatar")
    @Expose
    private String avatar;
    @SerializedName("other1")
    @Expose
    private String other1;
    @SerializedName("rate_num")
    @Expose
    private Integer rateNum;
    @SerializedName("author")
    @Expose
    private String author;
    @SerializedName("comments")
    @Expose
    private String comments;
    @SerializedName("rating")
    @Expose
    private String rating;
    @SerializedName("add_date")
    @Expose
    private String addDate;
    @SerializedName("rate_sum")
    @Expose
    private Integer rateSum;
    @SerializedName("category")
    @Expose
    private Category category;
    @SerializedName("id")
    @Expose
    private String id;
    @SerializedName("ontop")
    @Expose
    private String ontop;
    @SerializedName("other4")
    @Expose
    private String other4;
    @SerializedName("other2")
    @Expose
    private String other2;
    @SerializedName("pending")
    @Expose
    private String pending;
    @SerializedName("entry_url")
    @Expose
    private String entryUrl;
    @SerializedName("message")
    @Expose
    private String message;
    @SerializedName("title")
    @Expose
    private String title;
    @SerializedName("hgu_title")
    @Expose
    private String hguTitle;

    
    public String getOther3() {
        return other3;
    }

    
    public void setOther3(String other3) {
        this.other3 = other3;
    }

    
    public Integer getReads() {
        return reads;
    }

    
    public void setReads(Integer reads) {
        this.reads = reads;
    }

    
    public String getOther5() {
        return other5;
    }

    
    public void setOther5(String other5) {
        this.other5 = other5;
    }

    
    public String getAvatar() {
        return avatar;
    }

    
    public void setAvatar(String avatar) {
        this.avatar = avatar;
    }

    
    public String getOther1() {
        return other1;
    }

    
    public void setOther1(String other1) {
        this.other1 = other1;
    }

    
    public Integer getRateNum() {
        return rateNum;
    }

    
    public void setRateNum(Integer rateNum) {
        this.rateNum = rateNum;
    }

    
    public String getAuthor() {
        return author;
    }

    
    public void setAuthor(String author) {
        this.author = author;
    }

    
    public String getComments() {
        return comments;
    }

    
    public void setComments(String comments) {
        this.comments = comments;
    }

    
    public String getRating() {
        return rating;
    }

    
    public void setRating(String rating) {
        this.rating = rating;
    }

    
    public String getAddDate() {
        return addDate;
    }

    
    public void setAddDate(String addDate) {
        this.addDate = addDate;
    }

    
    public Integer getRateSum() {
        return rateSum;
    }

    
    public void setRateSum(Integer rateSum) {
        this.rateSum = rateSum;
    }

    
    public Category getCategory() {
        return category;
    }

    
    public void setCategory(Category category) {
        this.category = category;
    }

    
    public String getId() {
        return id;
    }

    
    public void setId(String id) {
        this.id = id;
    }

    
    public String getOntop() {
        return ontop;
    }

    
    public void setOntop(String ontop) {
        this.ontop = ontop;
    }

    
    public String getOther4() {
        return other4;
    }

    
    public void setOther4(String other4) {
        this.other4 = other4;
    }

    
    public String getOther2() {
        return other2;
    }

    
    public void setOther2(String other2) {
        this.other2 = other2;
    }

    
    public String getPending() {
        return pending;
    }

    
    public void setPending(String pending) {
        this.pending = pending;
    }

    
    public String getEntryUrl() {
        return entryUrl;
    }

    
    public void setEntryUrl(String entryUrl) {
        this.entryUrl = entryUrl;
    }

    
    public String getMessage() {
        return message;
    }

    
    public void setMessage(String message) {
        this.message = message;
    }

    
    public String getTitle() {
        return title;
    }

    
    public void setTitle(String title) {
        this.title = title;
    }

    
    public String getHguTitle() {
        return hguTitle;
    }

    
    public void setHguTitle(String hguTitle) {
        this.hguTitle = hguTitle;
    }
    }

    public class Category {
    @SerializedName("url")
    @Expose
    private String url;
    @SerializedName("name")
    @Expose
    private String name;
    @SerializedName("id")
    @Expose
    private Integer id;
    @SerializedName("description")
    @Expose
    private String description;

    
    public String getUrl() {
        return url;
    }

    
    public void setUrl(String url) {
        this.url = url;
    }

    
    public String getName() {
        return name;
    }

    
    public void setName(String name) {
        this.name = name;
    }

    
    public Integer getId() {
        return id;
    }

    
    public void setId(Integer id) {
        this.id = id;
    }

    
    public String getDescription() {
        return description;
    }

    
    public void setDescription(String description) {
        this.description = description;
    }
    }

    public class Error {
    @SerializedName("msg")
    @Expose
    private String msg;
    @SerializedName("code")
    @Expose
    private String code;

    
    public String getMsg() {
        return msg;
    }

    
    public void setMsg(String msg) {
        this.msg = msg;
    }

    
    public String getCode() {
        return code;
    }

    
    public void setCode(String code) {
        this.code = code;
    }
    }
Интерфейс:

Код: Выделить всё

public interface UcozNewsService {
    @GET("news/")
    Call<UcozApi> getNews(
    @Query("oauth_consumer_key") String oauth_consumer_key,
    @Query("oauth_consumer_secret") String oauth_consumer_secret,
    @Query("oauth_token") String oauth_token,
    @Query("oauth_token_secret") String oauth_token_secret);
    }
Код в самом классе:

Код: Выделить всё

 ....
    Retrofit retrofit = new Retrofit.Builder()
                        .baseUrl("http://pikasso.3dn.ru/uapi/")
                        .addConverterFactory(GsonConverterFactory.create())
                        .build();
                UcozNewsService ucozNewsService = retrofit.create(UcozNewsService.class);
                Call<UcozApi> call = ucozNewsService.getNews(consumerKey, consumerSecret, oAuthToken, oAuthTokenSecret);
                try {
                    call.enqueue(new Callback<UcozApi>() {
                        @Override
                        public void onResponse(Call<UcozApi> call, Response<UcozApi> response) {
                            try {
                                if (response.isSuccessful()) {
                                    UcozApi ucozApi = response.body();
                                    Log.d(TAG, "Message: " + ucozApi.getError().getMsg() + " Code: " + ucozApi.getError().getCode());

                                } else {
                                    int status = response.code();
                                    ResponseBody errorBody = response.errorBody();
                                    Log.d(TAG, "Статус: " + status + " Сообщние: " + response.message() + " ,Ошибка: " + errorBody);
                                }
                            } catch (Exception e) {
                                Log.d(TAG, "Ошибка в onResponse", e);
                            }
                        }

                        @Override
                        public void onFailure(Call<UcozApi> call, Throwable t) {
                            Log.d(TAG, "Ошибка в onFailure: " + t);
                        }
                    });
                } catch (Exception e) {
                    Log.d(TAG, "Exception", e);
                }
В логах получаю: Статус: 400 Сообщение: Bad request ,Ошибка: okhttp3.ResponseBody$1@41c12f88

Что я делаю не так? Буду признателен получить помощи!

Sergey777
Сообщения: 68
Зарегистрирован: 30 мар 2015, 17:16

Re: Проблема с API Retrofit 2

Сообщение Sergey777 » 14 июн 2016, 09:23

Sergey777 писал(а):Здравствуйте пытаюсь разобраться с API ucoz, но никак не могу получить 200 статус при обращении к сервису. Пример удачного ответа в json-формате:

Код: Выделить всё

{
         "page":1, 
         "pages_list":"1..32", 
         "per_page":"1", 
         "news":[{
         "other3":"a",
         "reads":20,
         "other5":"s",
         "avatar":"d",
         "other1":"g",
         "rate_num":0,
         "author":"Dmitry_DM",
         "comments":"yes",
         "rating":"0.00",
         "add_date":"2016-03-26 16:23",
         "rate_sum":0,
         "category":{
            "url":"http://yoursite.ucoz.ru/category/hello",
            "name":"Категория",
            "id":1,
            "description":"39_e"
         },
         "id":"61",
         "ontop":"no",
         "other4":"q",
         "other2":"f",
         "pending":"no",
         "entry_url":"http://yoursite.ucoz.ru/news/test/2016-03-26-61",
         "message":"аыва",
         "title":"тест",
         "hgu_title":"test" 
          } 
          ], 
          "total_news":32
          }
Или неудачного:

Код: Выделить всё

{"error":{"msg":"Missing required parameter","code":"MISSING_REQUIRED_PARAMETER"}}
POJO-объект такой:

Код: Выделить всё

 public class UcozApi {

    @SerializedName("page")
    @Expose
    private Integer page;
    @SerializedName("pages_list")
    @Expose
    private String pagesList;
    @SerializedName("per_page")
    @Expose
    private String perPage;
    @SerializedName("news")
    @Expose
    private List<News> news = new ArrayList<News>();
    @SerializedName("total_news")
    @Expose
    private Integer totalNews;
    @SerializedName("error")
    @Expose
    private Error error;

    
    public Integer getPage() {
        return page;
    }

    
    public void setPage(Integer page) {
        this.page = page;
    }

    
    public String getPagesList() {
        return pagesList;
    }

    
    public void setPagesList(String pagesList) {
        this.pagesList = pagesList;
    }

    
    public String getPerPage() {
        return perPage;
    }

    
    public void setPerPage(String perPage) {
        this.perPage = perPage;
    }

   
    public List<News> getNews() {
        return news;
    }

    
    public void setNews(List<News> news) {
        this.news = news;
    }

    
    public Integer getTotalNews() {
        return totalNews;
    }

    
    public void setTotalNews(Integer totalNews) {
        this.totalNews = totalNews;
    }

   
    public Error getError() {
        return error;
    }

    
    public void setError(Error error) {
        this.error = error;
    } 
    }


    public class News {
    @SerializedName("other3")
    @Expose
    private String other3;
    @SerializedName("reads")
    @Expose
    private Integer reads;
    @SerializedName("other5")
    @Expose
    private String other5;
    @SerializedName("avatar")
    @Expose
    private String avatar;
    @SerializedName("other1")
    @Expose
    private String other1;
    @SerializedName("rate_num")
    @Expose
    private Integer rateNum;
    @SerializedName("author")
    @Expose
    private String author;
    @SerializedName("comments")
    @Expose
    private String comments;
    @SerializedName("rating")
    @Expose
    private String rating;
    @SerializedName("add_date")
    @Expose
    private String addDate;
    @SerializedName("rate_sum")
    @Expose
    private Integer rateSum;
    @SerializedName("category")
    @Expose
    private Category category;
    @SerializedName("id")
    @Expose
    private String id;
    @SerializedName("ontop")
    @Expose
    private String ontop;
    @SerializedName("other4")
    @Expose
    private String other4;
    @SerializedName("other2")
    @Expose
    private String other2;
    @SerializedName("pending")
    @Expose
    private String pending;
    @SerializedName("entry_url")
    @Expose
    private String entryUrl;
    @SerializedName("message")
    @Expose
    private String message;
    @SerializedName("title")
    @Expose
    private String title;
    @SerializedName("hgu_title")
    @Expose
    private String hguTitle;

    
    public String getOther3() {
        return other3;
    }

    
    public void setOther3(String other3) {
        this.other3 = other3;
    }

    
    public Integer getReads() {
        return reads;
    }

    
    public void setReads(Integer reads) {
        this.reads = reads;
    }

    
    public String getOther5() {
        return other5;
    }

    
    public void setOther5(String other5) {
        this.other5 = other5;
    }

    
    public String getAvatar() {
        return avatar;
    }

    
    public void setAvatar(String avatar) {
        this.avatar = avatar;
    }

    
    public String getOther1() {
        return other1;
    }

    
    public void setOther1(String other1) {
        this.other1 = other1;
    }

    
    public Integer getRateNum() {
        return rateNum;
    }

    
    public void setRateNum(Integer rateNum) {
        this.rateNum = rateNum;
    }

    
    public String getAuthor() {
        return author;
    }

    
    public void setAuthor(String author) {
        this.author = author;
    }

    
    public String getComments() {
        return comments;
    }

    
    public void setComments(String comments) {
        this.comments = comments;
    }

    
    public String getRating() {
        return rating;
    }

    
    public void setRating(String rating) {
        this.rating = rating;
    }

    
    public String getAddDate() {
        return addDate;
    }

    
    public void setAddDate(String addDate) {
        this.addDate = addDate;
    }

    
    public Integer getRateSum() {
        return rateSum;
    }

    
    public void setRateSum(Integer rateSum) {
        this.rateSum = rateSum;
    }

    
    public Category getCategory() {
        return category;
    }

    
    public void setCategory(Category category) {
        this.category = category;
    }

    
    public String getId() {
        return id;
    }

    
    public void setId(String id) {
        this.id = id;
    }

    
    public String getOntop() {
        return ontop;
    }

    
    public void setOntop(String ontop) {
        this.ontop = ontop;
    }

    
    public String getOther4() {
        return other4;
    }

    
    public void setOther4(String other4) {
        this.other4 = other4;
    }

    
    public String getOther2() {
        return other2;
    }

    
    public void setOther2(String other2) {
        this.other2 = other2;
    }

    
    public String getPending() {
        return pending;
    }

    
    public void setPending(String pending) {
        this.pending = pending;
    }

    
    public String getEntryUrl() {
        return entryUrl;
    }

    
    public void setEntryUrl(String entryUrl) {
        this.entryUrl = entryUrl;
    }

    
    public String getMessage() {
        return message;
    }

    
    public void setMessage(String message) {
        this.message = message;
    }

    
    public String getTitle() {
        return title;
    }

    
    public void setTitle(String title) {
        this.title = title;
    }

    
    public String getHguTitle() {
        return hguTitle;
    }

    
    public void setHguTitle(String hguTitle) {
        this.hguTitle = hguTitle;
    }
    }

    public class Category {
    @SerializedName("url")
    @Expose
    private String url;
    @SerializedName("name")
    @Expose
    private String name;
    @SerializedName("id")
    @Expose
    private Integer id;
    @SerializedName("description")
    @Expose
    private String description;

    
    public String getUrl() {
        return url;
    }

    
    public void setUrl(String url) {
        this.url = url;
    }

    
    public String getName() {
        return name;
    }

    
    public void setName(String name) {
        this.name = name;
    }

    
    public Integer getId() {
        return id;
    }

    
    public void setId(Integer id) {
        this.id = id;
    }

    
    public String getDescription() {
        return description;
    }

    
    public void setDescription(String description) {
        this.description = description;
    }
    }

    public class Error {
    @SerializedName("msg")
    @Expose
    private String msg;
    @SerializedName("code")
    @Expose
    private String code;

    
    public String getMsg() {
        return msg;
    }

    
    public void setMsg(String msg) {
        this.msg = msg;
    }

    
    public String getCode() {
        return code;
    }

    
    public void setCode(String code) {
        this.code = code;
    }
    }
Интерфейс:

Код: Выделить всё

public interface UcozNewsService {
    @GET("news/")
    Call<UcozApi> getNews(
    @Query("oauth_consumer_key") String oauth_consumer_key,
    @Query("oauth_consumer_secret") String oauth_consumer_secret,
    @Query("oauth_token") String oauth_token,
    @Query("oauth_token_secret") String oauth_token_secret);
    }
Код в самом классе:

Код: Выделить всё

 ....
    Retrofit retrofit = new Retrofit.Builder()
                        .baseUrl("http://pikasso.3dn.ru/uapi/")
                        .addConverterFactory(GsonConverterFactory.create())
                        .build();
                UcozNewsService ucozNewsService = retrofit.create(UcozNewsService.class);
                Call<UcozApi> call = ucozNewsService.getNews(consumerKey, consumerSecret, oAuthToken, oAuthTokenSecret);
                try {
                    call.enqueue(new Callback<UcozApi>() {
                        @Override
                        public void onResponse(Call<UcozApi> call, Response<UcozApi> response) {
                            try {
                                if (response.isSuccessful()) {
                                    UcozApi ucozApi = response.body();
                                    Log.d(TAG, "Message: " + ucozApi.getError().getMsg() + " Code: " + ucozApi.getError().getCode());

                                } else {
                                    int status = response.code();
                                    ResponseBody errorBody = response.errorBody();
                                    Log.d(TAG, "Статус: " + status + " Сообщние: " + response.message() + " ,Ошибка: " + errorBody);
                                }
                            } catch (Exception e) {
                                Log.d(TAG, "Ошибка в onResponse", e);
                            }
                        }

                        @Override
                        public void onFailure(Call<UcozApi> call, Throwable t) {
                            Log.d(TAG, "Ошибка в onFailure: " + t);
                        }
                    });
                } catch (Exception e) {
                    Log.d(TAG, "Exception", e);
                }
В логах получаю: Статус: 400 Сообщение: Bad request ,Ошибка: okhttp3.ResponseBody$1@41c12f88

Что я делаю не так? Буду признателен получить помощи!
Разобрался! Не все параметры передавал. Тему можно закрывать.

Закрыто