1

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);
8
  • I don't see where you define context in your onResponse in your try that you pass to Toast.makeText also can you specify which line is giving the error is it where Declare and set jsonResponse? Commented Mar 31, 2017 at 22:18
  • @user3299379, it's asyncTask. I'm passing Activity context into constructor. I will upd question to highlight problem. Commented Mar 31, 2017 at 22:20
  • @user3299379, also Toasts working. There's np with them Commented Mar 31, 2017 at 22:22
  • can you try the following: Gson gson = new GsonBuilder().create(); User user = gson.fromJson(jsonResponse, User.class); to replace the line that is currently showing error Commented Mar 31, 2017 at 22:28
  • @user3299379, sure. Let me try. Commented Mar 31, 2017 at 22:29

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.