java - Not a JSON Object Exception -
i'm trying json values distance24 json output via google gson. can't figure out , exception comes (i'm using google appengine java).
here's class send , request , response.
package de.tum.in.eist.distance; import java.io.ioexception; import javax.inject.inject; import java.net.url; import com.google.appengine.api.urlfetch.httpresponse; import com.google.appengine.api.urlfetch.urlfetchservice; import com.google.gson.jsonobject; import de.tum.in.eist.jsonhelper; import de.tum.in.eist.urlfetchservicehelper; public class distance24client { private final urlfetchservice service; @inject public distance24client(urlfetchservice service) { this.service = service; } public distance24 getdistanceapi(string source, string destination) throws ioexception { url url = new url("http://www.distance24.org/route.json?stops=" + source + "|" + destination); httpresponse response = service.fetch(url); string jsonstring = urlfetchservicehelper.tostring(response); try { jsonobject json = jsonhelper.parse(jsonstring); return todistance24(json); } catch (exception e) { throw new ioexception("error ocurred in getdistanceapi(): " + e.getmessage()); } } private distance24 todistance24(jsonobject response) { if (!(response.get("stops").getasjsonobject().getasjsonarray().size() != 0)) { throw new illegalargumentexception("no status set distance24 api"); } else { jsonobject distances = response.get("distances").getasjsonobject(); return new distance24(distances); } } }
and here's distance24 object:
package de.tum.in.eist.distance; import com.google.gson.jsonarray; import com.google.gson.jsonobject; public class distance24 { private int[] distances; private int totaldistance; private double sourcelat; private double sourcelon; private double destlat; private double destlong; public distance24(jsonobject distances) { this.setdistances(getintarray(distances)); this.settotaldistance(getsum(this.distances)); this.setsourcelat(distances.get("stops").getasjsonobject().getasjsonarray().get(0).getasjsonobject().get("latitude").getasdouble()); this.setsourcelon(distances.get("stops").getasjsonobject().getasjsonarray().get(0).getasjsonobject().get("longitude").getasdouble()); this.setdestlat(distances.get("stops").getasjsonobject().getasjsonarray().get(1).getasjsonobject().get("latitude").getasdouble()); this.setdestlong(distances.get("stops").getasjsonobject().getasjsonarray().get(1).getasjsonobject().get("longitude").getasdouble()); } private int[] getintarray(jsonobject array) { jsonarray distances = array.getasjsonarray(); int[] result = new int[distances.size()]; for(int = 0; < distances.size(); i++) { result[i] = distances.get(i).getasint(); } return result; } private int getsum(int[] array) { int sum = 0; for(int element : array) { sum += element; } return sum; } private void setdistances(int[] distances) { this.distances = distances; } public int gettotaldistance() { return totaldistance; } public void settotaldistance(int totaldistance) { this.totaldistance = totaldistance; } public double getsourcelat() { return sourcelat; } public void setsourcelat(double sourcelat) { this.sourcelat = sourcelat; } public double getsourcelon() { return sourcelon; } public void setsourcelon(double sourcelon) { this.sourcelon = sourcelon; } public double getdestlat() { return destlat; } public void setdestlat(double destlat) { this.destlat = destlat; } public double getdestlong() { return destlong; } public void setdestlong(double destlong) { this.destlong = destlong; } }
as result, whole json object string output e.getmessage(). guess information retrieving works, though it's on wrong part of code.
plus in same try-catch-block of code (distance24client, method "todistance24") says, error ocurred in line 30, return statement of "todistance24" method.
(clickable)
running http://www.distance24.org/route.json?stops=detroit|dublin browser gives me
{"stops":[{"region":"michigan ... "distances":[5581]}
so distances array , not object. line:
jsonobject distances = response.get("distances").getasjsonobject();
is wrong. read distances jsonarray.
Comments
Post a Comment