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);
Burger King
  • 2,945
  • 3
  • 20
  • 45
Nagaraj Kandoor
  • 305
  • 5
  • 18
  • 1
    attach the json you're trying to parse, it seems like the json is invalid – Rohan Apr 18 '15 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. – Nagaraj Kandoor Apr 18 '15 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. – Rohan Apr 18 '15 at 11:34

1 Answers1

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.

Community
  • 1
  • 1
Cris
  • 2,002
  • 4
  • 30
  • 51
  • 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: https://www.googleapis.com/oauth2/v1/userinfo?access_token="\u003c!DOCTYPE html\u003e\u003chtml – Nagaraj Kandoor Apr 18 '15 at 11:49
  • 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. – Cris Apr 18 '15 at 12:33