Search code examples
androidjsonmigrationgsonandroidx

Gson does not parse json String


Now I'm working on an Android SDK project, the code is too old and I tried to collect data from server using okhttp3. By using the following function I'm able to collect the response from server.

String json = requestGetString(url);

    public String requestGetString(String uri) throws IOException {
    Response res = null;
    try {
        res = getStringHttp(host+uri);
        System.out.println(res.body().string());
        return res.body().string();
    }catch (NullPointerException e) {
        //body = null ??
        throw new IOException(e);
    } catch (IOException e) {
        throw new IOException(e);
    }finally {
        if(res != null){
            try{
                res.close();
            }catch (NullPointerException e){
                //body = null ??
                throw new IOException(e);
            }
        }
    }

}

I'm receiving the Json but I'cant parse it with my data class.

like the following

 AuthAppResponse result = parseJson(path, json, AuthAppResponse.class);

You can look my parser function

private <TResponse> TResponse parseJson(String path, String json, Class<TResponse> responseClass) throws IOException
{
    return parseJson(getGson(), path, json, responseClass);
}

and this,

private <TResponse> TResponse parseJson(Gson gson, String path, String json, Class<TResponse> responseClass) throws IOException
{
    try
    {
        return gson.fromJson(json, responseClass);
    }
    catch (JsonSyntaxException e)
    {
        throw new IOException("The response from '" + path + "' failed to be parsed as JSON.\ncontent = " + json, e);
    }
}

Nothing happen.

And the same SDK with latest code base it works fine with the same code. Anyone have an idea to figure out the possible solution for this. please share your comment.


Solution

  • Finally i fount the missing part in my code, I miss to add a proguard file in the old project, that's why the Gson does not parse the response