I want to fetch data from a web service but it is showing this error.
D/exception: com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was BEGIN_ARRAY at line 1 column 3 path $[0]
There are two arrays in an array, I tried parsing it but unable to get it, It is showing data in stringResponse but don't know why parsing through GSON not working.
Model Class
MainClass
private void getStringResponse() {
String url = "http://www.mocky.io/v2/5bd7f4683100003508474b3d";
StringRequest request = new StringRequest(
Request.Method.GET,
url,
new com.android.volley.Response.Listener<String>() {
#Override
public void onResponse(String response) {
String stringResponse = response.toString();
Gson gson = new Gson();
try{
ModelOne[] data = gson.fromJson(stringResponse, ModelOne[].class);
List<ModelOne.First> first = new ArrayList<>();
first = data[0].getZero();
Log.d("data", first.get(0).getFullName());
} catch (Exception e){
Log.d("exception", e.toString());
}
}
}, new com.android.volley.Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
Log.d("error", error.toString());
}
});
RequestQueue requestQueue = Volley.newRequestQueue(this);
requestQueue.add(request);
}
I just need minor guidelines, that how to tackle data when you have arrays within array and how to parse it.
and It was showing that your question has max code that's why I uploaded dropbox link
Related
i am using spring batch with Jdbc and Postgres DB. all the job and step execution data gets saved in spring batch created tables in PostgreS DB.
i am using it to save some step context data, which gets saved in batch_step_execution_context table in the column SERIALIZED_CONTEXT.
the data i am saving has some MBCS characters.
but i see that when writing data to the table and reading from it its using ISO-8859-1 charset.
hence my mbcs characters though serialized by Xstream default serializer gets stored as garbage.
any way to workaround this, so i can retrieve and save data as MBCS.
please find the code snippet from JDBCExecutionContextDao.
private String serializeContext(ExecutionContext ctx) {
Map<String, Object> m = new HashMap<String, Object>();
for (Entry<String, Object> me : ctx.entrySet()) {
m.put(me.getKey(), me.getValue());
}
ByteArrayOutputStream out = new ByteArrayOutputStream();
String results = "";
try {
serializer.serialize(m, out);
results = new String(out.toByteArray(), "ISO-8859-1");
}
catch (IOException ioe) {
throw new IllegalArgumentException("Could not serialize the execution context", ioe);
}
return results;
}
private class ExecutionContextRowMapper implements RowMapper<ExecutionContext> {
#Override
public ExecutionContext mapRow(ResultSet rs, int i) throws SQLException {
ExecutionContext executionContext = new ExecutionContext();
String serializedContext = rs.getString("SERIALIZED_CONTEXT");
if (serializedContext == null) {
serializedContext = rs.getString("SHORT_CONTEXT");
}
Map<String, Object> map;
try {
ByteArrayInputStream in = new ByteArrayInputStream(serializedContext.getBytes("ISO-8859-1"));
map = serializer.deserialize(in);
}
catch (IOException ioe) {
throw new IllegalArgumentException("Unable to deserialize the execution context", ioe);
}
for (Map.Entry<String, Object> entry : map.entrySet()) {
executionContext.put(entry.getKey(), entry.getValue());
}
return executionContext;
}
}
i expect to store and retrieve mbcs data.
hence my mbcs characters though serialized by Xstream default serializer gets stored as garbage.
The XStreamExecutionContextStringSerializer is deprecated, we recommend using the Jackson2ExecutionContextStringSerializer instead.
That said, if you want to use Multi-Byte characters for the execution context's data, you need to increase the size of the columns in the meta-data schema. This is explained in the International and Multi-byte Characters section of the reference documentation.
I need to log the incomming REST request (xml).
So I create a
public class RequestWrapper extends HttpServletRequestWrapper {
private String _body;
public RequestWrapper(HttpServletRequest request) throws IOException {
super(request);
_body = "";
BufferedReader bufferedReader = request.getReader();
String line;
while ((line = bufferedReader.readLine()) != null){
_body += line;
}
}
Now I use a filter to log out the incomming request:
class XmlogFilterFilters {
def filters = {
all(controller:'*', action:'*') {
before = {
RequestWrapper wrappedRequest = new RequestWrapper(request)
log.info(wrappedRequest.reader.text)
}
}
}
This logs the incomming request as expected.
But now in my controller the request is empty and cannot be used to Build my Domain-Object:
class InquiryHandlerController {
def save(Inquiry inquiryInstance) {
... *** inquiryInstance is null here
}
}
I guess the problem is, that th request was already read in the RequestWrapper, and therefor cannot be read again in the magic requestToDomain conversion.
So how can I pass the new RequestWrapper Object instead of the original request to the controller?
Finally found a solution:
use grailsplugin: grails-httplogger in version 1.1 do the right wrapper thing, so logging and consuming is working now.
https://github.com/prumps/grails-httplogger
I need to add header to a STOMP message currently it is working as below but i am recreating the message , is it possible to just add native header without having to recreate the message for performance .
public class MyChannelInterceptor extends ChannelInterceptorAdapter {
#Override
public Message<?> preSend(Message<?> message, MessageChannel channel) {
StompHeaderAccessor accessor = StompHeaderAccessor.wrap(message);
StompCommand command = accessor.getCommand();
if(command != null) {
log.debug("Receiving msg {} from {}",command,accessor.getUser().getName());
if(command == StompCommand.SEND) {
log.debug("Adding expires header to msg {} from {}",command,accessor.getUser().getName());
String ttlString = accessor.getFirstNativeHeader("ttl");
long ttl = 30000;
try {
ttl = Long.parseLong(ttlString);
}
catch(Exception ex) {
log.error("TTL header received but not in correct format {}",ttlString);
}
accessor.addNativeHeader("expires", Long.toString(System.currentTimeMillis() + ttl));
return MessageBuilder.createMessage(message.getPayload(), accessor.getMessageHeaders());
}
}
return message;
}
}
This is what i was looking for
StompHeaderAccessor accessor = MessageHeaderAccessor.getAccessor(message, StompHeaderAccessor.class);
The above code will get the actual StompHeaderAccessor of the message so if you manipulate the native headers they are directly reflected on the message while
StompHeaderAccessor accessor = StompHeaderAccessor.wrap(message);
will get a clone of the headers and you have to create a new message with the new cloned headers
full fixed code below
#Override
public Message<?> preSend(Message<?> message, MessageChannel channel) {
StompHeaderAccessor accessor = MessageHeaderAccessor.getAccessor(message, StompHeaderAccessor.class);
// StompHeaderAccessor accessor = StompHeaderAccessor.wrap(message);
if(accessor != null) {
StompCommand command = accessor.getCommand();
if(command != null) {
log.debug("Receiving msg {} from {}",command,accessor.getUser().getName());
if(command == StompCommand.SEND) {
log.debug("Adding expires header to msg {} from {}",command,accessor.getUser().getName());
String ttlString = accessor.getFirstNativeHeader("ttl");
long ttl = 30000;
if(ttlString != null) {
try {
ttl = Long.parseLong(ttlString);
}
catch(Exception ex) {
log.error("TTL header received but not in correct format {}",ttlString);
}
}
accessor.addNativeHeader("expires", Long.toString(System.currentTimeMillis() + ttl));
// I don't need any more to create a new message
//return MessageBuilder.createMessage(message.getPayload(), accessor.getMessageHeaders());
}
}
}
return message;
}
Since addNativeHeader succeeds, that indicates the message is still mutable - see addNativeHeader().
In any case, since the NATIVE_HEADERS message header is a MultiValueMap-valued header, you can update the header contents in-place.
Hence, there is no need to create a new message.
You would have to create a new message if you add a new header to the message itself (rather than updating the mutable contents of an existing header).
EDIT
I just ran a test; as long as the message is still mutable, you can change it...
#Test
public void test() {
Map<String, Object> map = new HashMap<String, Object>();
MutableMessageHeaders headers = new MutableMessageHeaders(map);
Message<String> message = MessageBuilder.createMessage("foo", headers);
StompHeaderAccessor accessor = StompHeaderAccessor.wrap(message);
accessor.addNativeHeader("foo", "bar");
System.out.println(message.getHeaders().get(NativeMessageHeaderAccessor.NATIVE_HEADERS));
accessor.setImmutable();
try {
accessor.addNativeHeader("baz", "qux");
fail("expected IllegalStateException");
}
catch (IllegalStateException e) {
}
}
That said, are you experiencing a performance problem or is this just a perceived issue. Message creation is not expensive.
I am trying to read array of JSON posted to a topic that my pipeline is subscribed to and persist the same to BigQuery. The problem I face while doing so is that it persists only the first object, can someone please provide me insight on what I am doing wrong here.
/** A DoFn that converts a table row from JSON into a BigQuery table row. */
static class FormatAsTableRowFn extends DoFn<TableRow, TableRow> {
private static final long serialVersionUID = 0;
static TableSchema getSchema() {
return new TableSchema().setFields(new ArrayList<TableFieldSchema>() {
// Compose the list of TableFieldSchema from tableSchema.
{
add(new TableFieldSchema().setName("PillBoxID").setType("STRING").setMode("NULLABLE"));
add(new TableFieldSchema().setName("Period").setType("STRING").setMode("NULLABLE"));
add(new TableFieldSchema().setName("Time").setType("TIMESTAMP").setMode("NULLABLE"));
add(new TableFieldSchema().setName("IsTaken").setType("STRING").setMode("NULLABLE"));
}
});
}
#Override
public void processElement(ProcessContext c) {
TableRow jsonRow = c.element();
// Setup a date formatter to parse the date appropriately
SimpleDateFormat ft = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
try {
TableRow bQueryRow = new TableRow()
.set("PillBoxID", (String) jsonRow.get("PillBoxID"))
.set("Period", (String) jsonRow.get("Period"))
.set("Time",ft.format(ft.parse((String) jsonRow.get("Time"))))
.set("IsTaken", (String) jsonRow.get("IsTaken"));
LOG.error("Inside try" + bQueryRow.getF());
c.output(bQueryRow);
} catch (ParseException pe) {
LOG.error("ParseException");
LOG.error(pe.getMessage());
}
}
}
and my pipleline code is as shown below,
bigQueryPipeLine
.apply(PubsubIO.Read.topic(options.getPubsubTopic()).withCoder(TableRowJsonCoder.of()))
.apply(ParDo.of(new FormatAsTableRowFn()))
.apply(BigQueryIO.Write.to(tableSpec)
.withSchema(FormatAsTableRowFn.getSchema()));
it is possible to process multiple records if you format the input JSON to include an array of items.
Example input:
{
"items":
[
{"PillBoxID":"ID5", "Period":"Morning", "Time":"2016-03-14T11:11:11", "IsTaken":"true"},
{"PillBoxID":"ID6", "Period":"Afternoon", "Time":"2016-03-14T15:11:11", "IsTaken":"false"}
]
}
The rough example processElement() code adds the 2 items to c.output() for later storage in BigQuery.
#Override
public void processElement(ProcessContext c) throws DatastoreException, IOException{
TableRow jsonRowObj = c.element();
LOG.info("Original input:" + c.element().toPrettyString());
ArrayList<Map> jsonRows = (ArrayList<Map>)jsonRowObj.get("items");
Iterator<Map> iterator = jsonRows.iterator();
while(iterator.hasNext()) {
Map jsonRow = (Map)iterator.next();
// Setup a date formatter to parse the date appropriately
SimpleDateFormat ft = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
try {
LOG.info("child input JSON: "+jsonRow.toString());
TableRow bQueryRow = new TableRow()
.set("PillboxID", (String) jsonRow.get("PillBoxID"))
.set("Period", (String) jsonRow.get("Period"))
.set("Time",ft.format(ft.parse((String) jsonRow.get("Time"))))
.set("IsTaken", (boolean) Boolean.parseBoolean((String)jsonRow.get("IsTaken")));
c.output(bQueryRow);
} catch (ParseException pe) {
LOG.error("");
LOG.error("ParseException " +pe.getMessage());
}
}
}
I am busy with an e-commerce web application using visual studio 2005 and IIS 7
I got this error
System.TypeInitializationException was unhandled by user code
Message="The type initializer for 'ShopConfiguration' threw an exception."
Source="App_Code.r-ihwy-d"
TypeName="ShopConfiguration"
StackTrace:
at ShopConfiguration.get_DbProviderName()
at GenericDataAccess.CreateCommand() in c:\inetpub\wwwroot\Beadafrican\App_Code\GenericDataAccess.cs:line 63
at CatalogAccess.GetDepartments() in c:\inetpub\wwwroot\Beadafrican\App_Code\CatalogAccess.cs:line 28
at UserControls_DepartmentsList.Page_Load(Object sender, EventArgs e) in c:\inetpub\wwwroot\Beadafrican\UserControls\DepartmentsList.ascx.cs:line 22
at System.Web.Util.CalliHelper.EventArgFunctionCaller(IntPtr fp, Object o, Object t, EventArgs e)
at System.Web.Util.CalliEventHandlerDelegateProxy.Callback(Object sender, EventArgs e)
at System.Web.UI.Control.OnLoad(EventArgs e)
at System.Web.UI.Control.LoadRecursive()
at System.Web.UI.Control.LoadRecursive()
at System.Web.UI.Control.LoadRecursive()
at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)
If I look at the code it refers to I dont see what is wrong? Here is the code if anyone can please assist it would be great!
GenericDataAccess.cs:
public static class GenericDataAccess
{
//static constructor
static GenericDataAccess()
{
//
// TODO: Add constructor logic here
//
}
//execute a command and returns the result as a DataTable Object
public static DataTable ExecuteSelectCommand(DbCommand command)
{
//The DataTable to be returned
DataTable table;
//Execute the command making sure the connection gets closed in the end
try
{
//open the data connection
command.Connection.Open();
//Execute the command and save the results in a DataTable
DbDataReader reader = command.ExecuteReader();
table = new DataTable();
table.Load(reader);
//Close the reader
reader.Close();
}
catch (Exception ex)
{
Utilities.LogError(ex);
throw ex;
}
finally
{
//Close the connection
command.Connection.Close();
}
return table;
}
//creates and prepares a new DbCommand object on a new connection
public static DbCommand CreateCommand()
{
//Obtain the database provider name
string dataProviderName = ShopConfiguration.DbProviderName;
//Obtain the database connection string
string connectionString = ShopConfiguration.DbConnectionString;
//Create a new data provider factory
DbProviderFactory factory = DbProviderFactories.GetFactory(dataProviderName);
//Obtain a database specific connection object
DbConnection conn = factory.CreateConnection();
//Set the connection string
conn.ConnectionString = connectionString;
//Create a database specific command object
DbCommand comm = conn.CreateCommand();
//Set the command type to stored procedure
comm.CommandType = CommandType.StoredProcedure;
//Return the initialised command object
return comm;
}
CatalogAccess.cs
public static class CatalogAccess
{
static CatalogAccess()
{
//
// TODO: Add constructor logic here
//
}
//Retrieve the list of departments
public static DataTable GetDepartments()
{
//get configured DbCommand object
DbCommand comm = GenericDataAccess.CreateCommand();
//set the stored procedure name
comm.CommandText = "GetDepartments";
//execute the stored procedure and return the results
return GenericDataAccess.ExecuteSelectCommand(comm);
}
}
DepartementList.ascx.cs
public partial class UserControls_DepartmentsList : System.Web.UI.UserControl
{
// Load department details into the DataList
protected void Page_Load(object sender, EventArgs e)
{
// don't reload data during postbacks
{
// CatalogAccess.GetDepartments returns a DataTable object containing
// department data, which is read in the ItemTemplate of the DataList
list.DataSource = CatalogAccess.GetDepartments();
// Needed to bind the data bound controls to the data source
list.DataBind();
}
}
}
the ShopConfiguration class
{
//Caches the connection string
private readonly static string dbConnectionString;
//Caches the data provider name
private readonly
static string dbProviderName;
//stores the number of products per page
private readonly static int productsPerPage;
//Stores the product description length for product lits
private readonly static int productDescriptionLenght;
//Store the name of your shop
private readonly static string siteName;
//Initialize various proeprties in the constructor
static ShopConfiguration()
{
dbConnectionString = ConfigurationManager.ConnectionStrings["LocalSqlServer"].ConnectionString;
dbProviderName = ConfigurationManager.ConnectionStrings["LocalSqlServer"].ProviderName;
productsPerPage = Int32.Parse(ConfigurationManager.AppSettings["ProductsPerPage"]);
productDescriptionLenght = Int32.Parse(ConfigurationManager.AppSettings["ProductDescriptionLenght"]);
siteName = ConfigurationManager.AppSettings["SiteName"];
}
//Returns the connection string for BeadAfrican database
public static string DbConnectionString
{
get
{
return dbConnectionString;
}
}
//Returns the data provider name
public static string DbProviderName
{
get
{
return dbProviderName;
}
}
I am quite sure that the TypeInitializationException that is thrown has another exception assigned to its InnerException property. If you examine that exception, I think you will find the real cause of your problem.
Sounds like you have specified an invalid setting for DbProviderName so internal checking code reports this exception. You'd better review the connection string settings.