1

In my project i have to implement login with gmail and storing data into datastore(bigtable),but i got an exception,error code is 500.

 com.google.gson.stream.MalformedJsonException:
 Use JsonReader.setLenient(true) to accept malformed JSON at line 1 column 12 path $

The line number is 88 and the code is:

   JsonObject json = (JsonObject)new JsonParser().parse(outputString.trim());
            String access_token = json.get("access_token").getAsString();
            System.out.println(access_token);
3
  • 1
    attach the json you're trying to parse, it seems like the json is invalid Commented Apr 18, 2015 at 11:05
  • String line, outputString = ""; BufferedReader reader = new BufferedReader(new InputStreamReader( urlConn.getInputStream())); while ((line = reader.readLine()) != null) { outputString += line; } The outputstring contains the json data after parsing i want string token. Commented Apr 18, 2015 at 11:28
  • Like I said the library is complaining about your JSON being invalid, so edit it in your answer and censor any data you don't want to share. Commented Apr 18, 2015 at 11:34

1 Answer 1

4

Instead of using JsonObject, try Gson library to convert from Json to String and vice versa. For a thorough example, see this answer.

If you first create a response class and then serialize it with Gson (like shown in that answer), then you make sure that you are creating a well formatted Json String.

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

2 Comments

After i changed the code like this: gson = new Gson(); String access_token =gson.toJson(outputString); i got the error Illegal character in query at index 59: googleapis.com/oauth2/v1/userinfo?access_token="\u003c!DOCTYPE html\u003e\u003chtml
Rohan is right. You need to rightly structure your outputString response. It cannot be null. Apart from that, in order to use the Gson library, you need to store your information in a object first, and only then convert it to json. Debug adding a breakpoint on your outputString variable and post its value.

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.