Roman Kozin
Roman Kozin

Reputation: 316

Unable to read URL string from json and apply it to Object in Android

I'm building an application and have a User object:

public class User {
    private Integer id = null;
    private String username = null;
    private String password = null;
    private String first_name = null;
    private String last_name = null;
    private String email = null;
    private String phone = null;
    private String avatar = null;
    private String role = null;
    private Boolean confirmed = null;
    private Boolean active = null;
    private String created_at = null;
    private String updated_at = null;
    private String sex = null;
    private Object company = null;
}

This object(class) contains setters and getters. When authorization response has come - i'm proccessing it with next callback:

public void onResponse(Call<ApiResponse> call, Response<ApiResponse> response) {
                ApiResponse apiResponse = response.body();

                Log.i("log_i", apiResponse.getData().toString());

                JsonReader jsonResponse = new JsonReader(new StringReader(apiResponse.getData().toString()));
                jsonResponse.setLenient(true);

                try {
                    User user = new Gson().fromJson(jsonResponse, User.class);
                    Log.i("log_i", user.toString());
                    Toast.makeText(context, apiResponse.getMessage(), Toast.LENGTH_SHORT).show();
                } catch (Exception e) {
                    Log.i("log_i", e.getMessage());
                    Toast.makeText(context, "Failed to authorize in case of application exception.", Toast.LENGTH_SHORT).show();
                }
            }

After it step to firt line after try{} - it's crashes and next error is apperas in Log:

I/log_i: com.google.gson.stream.MalformedJsonException: Unterminated object at line 1 column 147 path $.avatar

Logged JSON:

{
    id=4,
    username=admin,
    first_name=Test,
    last_name=User,
    [email protected],
    phone=012.345.678,
    confirmed=true,
    active=true,
    avatar=https://path/to/avatar.png,
    role=admin,
    created_at=2017-02-24T14: 22: 50+00: 00,
    updated_at=null,
    company_id=2.0,
    sex=m,
    company={
        id=2.0,
        title=companyTitle,
        active=true,
        created_at=2017-02-24T14: 22: 50+00: 00,
        updated_at=null,
        deleted_at=2017-02-24T14: 22: 50+00: 00
    }
}

Any help will be appreciated. Cause i'm trying to solve this issue

UPD 1

On next line i'm getting error:

User user = new Gson().fromJson(jsonResponse, User.class);

Upvotes: 1

Views: 252

Answers (1)

Roman Kozin
Roman Kozin

Reputation: 316

Problem has been solved. My actions was next: Instad of declaring:

User user = new Gson().fromJson(jsonResponse, User.class);

I should declare:

user = (User) apiUserResponse.getData();
// or
User user = (User) apiUserResponse.getData();

In other cases, Your JSON will be readed as malformed and exception will be thrown.

Upvotes: 1

Related Questions