1

I am trying to parse a json object using gson in Android application ,the test passed quite gracefully in emulator ,while i used the actual device ,the problems started I am getting the exception as follows

.....: com.google.gson.JsonParseException: Failed parsing JSON source: java.io.BufferedReader@44848f38 to Json
......: print stack Failed parsing JSON source: java.io.BufferedReader@44848f38 to Json
.......:com.google.gson.JsonParseException: Failed parsing JSON source: java.io.BufferedReader@44848f38 to Json
.......:    at com.google.gson.JsonParser.parse(JsonParser.java:59)
........:    at com...xx..allmenus.DiningDetail$3.run(DiningDetail.java:361)
........: Caused by: com.google.gson.ParseException: Encountered " <IDENTIFIER_SANS_EXPONENT> "HTTP "" at line 1, column 1.
07-29 11:33:55.186: WARN/System.err(2110): Was expecting one of:
07-29 11:33:55.186: WARN/System.err(2110):     <EOF> 
07-29 11:33:55.186: WARN/System.err(2110):     <DIGITS> ...
07-29 11:33:55.186: WARN/System.err(2110):     "null" ...
07-29 11:33:55.186: WARN/System.err(2110):     "NaN" ...
07-29 11:33:55.194: WARN/System.err(2110):     "Infinity" ...
07-29 11:33:55.194: WARN/System.err(2110):     <BOOLEAN> ...
07-29 11:33:55.194: WARN/System.err(2110):     <SINGLE_QUOTE_LITERAL> ...
07-29 11:33:55.194: WARN/System.err(2110):     <DOUBLE_QUOTE_LITERAL> ...
07-29 11:33:55.202: WARN/System.err(2110):     ")]}\'\n" ...
07-29 11:33:55.210: WARN/System.err(2110):     "{" ...
07-29 11:33:55.210: WARN/System.err(2110):     "[" ...
07-29 11:33:55.217: WARN/System.err(2110):     "-" ...
07-29 11:33:55.217: WARN/System.err(2110):     
07-29 11:33:55.217: WARN/System.err(2110):     at   com.google.gson.JsonParserJavacc.generateParseException(JsonParserJavacc.java:705)
......:   at com.google.gson.JsonParserJavacc.jj_consume_token(JsonParserJavacc.java:587)
......:     at com.google.gson.JsonParserJavacc.parse(JsonParserJavacc.java:62)
......:    at com.google.gson.JsonParser.parse(JsonParser.java:54)
.....:     ... 1 more

Please suggest a solution ,I am using gson 1.4 version.The code i use is

            URLsource = new URL("http://.........);
      in = new BufferedReader(new UnicodeReader(URLsource.openStream(), "UTF-8"));
       jse = new JsonParser().parse(in);
    in.close(); 
            JsonArray info = jse.getAsJsonObject().getAsJsonArray("info");

the o/p of the url as in the browser is

    {"info":[{"restaurant_id":"","name":"Vie de France","campus_id":"","address":"600 Maryland Ave SW","city":"WASHINGTON","state":"DC","neighborhood":"","neighborhood_urlname":"","zipcode":"20024","average_entree":"6.29","chain_id":"994","phone":"(202) 554-7870","is_voicestar":"0","menu_type":"1","url":"","logo":"","details":"","latitude":"38.886877","longitude":"-77.019913","properties":"rmwa","reviews":"y","yelp_phone":"2025547870","reservation_id":"","reservation_url":"","price":"","cross_street":"","public_transit":"","restaurant_description":"","payment_methods":"","hours":"","next_open":"","restaurant_status":"","is_preorder":"","citysearch_hours":" ","minimum_order":"","minimum_order_2":"","delivery_rate":"","delivery_time":"","delivery_info":"","misc_info":"","delivery_charge":"","delivery_charge_2":"","is_pickup":"","is_delivery":"","minimum_tip_amount":"","minimum_tip_percent":"","restaurant_account_id":"","urlname":"\/dc\/washington\/239256-vie-de-france\/","facebook_fan_page":"","twitter_page":"","is_dz":"0","loyalty":"","delivery_hours":""}],"cuisines":[{"mastercuisineid":"143","cuisine":"American","urlname":"american"},{"mastercuisineid":"90","cuisine":"Bakery & Pastries","urlname":"bakery-pastries"},{"mastercuisineid":"57","cuisine":"French","urlname":"french"}]}

thanks

2 Answers 2

1

If your JSON is on a server and you access it through an HTTP request, you need to parse only the content, not the whole file with the headers.
Try doing an HTTP client, use getResponse.getEntity or getResponse.getContent, and then your parser on one of these.

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

Comments

1

You appear to be attempting to parse HTTP as JSON. HTTP has a status line and headers before any JSON entity.

2 Comments

thanks Commonsware ,is there any way to skip status line and headers?
@ganesh: As Sephy indicated, use HttpClient and BasicResponseHandler instead of URL.

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.