Parsin error causing blank screen - java
enter image description here
Hello, I am with a problem that my code does not worked, where the cause of error is(Problem parsing the earthquake JSON results) with this do with my app when executed stay with blank screen. My code be reffers to udacity course about network and the all code can be find on the link below. Please I am begginner in progamming i don't know more what do.
(The code what I used)
public class QueryUtil {
/**
* Sample JSON response for a USGS query
*/
private static final String SAMPLE_JSON_RESPONSE = "{\"type\":\"FeatureCollection\",\"metadata\":{\"generated\":1462295443000,\"url\":\"http://earthquake.usgs.gov/fdsnws/event/1/query?format=geojson&starttime=2016-01-01&endtime=2016-01-31&minmag=6&limit=10\",\"title\":\"USGS Earthquakes\",\"status\":200,\"api\":\"1.5.2\",\"limit\":10,\"offset\":1,\"count\":10},\"features\":[{\"type\":\"Feature\",\"properties\":{\"mag\":7.2,\"place\":\"88km N of Yelizovo, Russia\",\"time\":1454124312220,\"updated\":1460674294040,\"tz\":720,\"url\":\"http://earthquake.usgs.gov/earthquakes/eventpage/us20004vvx\",\"detail\":\"http://earthquake.usgs.gov/fdsnws/event/1/query?eventid=us20004vvx&format=geojson\",\"felt\":2,\"cdi\":3.4,\"mmi\":5.82,\"alert\":\"green\",\"status\":\"reviewed\",\"tsunami\":1,\"sig\":798,\"net\":\"us\",\"code\":\"20004vvx\",\"ids\":\",at00o1qxho,pt16030050,us20004vvx,gcmt20160130032510,\",\"sources\":\",at,pt,us,gcmt,\",\"types\":\",cap,dyfi,finite-fault,general-link,general-text,geoserve,impact-link,impact-text,losspager,moment-tensor,nearby-cities,origin,phase-data,shakemap,tectonic-summary,\",\"nst\":null,\"dmin\":0.958,\"rms\":1.19,\"gap\":17,\"magType\":\"mww\",\"type\":\"earthquake\",\"title\":\"M 7.2 - 88km N of Yelizovo, Russia\"},\"geometry\":{\"type\":\"Point\",\"coordinates\":[158.5463,53.9776,177]},\"id\":\"us20004vvx\"},\n" +
"{\"type\":\"Feature\",\"properties\":{\"mag\":6.1,\"place\":\"94km SSE of Taron, Papua New Guinea\",\"time\":1453777820750,\"updated\":1460156775040,\"tz\":600,\"url\":\"http://earthquake.usgs.gov/earthquakes/eventpage/us20004uks\",\"detail\":\"http://earthquake.usgs.gov/fdsnws/event/1/query?eventid=us20004uks&format=geojson\",\"felt\":null,\"cdi\":null,\"mmi\":4.1,\"alert\":\"green\",\"status\":\"reviewed\",\"tsunami\":1,\"sig\":572,\"net\":\"us\",\"code\":\"20004uks\",\"ids\":\",us20004uks,gcmt20160126031023,\",\"sources\":\",us,gcmt,\",\"types\":\",cap,geoserve,losspager,moment-tensor,nearby-cities,origin,phase-data,shakemap,tectonic-summary,\",\"nst\":null,\"dmin\":1.537,\"rms\":0.74,\"gap\":25,\"magType\":\"mww\",\"type\":\"earthquake\",\"title\":\"M 6.1 - 94km SSE of Taron, Papua New Guinea\"},\"geometry\":{\"type\":\"Point\",\"coordinates\":[153.2454,-5.2952,26]},\"id\":\"us20004uks\"},\n" +
"{\"type\":\"Feature\",\"properties\":{\"mag\":6.3,\"place\":\"50km NNE of Al Hoceima, Morocco\",\"time\":1453695722730,\"updated\":1460156773040,\"tz\":0,\"url\":\"http://earthquake.usgs.gov/earthquakes/eventpage/us10004gy9\",\"detail\":\"http://earthquake.usgs.gov/fdsnws/event/1/query?eventid=us10004gy9&format=geojson\",\"felt\":117,\"cdi\":7.2,\"mmi\":5.28,\"alert\":\"green\",\"status\":\"reviewed\",\"tsunami\":0,\"sig\":695,\"net\":\"us\",\"code\":\"10004gy9\",\"ids\":\",us10004gy9,gcmt20160125042203,\",\"sources\":\",us,gcmt,\",\"types\":\",cap,dyfi,geoserve,impact-text,losspager,moment-tensor,nearby-cities,origin,phase-data,shakemap,tectonic-summary,\",\"nst\":null,\"dmin\":2.201,\"rms\":0.92,\"gap\":20,\"magType\":\"mww\",\"type\":\"earthquake\",\"title\":\"M 6.3 - 50km NNE of Al Hoceima, Morocco\"},\"geometry\":{\"type\":\"Point\",\"coordinates\":[-3.6818,35.6493,12]},\"id\":\"us10004gy9\"},\n" +
"{\"type\":\"Feature\",\"properties\":{\"mag\":7.1,\"place\":\"86km E of Old Iliamna, Alaska\",\"time\":1453631430230,\"updated\":1460156770040,\"tz\":-540,\"url\":\"http://earthquake.usgs.gov/earthquakes/eventpage/us10004gqp\",\"detail\":\"http://earthquake.usgs.gov/fdsnws/event/1/query?eventid=us10004gqp&format=geojson\",\"felt\":1816,\"cdi\":7.2,\"mmi\":6.6,\"alert\":\"green\",\"status\":\"reviewed\",\"tsunami\":1,\"sig\":1496,\"net\":\"us\",\"code\":\"10004gqp\",\"ids\":\",at00o1gd6r,us10004gqp,ak12496371,gcmt20160124103030,\",\"sources\":\",at,us,ak,gcmt,\",\"types\":\",cap,dyfi,finite-fault,general-link,general-text,geoserve,impact-link,impact-text,losspager,moment-tensor,nearby-cities,origin,phase-data,shakemap,tectonic-summary,trump-origin,\",\"nst\":null,\"dmin\":0.72,\"rms\":2.11,\"gap\":19,\"magType\":\"mww\",\"type\":\"earthquake\",\"title\":\"M 7.1 - 86km E of Old Iliamna, Alaska\"},\"geometry\":{\"type\":\"Point\",\"coordinates\":[-153.4051,59.6363,129]},\"id\":\"us10004gqp\"},\n" +
"{\"type\":\"Feature\",\"properties\":{\"mag\":6.6,\"place\":\"215km SW of Tomatlan, Mexico\",\"time\":1453399617650,\"updated\":1459963829040,\"tz\":-420,\"url\":\"http://earthquake.usgs.gov/earthquakes/eventpage/us10004g4l\",\"detail\":\"http://earthquake.usgs.gov/fdsnws/event/1/query?eventid=us10004g4l&format=geojson\",\"felt\":11,\"cdi\":2.7,\"mmi\":3.92,\"alert\":\"green\",\"status\":\"reviewed\",\"tsunami\":1,\"sig\":673,\"net\":\"us\",\"code\":\"10004g4l\",\"ids\":\",at00o1bebo,pt16021050,us10004g4l,gcmt20160121180659,\",\"sources\":\",at,pt,us,gcmt,\",\"types\":\",cap,dyfi,geoserve,impact-link,impact-text,losspager,moment-tensor,nearby-cities,origin,phase-data,shakemap,tectonic-summary,\",\"nst\":null,\"dmin\":2.413,\"rms\":0.98,\"gap\":74,\"magType\":\"mww\",\"type\":\"earthquake\",\"title\":\"M 6.6 - 215km SW of Tomatlan, Mexico\"},\"geometry\":{\"type\":\"Point\",\"coordinates\":[-106.9337,18.8239,10]},\"id\":\"us10004g4l\"},\n" +
"{\"type\":\"Feature\",\"properties\":{\"mag\":6.7,\"place\":\"52km SE of Shizunai, Japan\",\"time\":1452741933640,\"updated\":1459304879040,\"tz\":540,\"url\":\"http://earthquake.usgs.gov/earthquakes/eventpage/us10004ebx\",\"detail\":\"http://earthquake.usgs.gov/fdsnws/event/1/query?eventid=us10004ebx&format=geojson\",\"felt\":51,\"cdi\":5.8,\"mmi\":6.45,\"alert\":\"green\",\"status\":\"reviewed\",\"tsunami\":1,\"sig\":720,\"net\":\"us\",\"code\":\"10004ebx\",\"ids\":\",us10004ebx,pt16014050,at00o0xauk,gcmt20160114032534,\",\"sources\":\",us,pt,at,gcmt,\",\"types\":\",associate,cap,dyfi,geoserve,impact-link,impact-text,losspager,moment-tensor,nearby-cities,origin,phase-data,shakemap,\",\"nst\":null,\"dmin\":0.281,\"rms\":0.98,\"gap\":22,\"magType\":\"mww\",\"type\":\"earthquake\",\"title\":\"M 6.7 - 52km SE of Shizunai, Japan\"},\"geometry\":{\"type\":\"Point\",\"coordinates\":[142.781,41.9723,46]},\"id\":\"us10004ebx\"},\n" +
"{\"type\":\"Feature\",\"properties\":{\"mag\":6.1,\"place\":\"12km WNW of Charagua, Bolivia\",\"time\":1452741928270,\"updated\":1459304879040,\"tz\":-240,\"url\":\"http://earthquake.usgs.gov/earthquakes/eventpage/us10004ebw\",\"detail\":\"http://earthquake.usgs.gov/fdsnws/event/1/query?eventid=us10004ebw&format=geojson\",\"felt\":3,\"cdi\":2.2,\"mmi\":2.21,\"alert\":\"green\",\"status\":\"reviewed\",\"tsunami\":0,\"sig\":573,\"net\":\"us\",\"code\":\"10004ebw\",\"ids\":\",us10004ebw,gcmt20160114032528,\",\"sources\":\",us,gcmt,\",\"types\":\",cap,dyfi,geoserve,impact-text,losspager,moment-tensor,nearby-cities,origin,phase-data,shakemap,tectonic-summary,\",\"nst\":null,\"dmin\":5.492,\"rms\":1.04,\"gap\":16,\"magType\":\"mww\",\"type\":\"earthquake\",\"title\":\"M 6.1 - 12km WNW of Charagua, Bolivia\"},\"geometry\":{\"type\":\"Point\",\"coordinates\":[-63.3288,-19.7597,582.56]},\"id\":\"us10004ebw\"},\n" +
"{\"type\":\"Feature\",\"properties\":{\"mag\":6.2,\"place\":\"74km NW of Rumoi, Japan\",\"time\":1452532083920,\"updated\":1459304875040,\"tz\":540,\"url\":\"http://earthquake.usgs.gov/earthquakes/eventpage/us10004djn\",\"detail\":\"http://earthquake.usgs.gov/fdsnws/event/1/query?eventid=us10004djn&format=geojson\",\"felt\":8,\"cdi\":3.4,\"mmi\":3.74,\"alert\":\"green\",\"status\":\"reviewed\",\"tsunami\":0,\"sig\":594,\"net\":\"us\",\"code\":\"10004djn\",\"ids\":\",us10004djn,gcmt20160111170803,\",\"sources\":\",us,gcmt,\",\"types\":\",cap,dyfi,geoserve,impact-text,losspager,moment-tensor,nearby-cities,origin,phase-data,shakemap,tectonic-summary,\",\"nst\":null,\"dmin\":1.139,\"rms\":0.96,\"gap\":33,\"magType\":\"mww\",\"type\":\"earthquake\",\"title\":\"M 6.2 - 74km NW of Rumoi, Japan\"},\"geometry\":{\"type\":\"Point\",\"coordinates\":[141.0867,44.4761,238.81]},\"id\":\"us10004djn\"},\n" +
"{\"type\":\"Feature\",\"properties\":{\"mag\":6.5,\"place\":\"227km SE of Sarangani, Philippines\",\"time\":1452530285900,\"updated\":1459304874040,\"tz\":480,\"url\":\"http://earthquake.usgs.gov/earthquakes/eventpage/us10004dj5\",\"detail\":\"http://earthquake.usgs.gov/fdsnws/event/1/query?eventid=us10004dj5&format=geojson\",\"felt\":1,\"cdi\":2.7,\"mmi\":7.5,\"alert\":\"green\",\"status\":\"reviewed\",\"tsunami\":1,\"sig\":650,\"net\":\"us\",\"code\":\"10004dj5\",\"ids\":\",at00o0srjp,pt16011050,us10004dj5,gcmt20160111163807,\",\"sources\":\",at,pt,us,gcmt,\",\"types\":\",cap,dyfi,geoserve,impact-link,impact-text,losspager,moment-tensor,nearby-cities,origin,phase-data,shakemap,tectonic-summary,\",\"nst\":null,\"dmin\":3.144,\"rms\":0.72,\"gap\":22,\"magType\":\"mww\",\"type\":\"earthquake\",\"title\":\"M 6.5 - 227km SE of Sarangani, Philippines\"},\"geometry\":{\"type\":\"Point\",\"coordinates\":[126.8621,3.8965,13]},\"id\":\"us10004dj5\"},\n" +
"{\"type\":\"Feature\",\"properties\":{\"mag\":6,\"place\":\"Pacific-Antarctic Ridge\",\"time\":1451986454620,\"updated\":1459202978040,\"tz\":-540,\"url\":\"http://earthquake.usgs.gov/earthquakes/eventpage/us10004bgk\",\"detail\":\"http://earthquake.usgs.gov/fdsnws/event/1/query?eventid=us10004bgk&format=geojson\",\"felt\":0,\"cdi\":1,\"mmi\":0,\"alert\":\"green\",\"status\":\"reviewed\",\"tsunami\":0,\"sig\":554,\"net\":\"us\",\"code\":\"10004bgk\",\"ids\":\",us10004bgk,gcmt20160105093415,\",\"sources\":\",us,gcmt,\",\"types\":\",cap,dyfi,geoserve,losspager,moment-tensor,nearby-cities,origin,phase-data,shakemap,\",\"nst\":null,\"dmin\":30.75,\"rms\":0.67,\"gap\":71,\"magType\":\"mww\",\"type\":\"earthquake\",\"title\":\"M 6.0 - Pacific-Antarctic Ridge\"},\"geometry\":{\"type\":\"Point\",\"coordinates\":[-136.2603,-54.2906,10]},\"id\":\"us10004bgk\"}],\"bbox\":[-153.4051,-54.2906,10,158.5463,59.6363,582.56]}";
/**
* Create a private constructor because no one should ever create a {#link QueryUtil} object.
* This class is only meant to hold static variables and methods, which can be accessed
* directly from the class name QueryUtils (and an object instance of QueryUtils is not needed).
*/
private QueryUtil() {
}
/**
* Return a list of {#link InfosQuake} objects that has been built up from
* parsing a JSON response.
*/
public static ArrayList<InfosQuake> extractEarthquakes() {
// Create an empty ArrayList that we can start adding earthquakes to
ArrayList<InfosQuake> earthquakes = new ArrayList<>();
// Try to parse the SAMPLE_JSON_RESPONSE. If there's a problem with the way the JSON
// is formatted, a JSONException exception object will be thrown.
// Catch the exception so the app doesn't crash, and print the error message to the logs.
try {
// TODO: Parse the response given by the SAMPLE_JSON_RESPONSE string and
// build up a list of Earthquake objects with the corresponding data.
JSONObject root = new JSONObject(SAMPLE_JSON_RESPONSE);
// Getting json array code.
JSONArray earthQuakeArrays = root.getJSONArray("Feature");
// Looping through all infosNodes that we want.
int i;
for (i = 0; i < earthQuakeArrays.length(); i += 1);
JSONObject currentEarthQuake = earthQuakeArrays.getJSONObject(i);
JSONObject properties = currentEarthQuake.getJSONObject("properties");
String magnitude = properties.getString("mag");
String location = properties.getString("place");
String time = properties.getString("time");
InfosQuake earthquake = new InfosQuake(magnitude, location, time);
earthquakes.add(earthquake);
} catch (JSONException e) {
// If an error is thrown when executing any of the above statements in the "try" block,
// catch the exception here, so the app doesn't crash. Print a log message
// with the message from the exception.
Log.e("QueryUtils", "Problem parsing the earthquake JSON results", e);
}
// Return the list of earthquakes
return earthquakes;
}
}
(The error that is causing)
Problem parsing the earthquake JSON results
org.json.JSONException: No value for Feature
at org.json.JSONObject.get(JSONObject.java:389)
at org.json.JSONObject.getJSONArray(JSONObject.java:584)
at com.example.android.earthquakeapp.QueryUtil.extractEarthquakes(QueryUtil.java:60)
at com.example.android.earthquakeapp.EarthquakeActivity.onCreate(EarthquakeActivity.java:20)
at android.app.Activity.performCreate(Activity.java:6575)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1134)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3143)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3300)
at android.app.ActivityThread.access$1000(ActivityThread.java:211)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1705)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:145)
at android.app.ActivityThread.main(ActivityThread.java:6946)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1404)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1199)
https://classroom.udacity.com/courses/ud843/lessons/1335cf7d-bb4f-48c6-8503-f14b127d2abc/concepts/0eca5789-48d7-4662-8e87-d71490519bd4
You can parse like this below
yours did mistake in getting the array using the key features and your didnt loop correctly.
public static ArrayList<InfosQuake> extractEarthquakes() {
ArrayList<InfosQuake> earthquakes = new ArrayList<>();
try {
// TODO: Parse the response given by the SAMPLE_JSON_RESPONSE string and
// build up a list of Earthquake objects with the corresponding data.
JSONObject root = new JSONObject(SAMPLE_JSON_RESPONSE);
// Getting json array code.
JSONArray earthQuakeArrays = root.optJSONArray("features");
if (earthQuakeArrays == null) {
return earthquakes;
}
// Looping through all infosNodes that we want.
for (int i = 0; i < earthQuakeArrays.length(); i++) {
JSONObject currentEarthQuake = earthQuakeArrays.optJSONObject(i);
if (currentEarthQuake == null) {
continue;
}
JSONObject properties = currentEarthQuake.optJSONObject("properties");
if (properties == null) { // In your case we dont have any other things to get so continue
continue;
}
String magnitude = properties.optString("mag");
String location = properties.optString("place");
String time = properties.optString("time");
InfosQuake earthquake = new InfosQuake(magnitude, location, time);
earthquakes.add(earthquake);
}
} catch (JSONException e) {
// If an error is thrown when executing any of the above statements in the "try" block,
// catch the exception here, so the app doesn't crash. Print a log message
// with the message from the exception.
Log.e("QueryUtils", "Problem parsing the earthquake JSON results", e);
}
// Return the list of earthquakes
return earthquakes;
}
Related
java.io.NotSerializableException: Custom JSQL Parser Implementation
I have a Flink application where I run a custom SQL parser and parse the SQL provided by user. This generates a Predicate tree which gets evaluated once the streaming application starts. I use JSQL parser to parse the SQL. When running the application, I get a weird Serialization error which I didn't get before. I haven't changed anything in the code, yet I get this error suddenly using Flink API. Below is the code snippet where it is failing and I get a java.io.NotSerializableException: com.eventwatch.query.OperationType$$Lambda$1272/411594792: private Predicate<RowMapPair> checkEquals(String type, String column, Object value) { log.debug("string: " + value); StringValue stringValue = new StringValue((""+value).trim()); log.debug("stringValue: " + stringValue.toString()); return (rowMapPair) -> { // PLACE OF RUNTIME EXCEPTION MapState<String, Pair> mapState = rowMapPair.getMapState(); Row row = rowMapPair.getRow(); String _id = String.valueOf(row.getFieldAs("_id")); String key = String.join("__", _id, column); String curVal = null; try { Pair val = mapState.get(key); log.debug("** val: {}", mapState.get(key)); if (Objects.nonNull(val)) { curVal = String.valueOf(val.getValue()); } else { return false; } } catch (Exception e) { log.error("Error: {}", e.getMessage(), e); throw new RuntimeException(e); } return curVal.equalsIgnoreCase(stringValue.getValue()); }; } Below is the stack trace: Caused by: java.io.NotSerializableException: com.eventwatch.query.OperationType$$Lambda$1272/411594792 at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1184) at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:348) at org.apache.flink.util.InstantiationUtil.serializeObject(InstantiationUtil.java:632) at org.apache.flink.api.java.ClosureCleaner.clean(ClosureCleaner.java:143)
error when get value from marketdataincremental refresh
I got an error in my quickfixj Application. First, I got an error like this: Out of order repeating group members After that, I added this text into my initiator.config: ValidateUserDefinedFields=N ValidateIncomingMessage=N But now I got another error in my application: quickfix.FieldNotFound: Field was not found in message, field=55 at quickfix.FieldMap.getField(FieldMap.java:223) at quickfix.FieldMap.getString(FieldMap.java:237) at com.dxtr.fastmatch.marketdatarequestapps.TestMarketdataRequest.fromApp(TestMarketdataRequest.java:38) at quickfix.Session.fromCallback(Session.java:1847) at quickfix.Session.verify(Session.java:1791) at quickfix.Session.verify(Session.java:1862) at quickfix.Session.next(Session.java:1047) at quickfix.Session.next(Session.java:1204) at quickfix.mina.SingleThreadedEventHandlingStrategy$SessionMessageEvent.processMessage(SingleThreadedEventHandlingStrategy.java:163) at quickfix.mina.SingleThreadedEventHandlingStrategy.block(SingleThreadedEventHandlingStrategy.java:113) at quickfix.mina.SingleThreadedEventHandlingStrategy.lambda$blockInThread$1(SingleThreadedEventHandlingStrategy.java:145) at quickfix.mina.SingleThreadedEventHandlingStrategy$ThreadAdapter$RunnableWrapper.run(SingleThreadedEventHandlingStrategy.java:267) at java.lang.Thread.run(Thread.java:748) My code for get value of symbols is : public void fromApp(quickfix.Message message, SessionID sessionID) throws FieldNotFound, IncorrectDataFormat, IncorrectTagValue, UnsupportedMessageType { try { String symbol = message.getString(Symbol.FIELD); System.out.println(" FromApp " + message); message.getString(TransactTime.FIELD); // String seqNo = message.getString(MsgSeqNum.FIELD); double bid = message.getDouble(MDEntryPx.FIELD); double ask = message.getDouble(MDEntryPx.FIELD); // System.out.println(seqNo + " " + message); } catch (FieldNotFound fieldNotFound) { fieldNotFound.printStackTrace(); } } I have also using this code public void onMessage (MarketDataIncrementalRefresh message, SessionID sessionID) throws FieldNotFound{ try { MDReqID mdreqid = new MDReqID(); SendingTime sendingtime = new SendingTime(); NoMDEntries nomdentries = new NoMDEntries(); quickfix.fix42.MarketDataIncrementalRefresh.NoMDEntries group = new quickfix.fix42.MarketDataIncrementalRefresh.NoMDEntries(); MDUpdateAction mdupdateaction = new MDUpdateAction(); DeleteReason deletereason = new DeleteReason(); MDEntryType mdentrytype = new MDEntryType(); MDEntryID mdentryid = new MDEntryID(); Symbol symbol = new Symbol(); MDEntryOriginator mdentryoriginator = new MDEntryOriginator(); MDEntryPx mdentrypx = new MDEntryPx(); Currency currency = new Currency(); MDEntrySize mdentrysize = new MDEntrySize(); ExpireDate expiredate = new ExpireDate(); ExpireTime expiretime = new ExpireTime(); NumberOfOrders numberoforders = new NumberOfOrders(); MDEntryPositionNo mdentrypositionno = new MDEntryPositionNo(); message.getField(nomdentries); message.getField(sendingtime); message.getGroup(1, group); int list = nomdentries.getValue(); for (int i = 0; i < list; i++) { message.getGroup(i + 1, group); group.get(mdupdateaction); if (mdupdateaction.getValue() == '2') System.out.println("Enter"); group.get(deletereason); group.get(mdentrytype); group.get(mdentryid); group.get(symbol); group.get(mdentryoriginator); if (mdupdateaction.getValue() == '0') group.get(mdentrypx); group.get(currency); if (mdupdateaction.getValue() == '0') group.get(mdentrysize); } System.out.printf("Got Symbol {0} Price {1}", symbol.getValue(), mdentrypx.getValue()); }catch (Exception ex) { System.out.println("error" + ex); } but i also get error like this quickfix.FieldNotFound: Field was not found in message, field=55 at quickfix.FieldMap.getField(FieldMap.java:223) at quickfix.FieldMap.getString(FieldMap.java:237) at com.dxtr.fastmatch.marketdatarequestapps.TestMarketdataRequest.fromApp(TestMarketdataRequest.java:39) at quickfix.Session.fromCallback(Session.java:1847) at quickfix.Session.verify(Session.java:1791) at quickfix.Session.verify(Session.java:1862) at quickfix.Session.next(Session.java:1047) at quickfix.Session.next(Session.java:1204) at quickfix.mina.SingleThreadedEventHandlingStrategy$SessionMessageEvent.processMessage(SingleThreadedEventHandlingStrategy.java:163) at quickfix.mina.SingleThreadedEventHandlingStrategy.block(SingleThreadedEventHandlingStrategy.java:113) at quickfix.mina.SingleThreadedEventHandlingStrategy.lambda$blockInpacket_write_wait: Connection to 3.13.235.241 port 22: Broken pipe and here the value i check in my message.log 8=FIX.4.2^A9=0217^A35=X^A34=7291^A49=Fastmatch1^A52=20200401-10:47:59.833^A56=MDValueTrade2UAT1^A262=VT_020^A268=02^A279=2^A55=GBP/CHF^A269=0^A278=1140851192^A270=1.19503^A271=02000000^A279=0^A55=GBP/CHF^A269=0^A278=1140851194^A270=1.19502^A271=06000000^A10=114^A my broker have send to me the price and etc My question is: how to fix my problem from this code ?
First, I got an error like this: Out of order repeating group members Your data dictionary doesn't match your counterparty's. Fix that and this will go away. After that, I added this text into my initiator.config: ValidateUserDefinedFields=N ValidateIncomingMessage=N This did not fix anything -- it HIDES your actual problem and has you looking at a new fake problem. What you need to do: Your configuration has this, right? UseDataDictionary=Y DataDictionary=path/to/FIXnn.xml # or if FIX5: AppDataDictionary=path/to/FIX5n.xml TransportDataDictionary=path/to/FIXT.xml Find your counterparty's documentation, and make sure your xml file's messages and fields match what they say they're going to send you. Make sure all repeating groups have the same fields in the same order. Here is some documentation about how the Data Dictionary xml file is structured. It's pretty easy.
Java GSON check data
I'm having trouble with gson: For example I have this output from website: [["connected"], ["user1":"Hello"], ["user2":"Hey"], ["disconnected"]] But I want parse this JSON and output something like this: connected user1 says: Hello user2 says: Hey disconnected I quicly wrote this code: public static void PrintEvents(String id){ String response = Post.getResponse(Server()+"events?id="+id,""); // response is [["connected"],["user1":"Hello"],["user2":"Hey"],["disconnected"]] JsonElement parse = (new JsonParser()).parse(response); //found this in internet int bound = ????????????; // Should be 4 for (int i=1;i<=bound;i++){ String data = ???????????; if (data == "connected" || data == "disconnected") then { System.out.println(data); }else if(?????==2){// to check how many strings there is, if it's ["abc","def"] or ["abc"] String data2 = ??????????????; System.out.println(data+" says: "+data2); }else{ //something else } }; } What should I insert to these parts with question marks to make code work? I cannot find any way to make it work... Sorry for my bad English. EDIT: Changed response to [["connected"], ["user1","Hello"], ["user2","Hey"], ["disconnected"]]. Earlier response was not valid JSON.
The response that you have pasted is not a valid json. paste it in http://www.jsoneditoronline.org/ and see the error. Please find the below code snippet: public static void printEvents(String id) { String response = "[[\"connected\"] ,[\"user1:Hello\"],[\"user2:Hey\"],[\"disconnected\"]]"; JsonElement parse = (new JsonParser()).parse(response); //found this in internet int bound = ((JsonArray)parse).size(); // Should be 4 for (int i = 0; i < bound; i++) { String data = ((JsonArray)parse).get(0).getAsString(); if (data.equals("connected") || data.equals("disconnected")) { System.out.println(data); continue; } String[] splittedData = data.split(":"); if (splittedData.length == 2) {// to check how many strings there is, if it's ["abc","def"] or ["abc"] System.out.println(splittedData[0] + " says: " + splittedData[1]); } /* *else{ * your else logic goes here * } * */ } } Couple of suggestions: If you are new to json world, use jackson instead of Gson. the response is not a good design. Slightly correct json: { "firstKey": "connected", "userResponses": [ { "user1": "hey" }, { "user2": "hi" } ], "lastKey": "disconnected" } Also try to define pojos , instead of working inline with json.
You need to define a separate class like this: class MyClass{ String name; String value; } and then: List<MyClass> myclasses = new Gson().fromJson(response, new TypeToken<List<MyClass>>(){}.getType()); then for(MyClass myclass: myclasses){ ... }
org.json.JSONException: Missing value at character 2
I am trying to parse the below json response using following code but the code is working fine on normal eclipse program but failing on tomcat (java 1.8) 2016-12-20 00:18:17,146 DEBUG c.a.f.u.s.UpdateFileUploadData [Thread-35] sTempOutput::"[{\"Status\":\"Success\",\"MonthlyReportId\":\"R/I - 000913\"},{\"Status\":\"Success\",\"MonthlyReportId\":\"R/I - 001473\"},{\"Status\":\"Success\",\"MonthlyReportId\":\"R/I - 001474\"}]" 2016-12-20 00:18:17,146 DEBUG c.a.f.u.s.UpdateFileUploadData [Thread-35] transformed:[{\"Status\":\"Success\",\"MonthlyReportId\":\"R/I - 000913\"},{\"Status\":\"Success\",\"MonthlyReportId\":\"R/I - 001473\"},{\"Status\":\"Success\",\"MonthlyReportId\":\"R/I - 001474\"}] jsonObject = jsonObject.replaceAll("\\\"", "\""); if(jsonObject.startsWith("\"")){ jsonObject = jsonObject.substring(1, jsonObject.length()-1); } // replacing the " from start and end. if (logger.isDebugEnabled()) { logger.debug("transformed:{}", jsonObject); } org.json.JSONArray array = new org.json.JSONArray(jsonObject); if (logger.isDebugEnabled()) { logger.debug("array:::{}", array); } DBAdapter dbAdapter = new DBAdapter(); for (int start = 0 ;start<array.length(); start++){ JSONObject object = array.getJSONObject(start); String status = (String)object.get("Status"); String monthlyReportId = (String)object.get("MonthlyReportId"); if (logger.isDebugEnabled()) { logger.debug("status:::{}", status); logger.debug("monthlyReportId:::{}", monthlyReportId); } } This is killing me. I have also tried it - jsonObject = jsonObject.replaceAll("(\\r|\\n|\\r\\n)+", ""); jsonObject = jsonObject.replaceAll("\\\"", "\""); rest remains the same.
getJSONObject and subsequent getString returns null
This is a very straightforward question, but this error is very mysterious to me as I have not been able to find a solution or anyone else who has had this problem. I've also used a very similar technique in another activity and it worked just fine. I am making an android application which makes a POST request to a server. The response is a JSONObject that must be parsed into a number and another JSONObject which must also be parsed, and its values assigned to an array of CurrentGame objects. The first call to getJSONObject works fine, but calling getString on that JSONObject returns the following error: java.lang.NullPointerException: Attempt to write to field 'java.lang.String com.xxxxx.xxxxx.CurrentGame.oppEmail' on a null object reference Here is my java code: private void handleResponse(JSONObject response){ int numGroups = 0; try{ numGroups = response.getInt("Number"); }catch(JSONException e){ e.printStackTrace(); } Log.i("Number of Groups", String.valueOf(numGroups)); CurrentGame[] currentGames = new CurrentGame[numGroups]; JSONObject current; int yourTurn = 0; for(int i = 0; i < numGroups; i++){ try{ current = response.getJSONObject(String.valueOf(i)); Log.i("Current JSONObject: ", String.valueOf(current)); if(current.has("OppEmail")){ currentGames[i].oppEmail = current.getString("OppEmail"); } if(current.has("OppName")) { currentGames[i].oppName = current.getString("OppName"); } if(current.has("Group")) { currentGames[i].group = current.getString("Group"); } if(current.has("YourTurn")) { yourTurn = current.getInt("YourTurn"); } if(yourTurn == 0){ currentGames[i].yourTurn = true; } else{ currentGames[i].yourTurn = false; } } catch (JSONException e){ e.printStackTrace(); } } } Shouldn't the JSONObject.has() check at least be preventing this error? I know the first getInt() and getJSONObject are working. Heres the Log: 06-21 21:58:56.644 20116-20116/com.xxxxx.xxxxx D/Response:﹕ {"Number":2,"0":{"Group":"Test Group 1","OppEmail":"xxxxx#xxxxx.edu","OppName":"MikeyP","YourTurn":0},"1":{"Group":"Test Group 2","OppEmail":"xxxxx#xxxxx.edu","OppName":"MikeyP","YourTurn":1}} 06-21 21:58:56.644 20116-20116/com.xxxxxx.xxxxxt I/Number of Groups﹕ 2 06-21 21:58:56.644 20116-20116/com.xxxxx.xxxxx I/Current JSONObject﹕ {"Group":"Test Group 1","OppEmail":"xxxxxx#xxxxx.edu","OppName":"MikeyP","YourTurn":0} Here's the server code: $games['Number'] = $numgames; if($numgames > 0){ $i = 0; while($row = mysqli_fetch_array($getgames)){ $currGame['Group'] = $row['GroupName']; // Get the opponent's email and username if($row['Player1'] != $email){ $opponent = $row['Player1']; $currGame['OppEmail'] = $opponent; $sql = "SELECT Username FROM users WHERE Email = '".$opponent."'"; $username = mysqli_query($conn, $sql); $row2 = mysqli_fetch_assoc($username); $currGame['OppName'] = $row2['Username']; } else if($row['Player2'] != $email){ $opponent = $row['Player2']; $currGame['OppEmail'] = $opponent; $sql = "SELECT Username FROM users WHERE Email = '".$opponent."'"; $username = mysqli_query($conn, $sql); $row2 = mysqli_fetch_assoc($username); $currGame['OppName'] = $row2['Username']; } // Determine if it is this player's turn if($row['CurrentPlayer'] != $email){ $currGame['YourTurn'] = 0; } else{ $currGame['YourTurn'] = 1; } $games[$i] = $currGame; $i++; } } //Echo array of groups header('Content-Type: application/json'); $response = json_encode($games); echo $response; Thank you in advance for any ideas as to what I'm doing wrong here. I know similar questions have been asked about getString() returning null, but having read them all I'm still very stumped.
Problem is caused by : currentGames[i].oppEmail = current.getString("OppEmail"); line. Because currentGames Array is initialized with size 2 but not added any item of type CurrentGame. Instead of using currentGames[i].oppEmail create a object of CurrentGame class add all values then add it in currentGames Array like: CurrentGame objCurrentGame=new CurrentGame(); if(current.has("OppEmail")){ objCurrentGame.oppEmail = current.getString("OppEmail"); } ... same for other fields ... //Add objCurrentGame to Array currentGames[i]=objCurrentGame;
Parsing json this way is not robust and error prone, it is recommended to use such libraries as Gson Jackson Retrofit as these open source libraries offer stable implementation for such purposes and there is no need to reinvent the wheel yourself. example: YourPojoClass obj = new Gson().fromJson("{SomeJsonString}", YourPojoClass.class); In this way, you get the strongly typed pojo instance.You don't even need write the POJO class yourself, and there are many online service that can generate the POJO class out of json strings: http://www.jsonschema2pojo.org/ http://pojo.sodhanalibrary.com/