Не получается распарсить api с помощью retrofit2
Пытаюсь распарсить https://favqs.com/api/ с помощью retorfit2 Для начала хочу хотя бы извлечь дату из json
@Generated("jsonschema2pojo")
public class Example {
@SerializedName("qotd_date")
@Expose
private String qotdDate;
@SerializedName("quote")
@Expose
private Quote quote;
public String getQotdDate() {
return qotdDate;
}
public void setQotdDate(String qotdDate) {
this.qotdDate = qotdDate;
}
public Quote getQuote() {
return quote;
}
public void setQuote(Quote quote) {
this.quote = quote;
}
}
класс Quote:
@Generated("jsonschema2pojo")
public class Quote {
@SerializedName("id")
@Expose
private Integer id;
@SerializedName("dialogue")
@Expose
private Boolean dialogue;
@SerializedName("private")
@Expose
private Boolean _private;
@SerializedName("tags")
@Expose
private List<Object> tags = null;
@SerializedName("url")
@Expose
private String url;
@SerializedName("favorites_count")
@Expose
private Integer favoritesCount;
@SerializedName("upvotes_count")
@Expose
private Integer upvotesCount;
@SerializedName("downvotes_count")
@Expose
private Integer downvotesCount;
@SerializedName("author")
@Expose
private String author;
@SerializedName("author_permalink")
@Expose
private String authorPermalink;
@SerializedName("body")
@Expose
private String body;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public Boolean getDialogue() {
return dialogue;
}
public void setDialogue(Boolean dialogue) {
this.dialogue = dialogue;
}
public Boolean getPrivate() {
return _private;
}
public void setPrivate(Boolean _private) {
this._private = _private;
}
public List<Object> getTags() {
return tags;
}
public void setTags(List<Object> tags) {
this.tags = tags;
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
public Integer getFavoritesCount() {
return favoritesCount;
}
public void setFavoritesCount(Integer favoritesCount) {
this.favoritesCount = favoritesCount;
}
public Integer getUpvotesCount() {
return upvotesCount;
}
public void setUpvotesCount(Integer upvotesCount) {
this.upvotesCount = upvotesCount;
}
public Integer getDownvotesCount() {
return downvotesCount;
}
public void setDownvotesCount(Integer downvotesCount) {
this.downvotesCount = downvotesCount;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public String getAuthorPermalink() {
return authorPermalink;
}
public void setAuthorPermalink(String authorPermalink) {
this.authorPermalink = authorPermalink;
}
public String getBody() {
return body;
}
public void setBody(String body) {
this.body = body;
}
}
интерфейс Client:
public interface Client {
@GET("qotd_date")
Call<List<Example>> reposForUser(@Path("user") String user);
}
класс MyAdapter:
public class MyAdapter extends ArrayAdapter<Example> {
private Context context;
private List<Example> values;
public MyAdapter(Context context, List<Example> values) {
super(context, R.layout.list_item_pagination, values);
this.context = context;
this.values = values;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
if (row == null) {
LayoutInflater inflater =
(LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
row = inflater.inflate(R.layout.list_item_pagination, parent, false);
}
TextView textView = (TextView) row.findViewById(R.id.list_item_pagination_text);
Example item = values.get(position);
String message = item.getQotdDate();
textView.setText(message);
return row;
}
}
класс MainActivity:
public class MainActivity extends AppCompatActivity {
private ListView listView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView = (ListView) findViewById(R.id.pagination_list);
Retrofit.Builder builder = new Retrofit.Builder()
.baseUrl("https://favqs.com/api/")
.addConverterFactory(GsonConverterFactory.create());
Retrofit retrofit = builder.build();
Client client = retrofit.create(Client.class);
Call<List<Example>> call = client.reposForUser("qotd_date");
call.enqueue(new Callback<List<Example>>() {
@Override
public void onResponse(Call<List<Example>> call, Response<List<Example>> response) {
List<Example> repos = response.body();
listView.setAdapter(new MyAdapter(MainActivity.this, repos));
}
@Override
public void onFailure(Call<List<Example>> call, Throwable t) {
Toast.makeText(MainActivity.this, "error :(", Toast.LENGTH_SHORT).show();
}
});
}
}
К сожалению, приложение не работатет, и как это исправить, я не понимаю
При запуске появляется ошибка RuntimeException в строке Call<List<Example>> call = client.reposForUser("qotd_date");
Текст ошибки:
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.semenov.retrofitproject, PID: 10813
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.semenov.retrofitproject/com.semenov.retrofitproject.ui.MainActivity}: java.lang.IllegalArgumentException: URL "qotd_date" does not contain "{user}". (parameter #1)
for method GitHubClient.reposForUser
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3450)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3602)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:85)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2067)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:223)
at android.app.ActivityThread.main(ActivityThread.java:7697)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:952)
Caused by: java.lang.IllegalArgumentException: URL "qotd_date" does not contain "{user}". (parameter #1)
for method GitHubClient.reposForUser
at retrofit2.Utils.methodError(Utils.java:54)
at retrofit2.Utils.methodError(Utils.java:43)
at retrofit2.Utils.parameterError(Utils.java:64)
at retrofit2.RequestFactory$Builder.validatePathName(RequestFactory.java:829)
at retrofit2.RequestFactory$Builder.parseParameterAnnotation(RequestFactory.java:416)
at retrofit2.RequestFactory$Builder.parseParameter(RequestFactory.java:325)
at retrofit2.RequestFactory$Builder.build(RequestFactory.java:206)
at retrofit2.RequestFactory.parseAnnotations(RequestFactory.java:67)
at retrofit2.ServiceMethod.parseAnnotations(ServiceMethod.java:26)
at retrofit2.Retrofit.loadServiceMethod(Retrofit.java:202)
at retrofit2.Retrofit$1.invoke(Retrofit.java:160)
at java.lang.reflect.Proxy.invoke(Proxy.java:1006)
at $Proxy2.reposForUser(Unknown Source)
at com.semenov.retrofitproject.ui.MainActivity.onCreate(MainActivity.java:41)
at android.app.Activity.performCreate(Activity.java:8017)
at android.app.Activity.performCreate(Activity.java:8001)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1310)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3423)
... 11 more
Также, скорее всего, я не правильно реализовал interface Client:
public interface Client {
@GET("qotd_date")
Call<List<Example>> reposForUser(@Path("user") String user);
}
Текс ошибки из метода onFailure:
W/System.err: com.google.gson.stream.MalformedJsonException: Use JsonReader.setLenient(true) to accept malformed JSON at line 1 column 1 path $
W/System.err: at com.google.gson.stream.JsonReader.syntaxError(JsonReader.java:1562)
at com.google.gson.stream.JsonReader.checkLenient(JsonReader.java:1403)
at com.google.gson.stream.JsonReader.doPeek(JsonReader.java:592)
at com.google.gson.stream.JsonReader.peek(JsonReader.java:424)
at com.google.gson.internal.bind.CollectionTypeAdapterFactory$Adapter.read(CollectionTypeAdapterFactory.java:74)
at com.google.gson.internal.bind.CollectionTypeAdapterFactory$Adapter.read(CollectionTypeAdapterFactory.java:61)
at retrofit2.converter.gson.GsonResponseBodyConverter.convert(GsonResponseBodyConverter.java:40)
at retrofit2.converter.gson.GsonResponseBodyConverter.convert(GsonResponseBodyConverter.java:27)
at retrofit2.OkHttpCall.parseResponse(OkHttpCall.java:243)
at retrofit2.OkHttpCall$1.onResponse(OkHttpCall.java:153)
at okhttp3.RealCall$AsyncCall.execute(RealCall.java:174)
at okhttp3.internal.NamedRunnable.run(NamedRunnable.java:32)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
W/System.err: at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
at java.lang.Thread.run(Thread.java:923)
