Back

Unable to get User Name from current Session

  • 0
  • Android
  • Accounts
  • Users
asapsonter
12 Apr, 2023, 07:46

I am trying to get name from logged In user Session and display it on a textview from the following code snippet.

TypeScript
   private void getUserName(){
        client = new Client(requireActivity())
                .setEndpoint(endpoint)
                .setProject(project);
        account = new Account(client);
        account.getSession("current",
                new CoroutineCallback<>((result, error)-> {
                    if (error != null) {
                        error.printStackTrace();
                    }else{
                        // pass `result` in to a json object
                        try {
                            JSONObject jsonObject = new JSONObject(String.valueOf(result));
                            //get parameter "name" from the json object
                            String name = jsonObject.getString("name");
                            // pass the name parameter into textview
                            binding.userNameDisplay.setText(name);
                          
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                }
                        ));
    }

I get this exception Error org.json.JSONException: Value Session(id of type java.lang.String cannot be converted to JSONObject

TL;DR
The user is unable to retrieve the user name from the current session in their Android app using the Appwrite SDK. They've attempted to use the `toString()` method as suggested in the documentation, but it did not solve the issue. They've shared a log showing the session details, but it is not in JSON format. The user wants to display the user name on a textview. They've provided the code snippet they are using, but it results in a `JSONException`.
Arth
12 Apr, 2023, 07:53

can you log the result to check its shape

Arth
12 Apr, 2023, 07:54

which lang is that?

asapsonter
12 Apr, 2023, 08:04

Java. I did the log. Its is not in JSON. Weird stuff. they is no name feild. But i can see the user name on the appwrite console. All i can see on the log is id createdAt etc

Arth
12 Apr, 2023, 08:05

can you show me the output

Arth
12 Apr, 2023, 08:05

android sdk?

asapsonter
12 Apr, 2023, 08:08
TypeScript
 JSON Error              syc...scan  E  The result string is not a valid JSON object: Session(id=643664af54b3546543bf, createdAt=2023-04-12T07:58:39.350+00:00, userId=643612e4b8d495e5f32a, expire=2024-04-11 07:58:39.350, provider=email, providerUid=********, providerAccessToken=, providerAccessTokenExpiry=, providerRefreshToken=, ip=********, osCode=AND, osName=Android, osVersion=12, clientType=browser, clientCode=AN, clientName=Android Browser, clientVersion=, clientEngine=WebKit, clientEngineVersion=, deviceName=smartphone, deviceBrand=Samsung, deviceModel=SM-A136B, countryCode=--, countryName=Unknown, current=true)
Arth
12 Apr, 2023, 08:20

noo, just the result before parsing it

asapsonter
12 Apr, 2023, 09:08
TypeScript
 Session(id=643674b62e1330fa1564, createdAt=2023-04-12T09:07:02.501+00:00, userId=Sam, expire=2024-04-11 09:07:02.501, provider=email, providerUid=*****************, providerAccessToken=, providerAccessTokenExpiry=, providerRefreshToken=, ip=************, osCode=AND, osName=Android, osVersion=12, clientType=browser, clientCode=AN, clientName=Android Browser, clientVersion=, clientEngine=WebKit, clientEngineVersion=, deviceName=smartphone, deviceBrand=Samsung, deviceModel=SM-A136B, countryCode=--, countryName=Unknown, current=true)
asapsonter
12 Apr, 2023, 09:09

Its still same log

Arth
12 Apr, 2023, 10:30

there's a toString() method for the result object according to this can you try that https://appwrite.io/docs/client/account?sdk=android-java#accountGetSession

Reply

Reply to this thread by joining our Discord

Reply on Discord

Need support?

Join our Discord

Get community support by joining our Discord server.

Join Discord

Get premium support

Join Appwrite Pro and get email support from our team.

Learn more