java - Does HttpClient adds any prefix to a JSON response? -
i using apache httpclient 4.5.2 in following code fetch json content.
surprisingly "?" gets prefixed in body leading malformed json.
code below:
public class urldatareader { public static void main(string[] args) throws java.lang.exception { urldatareader obj = new urldatareader(); string eventsurl = "http://api.511.org/traffic/events/?api_key=<my-personal-api-key>&format=json"; string jsontext = null; try { jsontext = obj.readurlcontent(eventsurl, true); } catch (java.lang.exception e) { system.out.print("exception in reading data url"); e.printstacktrace(); } if (jsontext != null) { system.out.println("read json content url :" + jsontext + "\n\n\n"); gson gson = new gsonbuilder().create(); eventqueryresponse response = gson.fromjson(jsontext, eventqueryresponse.class); system.out.println("json read java object:" + response.tostring()); } } public string readurlcontent(string urltoread, boolean useproxy) throws java.lang.exception { string targethost = new url(urltoread).gethost(); string inputline = null; stringbuffer result = null; string responsejsonbody = null; if (useproxy) { credentialsprovider credsprovider = new basiccredentialsprovider(); credsprovider.setcredentials( new authscope(proxyhost, proxyport), new usernamepasswordcredentials(proxyusername, proxypassword)); closeablehttpclient httpclient = httpclients.custom().setdefaultcredentialsprovider(credsprovider).build(); httphost target = new httphost(targethost, 80); httphost proxy = new httphost(proxyhost, proxyport); requestconfig config = requestconfig.custom().setproxy(proxy).build(); system.out.println("url path:" + new url(urltoread).getfile()); httpget httpget = new httpget(new url(urltoread).getfile()); httpget.setconfig(config); closeablehttpresponse response = httpclient.execute(target, httpget); responsejsonbody = entityutils.tostring(response.getentity(), "utf-8"); response.close(); httpclient.close(); } else { system.out.println("not implemented yet!!"); } return responsejsonbody; } private static string proxyusername = "username"; private static string proxypassword = "password"; private static string proxyhost = "proxy.host.com"; private static int proxyport = 80; }
the output
url path:/traffic/events/?api_key=<my-personal-ap-key>&format=json read json content url :?{"events":[{"url":"/traffic/events/511.org/23765","jurisdiction_url":"http://api.511.org/jurisdictions/511.org/","id":"511.org/23765","status":"active","headline":"caltrans : long-term construction on ca-128 eastbound , westbound between lower chiles valley rd (napa) , berryessa knoxville rd (napa). alternate lanes closed. expect delays.","event_type":"construction","severity":"unknown","created":"2016-08-05t20:20z","updated":"2016-08-07t17:28z","geography":{"type":"point","crs":{"type":"name","properties":{"name":"urn:ogc:def:crs:epsg::4326"}},"coordinates":[-122.296272,38.503876]},"roads":[{"name":"ca-128 e","from":"lower chiles valley rd","to":"berryessa knoxville rd","state":"open"}],"schedule":{"intervals":["2016-08-05t20:20z/2016-10-01t06:55z"]}},{"url":"/traffic/events/511.org/24826","jurisdiction_url":"http://api.511.org/jurisdictions/511.org/","id":"511.org/24826","status":"active","headline":"chp : obstruction on i-580 eastbound west of marina bay pky (richmond). center lanes blocked. expect delays.","event_type":"incident","severity":"unknown","created":"2016-08-08t12:00z","updated":"2016-08-08t12:01z","geography":{"type":"point","crs":{"type":"name","properties":{"name":"urn:ogc:def:crs:epsg::4326"}},"coordinates":[-122.353792,37.921656]},"roads":[{"name":"i-580 e","from":"marina bay pky","state":"open"}],"schedule":{"intervals":["2016-08-08t12:00z/"]}}],"pagination":{"offset":0},"meta":{"url":"/traffic/events/?api_key=65ce84c9-e5c4-43bf-ac16-a672e0b9266f&format=json","up_url":"/","version":"v1"}}
not sure "?" coming @ beginning of json response.
any ideas?
Comments
Post a Comment