0

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)
Jaeger
  • 1,646
  • 8
  • 27
  • 59

2 Answers2

0

It looks like your exception message is telling you what to do:

Use JsonReader.setLenient(true) 

Also, this message is telling you that you need to change your JSON parsing since its expecting an Object but its getting an array:

 Expected BEGIN_OBJECT but was BEGIN_ARRAY

Your example JSON looks like: {"MyDatabase":[{"id":". It seems like you are not accounting for the outer Object with key 'MyDatabase' in your serialization. You could add another model class for MyDatabase that then contains the list of episode items.

Eric Levine
  • 13,536
  • 5
  • 49
  • 49
  • I've seen the "JsonReader", when using it says can't reference from static,. – Jaeger Jun 06 '16 at 18:16
  • @AboHaniCall I'm sure you are doing something like `JsonReader reader = new JsonReader(...` in your code. After that, call `reader.setLenient(true)` – Eric Levine Jun 06 '16 at 18:46
  • I've did it, searched Stackoverflow and saw this http://stackoverflow.com/questions/27485346/malformedjsonexception-with-retrofit-api and other answers, the error expanded to another un-related problem, that didn't help at all, for the second one, the code expects Array, i don't know where i typed to expects an Object. – Jaeger Jun 06 '16 at 18:59
  • @AboHani I added a suggestion for fixing the JSON parsing to my answer. – Eric Levine Jun 06 '16 at 19:45
  • Updated the OP, I'm already taking care of it in "class Model", which used in Retrofit, I've the same code that use another GET param, and doesn't show errors like this one. – Jaeger Jun 06 '16 at 20:15
  • Update: for the search query, it was a mistake from searchview, the code encode it and it sent a weird string as a query. for the first problem, I've added the Android Client side request sample, i think the problem from"u062a ..etc" maybe ? . – Jaeger Jun 07 '16 at 06:27
0

elevine is correct. I wanted to add a citation and more information.

This link explains further. The issue is that there is some sort of problem with your json object but it is recognized mostly correct. Usually white space at the end causes this.

Community
  • 1
  • 1