Parsing JSON file with Jackson for Android (Java) - java

Title says all. Trying to simply parse a JSON file I added to this android project via Jackson to use throughout my app.
I was able to to do this successfully in a regular Java Application, but not in an Android project for an unknown reason. Will post both codes.
Android Code:
public class MainActivity extends ActionBarActivity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ObjectMapper mapper = new ObjectMapper();
InputStream is = MainActivity.class.getResourceAsStream("contacts.json");
mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
List<Contact> contacts = null;
Contact contact = null;
try {
// THIS IS THE LINE THAT BREAKS
contacts = mapper.readValue(is, TypeFactory.defaultInstance().constructCollectionType(List.class, Contact.class));
contact = contacts.get(0);
} catch (JsonParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (JsonMappingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
Again, here's the line that breaks:
contacts = mapper.readValue(is, TypeFactory.defaultInstance().constructCollectionType(List.class, Contact.class));
This is the LogCat of the error:
com.fasterxml.jackson.databind.JsonMappingException: No content to map due to end-of-input
I've searched this plenty and have not found a solution for this trivial task. I'd like to also know why this code works in a Java Application but not in an Android Project. If onCreate could throw those exceptions like Main can in the Java App I think it would work, but obviously it can't. Thanks for the solution in advance.
Just for reference, here is my working Java Application code of using Jackson to parse a JSON file.
package test;
// file with accessors and mutators for JSON
import test.Contact;
import java.io.IOException;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.type.TypeFactory;
import com.fasterxml.jackson.core.JsonParseException;
import java.io.InputStream;
import java.util.List;
public class test {
public static void main(String[] args) throws JsonParseException, JsonMappingException, IOException {
ObjectMapper mapper = new ObjectMapper();
InputStream is = test.class.getResourceAsStream("contacts.json");
mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
List<Contact> contacts = mapper.readValue(is, TypeFactory.defaultInstance().constructCollectionType(List.class, Contact.class));
// point to first contact
Contact contact = contacts.get(0);
for (int i = 0; i < contacts.size(); i++) {
contact = contacts.get(i);
System.out.println(contact.getName());
System.out.println(contact.getEmployeeId());
System.out.println(contact.getCompany());
System.out.println(contact.getDetailsURL());
System.out.println(contact.getSmallImageURL());
System.out.println(contact.getBirthdate());
System.out.println(contact.getPhone().getWork());
System.out.println(contact.getPhone().getHome());
System.out.println(contact.getPhone().getMobile());
}
}
}

Related

Convert avro file into csv in java web application

We have a requirement where we would like to convert an avro file which we download from our third party vendor API in our java web application. I tried going through some of the resources where all i could find was command s to execute with help of avro-tools.jar But i am looking for a way to achieve this within Java web application. Any help greatly appreciated.
You can use avro-tools to read the avro records , get Schema and records from the file
Attaching a rough draft :
I'm using JSON as intermediary ,You can modify it to any format of your choice
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.json.CDL;
import org.json.JSONArray;
import org.json.JSONException;
import org.apache.avro.Schema;
import org.apache.avro.Schema.Field;
import org.apache.avro.file.DataFileReader;
import org.apache.avro.generic.GenericData;
import org.apache.avro.generic.GenericDatumReader;
import org.apache.commons.io.FileUtils;
public class AvroToCSV {
public static void readAvro(File file) {
// Read Avro ,parse Schema to get field names and parse it to json
try {
GenericDatumReader<GenericData.Record> datum = new GenericDatumReader<GenericData.Record>();
DataFileReader<GenericData.Record> reader = new DataFileReader<GenericData.Record>(file, datum);
GenericData.Record record = new GenericData.Record(reader.getSchema());
Schema schema = reader.getSchema();
List<String> fieldValues = new ArrayList<>();
JSONArray jsonArray = new JSONArray();
for (Field field : schema.getFields()) {
fieldValues.add(field.name());
}
while (reader.hasNext()) {
reader.next(record);
Map<String, String> jsonFileds = new HashMap<String, String>();
for (String item : fieldValues) {
System.out.println(item);
jsonFileds.put(item, record.get(item).toString());
}
jsonArray.put(jsonFileds);
}
System.out.println(jsonArray.toString());
reader.close();
jsonToCSV(jsonArray);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void jsonToCSV(JSONArray json) {
File file = new File("avroToJson.csv");
String csv;
try {
csv = CDL.toString(json);
FileUtils.writeStringToFile(file, csv);
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void main(String[] args) {
File f = new File("test.avro");
readAvro(f);
}
}

Android: Sort JSON

New to using REST API and JSON files, but I have retrieved data from a weather API about my current locations weather conditions. The JSON file has data, such as my location, weather speed etc. I wish to sort all these individual parts of data into textViews so they can be clearly seen.
My Asynch Class:
import android.app.Activity;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.os.AsyncTask;
import java.util.ArrayList;
import android.widget.*;
import java.util.Date;
import android.util.Log;
public class RESTAPI extends Activity {
ArrayList<String> items = new ArrayList<String>();
// json test string
String jsonTest;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_restapi);
// start the AsyncTask for calling the REST service using httpConnect class
new AsyncTaskParseJson().execute();
}
// added asynctask class methods below - you can make this class as a separate class file
public class AsyncTaskParseJson extends AsyncTask<String, String, String> {
// set the url of the web service to call
String yourServiceUrl = "http://api.apixu.com/v1/current.json?key=e87e62510df946cc84c02652162112&q=LN11RX";
#Override
protected void onPreExecute() {
}
#Override
protected String doInBackground(String... arg0) {
try {
// create new instance of the httpConnect class
httpConnect jParser = new httpConnect();
// get json string from service url
String json = jParser.getJSONFromUrl(yourServiceUrl);
// save returned json to your test string
jsonTest = json.toString();
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
#Override
protected void onPostExecute(String strFromDoInBg) {
TextView tv1 = (TextView) findViewById(R.id.jsontext);
tv1.setText(jsonTest);
}
}
}
My httpConnect Class to handle the URL:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import android.util.Log;
public class httpConnect {
final String TAG = "JsonParser.java";
static String json = "";
public String getJSONFromUrl(String url) {
try {
URL u = new URL(url);
HttpURLConnection restConnection = (HttpURLConnection) u.openConnection();
restConnection.setRequestMethod("GET");
restConnection.setRequestProperty("Content-length", "0");
restConnection.setUseCaches(false);
restConnection.setAllowUserInteraction(false);
restConnection.setConnectTimeout(10000);
restConnection.setReadTimeout(10000);
restConnection.connect();
int status = restConnection.getResponseCode();
// switch statement to catch HTTP 200 and 201 errors
switch (status) {
case 200:
case 201:
BufferedReader br = new BufferedReader(new InputStreamReader(restConnection.getInputStream()));
// create a new string builder to store json data returned from the REST service
StringBuilder sb = new StringBuilder();
String line;
// loop through returned data line by line and append to stringbuilder 'sb' variable
while ((line = br.readLine()) != null) {
sb.append(line+"\n");
}
br.close();
try {
json = sb.toString();
} catch (Exception e) {
Log.e(TAG, "Error parsing data " + e.toString());
}
return json;
}
// HTTP 200 and 201 error handling from switch statement
} catch (MalformedURLException ex) {
Log.e(TAG, "Malformed URL ");
} catch (IOException ex) {
Log.e(TAG, "IO Exception ");
}
return null;
}
So is there anyway to sort the returned data and put each bit of data into its own textbox?
Screenshot of JSON:
If I am understanding your question correctly, try giving this link a go. Let me know if I have misunderstood and I will try and help you with an alternative.
Edit:
Roughly another way to dynamically create new a new TextView and set data:
TextView view;
LinearLayout currLayout = (LinearLayout) findViewById(R.id.LinearLayout);
for(String value : items) {
view = new TextView();
view.setText(value);
currLayout.addView(view);
}
Make pojo classes for the response you get:
Open any converting site like this:-
http://www.jsonschema2pojo.org/
here paste the json reponse, and click zip, all pojo class will be automatically created for you.
Now in your code do this
protected String doInBackground(String... arg0) {
try {
// create new instance of the httpConnect class
httpConnect jParser = new httpConnect();
// get json string from service url
String json = jParser.getJSONFromUrl(yourServiceUrl);
// save returned json to your test string
jsonTest = json.toString();
Gson gson = new Gson();
/*here Example class is the main pojo class, you can use this class which will be there in the zip, which is created from jsontoPojo converting site */
Example response = gson.fromJson(json, Example.class);
/*
*Now to get data
* just do this */
String name = getLocation().getName();
.
.
.
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
For details view this example,example of data parsing using gson
And you need to add this dependency as well in build.gradle file
compile 'com.google.code.gson:gson:2.4'

Java: JSONObject.toString() - adding extra space after colon (":" -> ": ")

Been googling a while but haven't found anything useful since keywords are too common.
Currently in my Java code method toString() (called on some JSONObject object) produces output like this:
{"key_name":<numerical_value>}
while what I need (due to the fact that parser on the other side of the project is imperfect) is:
{"key_name": <numerical_value>}
The only difference is that extra space after colon. Is there any JSONObject builtin way to do it or do I need some handwritten simple string operation for it?
I can only imagine building a custom JSONStringer. In fact, it could be a JSONWriter built using a StringWriter. Then you override all the value methods to add a space before calling parent class method.
class JSONSpaceStringer extends JSONWriter {
private StringWriter sw;
JSONSpaceStringer(StringWriter sw) {
parent(sw); // initialize parent
this.sw = sw;
}
#Override
public JSONWriter value(long l) throws JSONException {
sw.write(" "); // add an initial space
parent.value(l); // and let parent class do the job...
}
// same for all other value methods
//...
}
use the below code. it will help you to solve the problem.
package com.javamad.utils;
import java.io.IOException;
import org.apache.log4j.Logger;
import org.codehaus.jackson.JsonGenerationException;
import org.codehaus.jackson.JsonParseException;
import org.codehaus.jackson.map.JsonMappingException;
import org.codehaus.jackson.map.ObjectMapper;
public class JsonUtils {
private static Logger logger = Logger.getLogger(JsonUtils.class.getName());
public static <T> T jsonToJavaObject(String jsonRequest, Class<T> valueType)
throws JsonParseException, JsonMappingException, IOException {
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.configure(org.codehaus.jackson.map.DeserializationConfig.Feature.UNWRAP_ROOT_VALUE,false);
T finalJavaRequest = objectMapper.readValue(jsonRequest, valueType);
return finalJavaRequest;
}
public static String javaToJson(Object o) {
String jsonString = null;
try {
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.configure(org.codehaus.jackson.map.DeserializationConfig.Feature.UNWRAP_ROOT_VALUE,true);
jsonString = objectMapper.writeValueAsString(o);
} catch (JsonGenerationException e) {
logger.error(e);
} catch (JsonMappingException e) {
logger.error(e);
} catch (IOException e) {
logger.error(e);
}
return jsonString;
}
}

What does this JSON processing error indicate?

I am trying to process a JSON file, but I have no idea why this exception is being triggered. I am using sample code, so I am not sure how org.codehaus.jackson clearly works.
import java.io.File;
import skf.config.Configuration;
import skf.config.ConfigurationFactory;
import federate.ExcavatorFederate;
import federate.ExcavatorFederateAmbassador;
public class ExcavatorMain {
private static final File configurationFile = new File(
"testResources/configuration/conf.json");
public static void main(String[] args) {
System.out.println(configurationFile);
// TODO Auto-generated method stubz
ExcavatorFederateAmbassador fedamb = new ExcavatorFederateAmbassador();
ExcavatorFederate fed = new ExcavatorFederate(fedamb);
ConfigurationFactory factory = null;
Configuration config = null;
try {
factory = new ConfigurationFactory();
config = factory.importConfiguration(configurationFile);
fed.configureAndStart(config);
} catch (Exception e) {
e.printStackTrace();
}
}
}
The exceptions were:
testResources\configuration\conf.json
Exception in thread "main" java.lang.NoSuchMethodError: org.codehaus.jackson.type.JavaType.<init>(Ljava/lang/Class;I)V
at org.codehaus.jackson.map.type.TypeBase.<init>(TypeBase.java:34)
at org.codehaus.jackson.map.type.SimpleType.<init>(SimpleType.java:46)
at org.codehaus.jackson.map.type.SimpleType.constructUnsafe(SimpleType.java:63)
at org.codehaus.jackson.map.ObjectMapper.<clinit>(ObjectMapper.java:183)
at skf.config.ConfigurationFactory.getObjectMapper(ConfigurationFactory.java:72)
at skf.config.ConfigurationFactory.importConfiguration(ConfigurationFactory.java:53)
at ExcavatorMain.main(ExcavatorMain.java:25)
I don't get it.

Unable to search journal article in liferay portlet using search util

I am using the below code but it is not able to search the journal article/web content in liferay 6.1
package com.abp.portlets;
import java.io.IOException;
import javax.portlet.PortletException;
import javax.portlet.RenderRequest;
import javax.portlet.RenderResponse;
import com.liferay.portal.kernel.search.BooleanClauseOccur;
import com.liferay.portal.kernel.search.BooleanQuery;
import com.liferay.portal.kernel.search.BooleanQueryFactoryUtil;
import com.liferay.portal.kernel.search.Field;
import com.liferay.portal.kernel.search.Hits;
import com.liferay.portal.kernel.search.ParseException;
import com.liferay.portal.kernel.search.SearchContext;
import com.liferay.portal.kernel.search.SearchEngineUtil;
import com.liferay.portal.kernel.search.SearchException;
import com.liferay.portal.kernel.util.Validator;
import com.liferay.portal.kernel.util.WebKeys;
import com.liferay.portal.theme.ThemeDisplay;
import com.liferay.util.bridges.mvc.MVCPortlet;
/**
* Portlet implementation class Search
*/
public class Search extends MVCPortlet {
public void doView(RenderRequest renderRequest, RenderResponse renderResponse)throws IOException, PortletException
{
ThemeDisplay themeDisplay = (ThemeDisplay)
renderRequest.getAttribute(WebKeys.THEME_DISPLAY);
SearchContext searchContext = new SearchContext();
searchContext.setSearchEngineId(SearchEngineUtil.SYSTEM_ENGINE_ID);
BooleanQuery contextQuery = BooleanQueryFactoryUtil.create(searchContext);
contextQuery.addRequiredTerm(Field.COMPANY_ID, themeDisplay.getCompanyId());
contextQuery.addRequiredTerm(Field.GROUP_ID, themeDisplay.getScopeGroupId());
BooleanQuery fullQuery = BooleanQueryFactoryUtil.create(searchContext);
String keywords = "mridul test";
BooleanQuery searchQuery = BooleanQueryFactoryUtil.create(searchContext);
if (Validator.isNotNull(keywords)) {
keywords = keywords.trim();
try {
searchQuery.addTerm(Field.TITLE, keywords,true);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
// BooleanQuery fullQuery = BooleanQueryFactoryUtil.create(searchContext);
//fullQuery.add(contextQuery, BooleanClauseOccur.MUST);
// if (searchQuery.clauses().size() > 0) {
// fullQuery.add(searchQuery, BooleanClauseOccur.MUST);
// }
System.out.println("fullQuery===============>>"+fullQuery);
try {
fullQuery.add(searchQuery, BooleanClauseOccur.MUST);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
Hits hits = SearchEngineUtil.search(searchContext, fullQuery);
for(int i=0;i<hits.getLength();i++)
{
System.out.println(hits.snippet(i).toString());
}
} catch (SearchException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
The output I am getting is...
fullQuery===============>>+(+((title:mridul title:test)))
Please help..
Lucene uses fields to index data.
searchQuery.addTerm(**Field.CONTENT**, keywords,true);
Or
searchQuery.addTerms(new String[]{Field.TITLE,Field.DESCRIPTION,Field.CONTENT}, keywords, true)
It looks like you are searching for the exact phrase "mridul test". I think you probably want to search for "mridul" and "test". If so, give this a spin:
String[] terms = keywords.split(" ");
for(String term : terms){
searchQuery.addTerm(Field.TITLE, term,true);
}

Categories

Resources