I've the following app that communite with api in php.
I'm using Retrofit to do it, using GET method, i'm sending an int, to return items via that int .
Mysql Table Series :
id
Mysql Table Episodes:
id
f_id (Foreign key to Series) .
Activity is loading the series items, when clicked, it send the series id to the GET, and it returns items, but here's the problem, it log an error, but in the Mysql side, the query is correct .
Example, it sends "1" as series id, the result is (i used echo $query to check) :
SELECT * FROM Episodes WHERE Episodes.series_f_key= 1 ORDER BY Episodes.id DESC {"MyDatabase":[{"id":"1","title":"1","image":"TestImge.jpg","link":"https://www.youtube.com/watch?v=uzgp65UnPxA","series":"\u062a\u062c\u0631\u0628\u0629","series_f_key":"1"}]}
It's correct, but in the Android client side, it says :
com.google.gson.stream.MalformedJsonException: Use JsonReader.setLenient(true) to accept malformed JSON at line 3 column 1 path $
Also, I'm sending an Arabic query, the same problem is happening, the query is correct, and the "echo $query" respond is like this :
{"MyDatabase":[{"id":"1","title":"\u062a\u062c\u0631\u0628\u0629","image":"Test.jpg","country":"\u0633\u0648\u0631\u064a\u0627"}]}
Android client side :
com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was BEGIN_ARRAY at line 3 column 2 path $
Other GET are working without any problems, only these are causing issues.
Retrofit Code :
private void MakeConnection(final EpisodesAdapter Adapter,int id){
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(Constants.BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.build();
ReApi Api = retrofit.create(ReApi.class);
Connection = Api.GetEpisodes(id);
Connection.enqueue(new Callback<Model>() {
@Override
public void onResponse(Call<Model> call, Response<Model> response) {
List<EpisodeItem> LatestItems = response.body().GetList();
for (int i = 0; i < LatestItems.size(); i++) {
if (Items == null)
Items = new ArrayList<>();
Items.add(LatestItems.get(i));
}
Adapter.notifyDataSetChanged();
}
@Override
public void onFailure(Call<Model> call, Throwable t) {
Toast.makeText(EpisodesActivity.this, getResources().getString(R.string.couldnotfindanything), Toast.LENGTH_LONG).show();
t.printStackTrace();
}
});
}
private interface ReApi{
@GET("myapitest.php")
Call<Model> GetEpisodes(@Query("getbyid")int id);
}
private class Model{
private List<EpisodeItem> MyDatabase;
public List<EpisodeItem> GetList() {
return MyDatabase;
}
public void SetList(List<EpisodeItem> rt) {
this.MyDatabase= rt;
}
}
Model Class :
private int id;
private String title;
private String link;
private String image;
private String series;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getLink() {
return link;
}
public void setLink(String link) {
this.link = link;
}
public String getImage() {
return image;
}
public void setImage(String image) {
this.image = image;
}
public String getSeries() {
return series;
}
public void setSeries(String series) {
this.series = series;
}
Request Sample :
200 OK http://www.syriantc.com/ApiTest/apifile.php?getbyepisode=1 (313ms)
06-07 09:33:49.107 29654-29863/com.abohani.test D/OkHttp: Date: Tue, 07 Jun 2016 06:33:46 GMT
06-07 09:33:49.107 29654-29863/com.abohani.test D/OkHttp: Server: Apache/2.4.18
06-07 09:33:49.107 29654-29863/com.abohani.test D/OkHttp: X-Powered-By: PHP/5.4.45
06-07 09:33:49.107 29654-29863/com.abohani.test D/OkHttp: Vary: Accept-Encoding,User-Agent
06-07 09:33:49.107 29654-29863/com.abohani.test D/OkHttp: Keep-Alive: timeout=5
06-07 09:33:49.107 29654-29863/com.abohani.test D/OkHttp: Connection: Keep-Alive
06-07 09:33:49.107 29654-29863/com.abohani.test D/OkHttp: Content-Type: text/html
06-07 09:33:49.107 29654-29863/com.abohani.test D/OkHttp: OkHttp-Sent-Millis: 1465281228946
06-07 09:33:49.107 29654-29863/com.abohani.test D/OkHttp: OkHttp-Received-Millis: 1465281229106
06-07 09:33:49.108 29654-29863/com.abohani.test D/OkHttp:
06-07 09:33:49.108 29654-29863/com.abohani.test D/OkHttp: SELECT * FROM Episodes WHERE Episodes.series_f_key= 1 ORDER BY Episodes.id DESC
06-07 09:33:49.108 29654-29863/com.abohani.test D/OkHttp: {"MyDatabase":[{"id":"1","title":"1","image":"Test.jpg","link":"https://drive.google.com/file/d/0B8UrrDgeyvYJMXdtMXdYeEFnLVU/view","series":"\u062a\u062c\u0631\u0628\u0 629","series_f_key":"1"}]}
06-07 09:33:49.108 29654-29863/com.abohani.test D/OkHttp: <-- END HTTP (284-byte body)