Hi I am writing a web server to be hosted locally that will have latitude and longitude posted in the URL/URI from an android device and this will be used as search criteria in an SQL Select query to retrieve the 5 clostes train stations.
I have made the code work with the hard coded Longitude and Latitude but now need to add in the functionality of it being dynamically added form teh adnroid device using the Post/Get functions, unfortunately i have never used get/post so dont know where to start.
Below is my code from all Classes in the web server, as said it all works hardcoded but now needs to accept input from an android device and return the same expected results. Thanks
public class WebServer {
static String jArray = "";
public static void main(String[] args) {
try{
HttpServer server = HttpServer.create(new InetSocketAddress(8080),0);
server.createContext("/",new HttpHandler(){
public void handle(HttpExchange he) throws IOException{
try {
jArray = sqlConnector.train(jArray);
} catch (Throwable e) {
e.printStackTrace();
}
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(he.getResponseBody()));
System.out.println("Processing Request");
he.sendResponseHeaders(200, 0);
String output = "<html><head></head><body><p>" + jArray + "</p></body></html>";
bw.write(output);
bw.close();
}
});
server.start();
System.out.println("Started up . . .");
}
catch (IOException ioe){
System.err.println("problems Starting Webserver: " + ioe);
}
}
}
public class sqlConnector {
public static String train(String jArray) throws Exception{
PreparedStatement s = null;
try
{
Connection c = DriverManager.getConnection("jdbc:sqlite:C:/Users/Colin/trainstations.db");
s = c.prepareStatement("SELECT Latitude, Longitude, StationName,( 3959 * acos(cos(radians(53.4355)) * cos(radians(Latitude)) * cos(radians(Longitude) - radians(-3.0508)) + sin(radians(53.4355)) * sin(radians(Latitude )))) AS distance FROM stations ORDER BY distance ASC LIMIT 0,5;");
ResultSet rs = s.executeQuery();
jArray = jsonConverter.convertResultSetIntoJSON(rs, jArray);
}
catch (SQLException se)
{
se.printStackTrace();
}
return jArray;
}
}
public class jsonConverter {
public static String convertResultSetIntoJSON(ResultSet rs, String jArray) throws Exception {
JSONArray jsonArray = new JSONArray();
while (rs.next()) {
int total_rows = rs.getMetaData().getColumnCount();
JSONObject obj = new JSONObject();
for (int i = 0; i < total_rows; i++) {
String columnName = rs.getMetaData().getColumnLabel(i + 1).toString();
Object columnValue = rs.getObject(i + 1);
obj.put(columnName, columnValue);
}
jsonArray.put(obj);
}
jArray = jsonArray.toString();
return jArray;
}
}
I am currently connected to another webserver that hosts the same data and is fully functinal and after the port number its format is as follows
/stations?lat=" + lat + "&lng=" + lng);
where lat and lng are my variable taken using GPS
The process would be like this:
1) Parse the Parameters from query String he.getRequestURI().getQuery()
/**
* returns the url parameters in a map
* #param query
* #return map
*/
public static Map<String, String> queryToMap(String query){
Map<String, String> result = new HashMap<String, String>();
for (String param : query.split("&")) {
String pair[] = param.split("=");
if (pair.length>1) {
result.put(pair[0], pair[1]);
}else{
result.put(pair[0], "");
}
}
return result;
}
2) Pass these parameters into select method
public static String train(String jArray, double lat, double long) throws Exception{
3) Use it in your statement
s = c.prepareStatement("SELECT Latitude, Longitude, StationName,
( 3959 * acos(cos(radians(?)) * cos(radians(Latitude))
* cos(radians(Longitude) - radians(?)) + sin(radians(?))
* sin(radians(Latitude ))))
AS distance FROM stations ORDER BY distance ASC LIMIT 0,5;");
s.setDouble(1, lat);
s.setDouble(2, long);
s.setDouble(3, lat);
And finally
4) Fix your code.
use a Database connection pool instead of connecting to the database for every call
use try-with-resource to automatically close ResultSet, PreparedStatement, Connection (even in case of Errors)
Hope that helps as a rough guideline :-)
Related
I have to do some custom preprocessing tasks on a huge data file (~200GB).
currently, its works as below way.
select * from table
preprocessing line by line
return a new single flow file
so I decided to convert the above approach to the below way.
get the row count from the user (let's assume the user gives 1000)
execute select * query as resultSet
read the results line by line (rs.next())
when the line count reaches 1000 return the flow file and continues to other lines
So my approach is as below
onTrigger
public void onTrigger(final ProcessContext context, final ProcessSession session) throws ProcessException {
logger = getLogger();
FlowFile flowFile = session.get();
if (flowFile == null) {
return;
}
try {
final Long rowLimit = context.getProperty(ProcessorUtils.MAX_RECORD).evaluateAttributeExpressions(flowFile).asLong();
Connection conn = DriverManager.getConnection(
// db connection properties
);
Statement stm = conn.createStatement(ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY);
ResultSet rs = stm.executeQuery("sql query");
Map<String, String> flowFileAttributes = flowFile.getAttributes();
process(
rs,
session,
flowFileAttributes,
rowLimit,
);
FlowFile stateFlowFile = session.create();
session.putAttribute(stateFlowFile, "processing_status", "end");
session.putAttribute(stateFlowFile, "record_count", "0");
session.transfer(stateFlowFile, GPReaderProcessorUtils.STATUS); // working line
} catch (Exception e) {
logger.warn(" conn " + e);
session.transfer(flowFile, GPReaderProcessorUtils.FAILURE);
}
}
Recursion Approach for termination based on line count
private void process(ResultSet rs, ProcessSession session, Map<String, String> flowFileAttributes, Long rowLimit) throws SQLException {
try{
logger.info("-> start processing with row limit = " + rowLimit);
AtomicInteger mainI = new AtomicInteger(0);
FlowFile flowFile =
session.write(session.putAllAttributes(session.create(), flowFileAttributes), (OutputStream out) -> {
int i = 0;
Map<String, String> preProcessResults = null;
try {
String res = "";
while (i < rowLimit && rs.next()) {
//preprocessing happens here
i++;
mainI.set(i);
out.write(preprocess results.toString().getBytes(StandardCharsets.UTF_8));
}
}catch (SQLException e) {
e.printStackTrace();
}
}
logger.info("gp-log ->"+ (String.valueOf(i)));
out.close();
});
FlowFile stateFlowFile = session.create();
session.putAttribute(stateFlowFile, "processing_status", "processing");
session.putAttribute(stateFlowFile, "record_count", mainI.toString());
session.transfer(stateFlowFile, GPReaderProcessorUtils.STATUS); // state relationship
session.transfer(flowFile, GPReaderProcessorUtils.SUCCESS); // preprocessed flow files returns
if(!rs.isAfterLast() && mainI != 0 && !rs.isLast()){ // recurrsion call
logger.info("gp-log -> recursion call" );
process(rs, session,flowFileAttributes,column,rowLimit);
}
}catch (Exception e){
logger.info(e.getMessage());
logger.error(e.getMessage());
session.transfer(session.putAllAttributes(session.create(),flowFileAttributes), GPReaderProcessorUtils.FAILURE);
}
}
Expected Behaviour -> while processing this one return completed rows as flow files
Current Behaviour -> after finishing all return all flow files (generated in recursion) once.
please advise on this.
your processor should extend AbstractSessionFactoryProcessor and create/commit sessions for incoming file and for each outgoing file.
files going to output queue as soon as session been committed.
[Update]
I'm new in weka. I want to add my double[] array to my weka Instances dataRaw but I have no idea how to do it.
This is my Code:
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import weka.core.DenseInstance;
import weka.core.Instances;
public class SVMTest
{
private Connection connect;
public SVMTest() throws Exception
{
try
{
String jdbcDriver ="org.gjt.mm.mysql.Driver";
String jdbcURL = "jdbc:mysql://localhost:3306/xign?";
Class.forName("com.mysql.jdbc.Driver");
connect = DriverManager
.getConnection("jdbc:mysql://localhost:3306/myDB?"
+ "user=" + "root" + "&password=" +
"xxx###111");
} catch (ClassNotFoundException ex)
{
Logger.getLogger(SVMTest.class.getName()).log(Level.SEVERE, null, ex);
}
}
public ArrayList<Double[]> loadValues(String generatedString) throws SQLException
{
ArrayList<Double[]> pictures = new ArrayList<>();
PreparedStatement ps = null;
ResultSet rs = null;
Double picture[] = new Double[3];
try
{
ps = connect.prepareStatement("SELECT X, Y, Z FROM myDB.Sensor WHERE key = ?");
ps.setString(1, generatedString);
rs = ps.executeQuery();
while(rs.next())
{
picture[0] = (rs.getDouble("X") * 100000);
picture[1] = (rs.getDouble("Y") * 100000);
picture[2] = (rs.getDouble("Z") * 100000);
pictures.add(picture);
picture = new Long[3];
}
}
catch (SQLException ex)
{
Logger.getLogger(SVMTest.class.getName()).log(Level.SEVERE, null, ex);
}
finally
{
if(rs != null )
try{ rs.close(); }
catch(SQLException ex) { ex.printStackTrace(); }
if(ps != null)
try{ ps.close(); }
catch(SQLException ex) { ex.printStackTrace(); }
}
return pictures;
}
public double [] toRawArray(Double[] array)
{
double[] out = new double[array.length];
for(int i = 0; i < array.length; i++)
{
out[i] = array[i];
}
return out;
}
public static void main(String[] args) throws Exception
{
SVMTest svm = new SVMTest();
ArrayList<Double[]> myValues = svm.loadValues("123456ASDF");
//at this point I want to add ArrayList<Double[]> myValues to
//weka Instances to classify the data but I don't really have
//an idea
Instances dataRaw = new Instances(?????); <--Error
for(Double[] a : myValues)
{
DenseInstance myDense = new DenseInstance(1.0, toRawArray(a));
dataRaw.add((Instance)myDense.dataset());
}
}
}
Double[] a looks like this:
for(Double[] a : alValues)
{
for(Double b : a))
{
System.out.print("[" + b + "]");
}
System.out.println();
}
//Output:
//[-1198.54][8534.44][4293.29]
//[-994.13][8812.43][3534.66]
//[-818.84][9026.96][2915.99]
//[-670.76][9186.82][2436.73]
Just basic explanation :-
First, to classify you need a model and to get a model you need to train an algorithm on data with attributes and classIndex.
Attributes is "Type of data", assuming if you have data of employees then name, desgination, age , salary etc are attributes or in simple terms column names in csv file.
Type of data can be Numeric (Integer or Real) or Nominal meaning normal string.
Classindex is the attribute/column index which you want your algorithm to predict/classify based on training instances. For example you can predict salary using age and designation.
After model is generated, on that model you can do classification (prediction) by sending data in similar format meaning instance created with same attributes and classindex.
You need to be sure about which algorithm you want to run and which attribute/column index you want to predict.
[Note :- There are algorithms which work with only numeric data and some other algorithms work only on Nominal data and some algorithm will work on both types of data. So you should pick an algorithm depending on type of data. There are other stuffs which you should check before selecting an algorithm but basic is the type of data.]
I suggest you to go through about machine learning and weka before attempting to run an algorithm.
Sample code which you can try and I am assuming your classindex to be z :-
ArrayList<Attribute> attributes = new ArrayList<Attribute>();
attributes.add(new Attribute("x"));
attributes.add(new Attribute("y"));
attributes.add(new Attribute("z"));
Instances dataRaw = new Instances("TestInstances", attributes , 0);
dataRaw.setClassIndex(dataRaw.numAttributes() - 1); // Assuming z (z on lastindex) as classindex
for (Double[] a: myValues) {
dataRaw.add(new DenseInstance(1.0, a));
}
// Then train or build the algorithm/model on instances (dataRaw) created above.
MultilayerPerceptron mlp = new MultilayerPerceptron(); // Sample algorithm, go through about neural networks to use this or replace with appropriate algorithm.
mlp.buildClassifier(dataRaw);
// Create a test instance,I think you can create testinstance without
// classindex value but cross check in weka as I forgot about it.
double[] values = new double[]{-818.84, 9186.82, 2436.73}; // sample values
DenseInstance testInstance = new DenseInstance(1.0, values);
testInstance.setDataset(dataRaw); // To associate with instances object
// now you can clasify
double classify = mlp.classifyInstance(testInstance);
For more information :- How to use weka programmatically
My app i've created using android studio keeps crashing and I don't know why. Its a weather app and i'm following google's developing android app course, here's the logcat:
07-20 18:19:45.740 16410-16434/com.alexander.sunshine E/ActivityThread﹕ Failed to find provider info for com.alexander.android.sunshine.app
07-20 18:19:45.748 16410-16434/com.alexander.sunshine E/AndroidRuntime﹕ FATAL EXCEPTION: AsyncTask #1
Process: com.alexander.sunshine, PID: 16410
java.lang.RuntimeException: An error occured while executing doInBackground()
at android.os.AsyncTask$3.done(AsyncTask.java:304)
at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:355)
at java.util.concurrent.FutureTask.setException(FutureTask.java:222)
at java.util.concurrent.FutureTask.run(FutureTask.java:242)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
at java.lang.Thread.run(Thread.java:818)
Caused by: java.lang.NullPointerException: Attempt to invoke interface method 'boolean android.database.Cursor.moveToFirst()' on a null object reference
at com.alexander.sunshine.app.FetchWeatherTask.addLocation(FetchWeatherTask.java:109)
at com.alexander.sunshine.app.FetchWeatherTask.getWeatherDataFromJson(FetchWeatherTask.java:214)
at com.alexander.sunshine.app.FetchWeatherTask.doInBackground(FetchWeatherTask.java:414)
at com.alexander.sunshine.app.FetchWeatherTask.doInBackground(FetchWeatherTask.java:34)
at android.os.AsyncTask$2.call(AsyncTask.java:292)
at java.util.concurrent.FutureTask.run(FutureTask.java:237)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
at java.lang.Thread.run(Thread.java:818)
And here's the FetchWeatherTask.java:
package com.alexander.sunshine.app;
import android.content.ContentUris;
import android.content.ContentValues;
import android.content.Context;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.database.DatabaseUtils;
import android.net.Uri;
import android.os.AsyncTask;
import android.preference.PreferenceManager;
import android.text.format.Time;
import android.util.Log;
import android.widget.ArrayAdapter;
import com.alexander.sunshine.R;
import com.alexander.sunshine.app.data.WeatherContract;
import com.alexander.sunshine.app.data.WeatherContract.WeatherEntry;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Vector;
public class FetchWeatherTask extends AsyncTask<String, Void, String[]> {
private final String LOG_TAG = FetchWeatherTask.class.getSimpleName();
private ArrayAdapter<String> mForecastAdapter;
private final Context mContext;
public FetchWeatherTask(Context context, ArrayAdapter<String> forecastAdapter) {
mContext = context;
mForecastAdapter = forecastAdapter;
}
private boolean DEBUG = true;
/* The date/time conversion code is going to be moved outside the asynctask later,
* so for convenience we're breaking it out into its own method now.
*/
private String getReadableDateString(long time){
// Because the API returns a unix timestamp (measured in seconds),
// it must be converted to milliseconds in order to be converted to valid date.
Date date = new Date(time);
SimpleDateFormat format = new SimpleDateFormat("E, MMM d");
return format.format(date).toString();
}
/**
* Prepare the weather high/lows for presentation.
*/
private String formatHighLows(double high, double low) {
// Data is fetched in Celsius by default.
// If user prefers to see in Fahrenheit, convert the values here.
// We do this rather than fetching in Fahrenheit so that the user can
// change this option without us having to re-fetch the data once
// we start storing the values in a database.
SharedPreferences sharedPrefs =
PreferenceManager.getDefaultSharedPreferences(mContext);
String unitType = sharedPrefs.getString(
mContext.getString(R.string.pref_units_key),
mContext.getString(R.string.pref_units_metric));
if (unitType.equals(mContext.getString(R.string.pref_units_imperial))) {
high = (high * 1.8) + 32;
low = (low * 1.8) + 32;
} else if (!unitType.equals(mContext.getString(R.string.pref_units_metric))) {
Log.d(LOG_TAG, "Unit type not found: " + unitType);
}
// For presentation, assume the user doesn't care about tenths of a degree.
long roundedHigh = Math.round(high);
long roundedLow = Math.round(low);
String highLowStr = roundedHigh + "/" + roundedLow;
return highLowStr;
}
/**
* Helper method to handle insertion of a new location in the weather database.
*
* #param locationSetting The location string used to request updates from the server.
* #param cityName A human-readable city name, e.g "Mountain View"
* #param lat the latitude of the city
* #param lon the longitude of the city
* #return the row ID of the added location.
*/
long addLocation(String locationSetting, String cityName, double lat, double lon) {
long locationId;
// First, check if the location with this city name exists in the db
Cursor locationCursor = mContext.getContentResolver().query(
WeatherContract.LocationEntry.CONTENT_URI,
new String[]{WeatherContract.LocationEntry._ID},
WeatherContract.LocationEntry.COLUMN_LOCATION_SETTING + " = ?",
new String[]{locationSetting},
null);
if (locationCursor.moveToFirst()) {
int locationIdIndex = locationCursor.getColumnIndex(WeatherContract.LocationEntry._ID);
locationId = locationCursor.getLong(locationIdIndex);
} else {
// Now that the content provider is set up, inserting rows of data is pretty simple.
// First create a ContentValues object to hold the data you want to insert.
ContentValues locationValues = new ContentValues();
// Then add the data, along with the corresponding name of the data type,
// so the content provider knows what kind of value is being inserted.
locationValues.put(WeatherContract.LocationEntry.COLUMN_CITY_NAME, cityName);
locationValues.put(WeatherContract.LocationEntry.COLUMN_LOCATION_SETTING, locationSetting);
locationValues.put(WeatherContract.LocationEntry.COLUMN_COORD_LAT, lat);
locationValues.put(WeatherContract.LocationEntry.COLUMN_COORD_LONG, lon);
// Finally, insert location data into the database.
Uri insertedUri = mContext.getContentResolver().insert(
WeatherContract.LocationEntry.CONTENT_URI,
locationValues
);
// The resulting URI contains the ID for the row. Extract the locationId from the Uri.
locationId = ContentUris.parseId(insertedUri);
}
locationCursor.close();
// Wait, that worked? Yes!
return locationId;
}
/*
Students: This code will allow the FetchWeatherTask to continue to return the strings that
the UX expects so that we can continue to test the application even once we begin using
the database.
*/
String[] convertContentValuesToUXFormat(Vector<ContentValues> cvv) {
// return strings to keep UI functional for now
String[] resultStrs = new String[cvv.size()];
for ( int i = 0; i < cvv.size(); i++ ) {
ContentValues weatherValues = cvv.elementAt(i);
String highAndLow = formatHighLows(
weatherValues.getAsDouble(WeatherEntry.COLUMN_MAX_TEMP),
weatherValues.getAsDouble(WeatherEntry.COLUMN_MIN_TEMP));
resultStrs[i] = getReadableDateString(
weatherValues.getAsLong(WeatherEntry.COLUMN_DATE)) +
" - " + weatherValues.getAsString(WeatherEntry.COLUMN_SHORT_DESC) +
" - " + highAndLow;
}
return resultStrs;
}
/**
* Take the String representing the complete forecast in JSON Format and
* pull out the data we need to construct the Strings needed for the wireframes.
*
* Fortunately parsing is easy: constructor takes the JSON string and converts it
* into an Object hierarchy for us.
*/
private String[] getWeatherDataFromJson(String forecastJsonStr,
String locationSetting)
throws JSONException {
// Now we have a String representing the complete forecast in JSON Format.
// Fortunately parsing is easy: constructor takes the JSON string and converts it
// into an Object hierarchy for us.
// These are the names of the JSON objects that need to be extracted.
// Location information
final String OWM_CITY = "city";
final String OWM_CITY_NAME = "name";
final String OWM_COORD = "coord";
// Location coordinate
final String OWM_LATITUDE = "lat";
final String OWM_LONGITUDE = "lon";
// Weather information. Each day's forecast info is an element of the "list" array.
final String OWM_LIST = "list";
final String OWM_PRESSURE = "pressure";
final String OWM_HUMIDITY = "humidity";
final String OWM_WINDSPEED = "speed";
final String OWM_WIND_DIRECTION = "deg";
// All temperatures are children of the "temp" object.
final String OWM_TEMPERATURE = "temp";
final String OWM_MAX = "max";
final String OWM_MIN = "min";
final String OWM_WEATHER = "weather";
final String OWM_DESCRIPTION = "main";
final String OWM_WEATHER_ID = "id";
try {
JSONObject forecastJson = new JSONObject(forecastJsonStr);
JSONArray weatherArray = forecastJson.getJSONArray(OWM_LIST);
JSONObject cityJson = forecastJson.getJSONObject(OWM_CITY);
String cityName = cityJson.getString(OWM_CITY_NAME);
JSONObject cityCoord = cityJson.getJSONObject(OWM_COORD);
double cityLatitude = cityCoord.getDouble(OWM_LATITUDE);
double cityLongitude = cityCoord.getDouble(OWM_LONGITUDE);
long locationId = addLocation(locationSetting, cityName, cityLatitude, cityLongitude);
// Insert the new weather information into the database
Vector<ContentValues> cVVector = new Vector<ContentValues>(weatherArray.length());
// OWM returns daily forecasts based upon the local time of the city that is being
// asked for, which means that we need to know the GMT offset to translate this data
// properly.
// Since this data is also sent in-order and the first day is always the
// current day, we're going to take advantage of that to get a nice
// normalized UTC date for all of our weather.
Time dayTime = new Time();
dayTime.setToNow();
// we start at the day returned by local time. Otherwise this is a mess.
int julianStartDay = Time.getJulianDay(System.currentTimeMillis(), dayTime.gmtoff);
// now we work exclusively in UTC
dayTime = new Time();
for(int i = 0; i < weatherArray.length(); i++) {
// These are the values that will be collected.
long dateTime;
double pressure;
int humidity;
double windSpeed;
double windDirection;
double high;
double low;
String description;
int weatherId;
// Get the JSON object representing the day
JSONObject dayForecast = weatherArray.getJSONObject(i);
// Cheating to convert this to UTC time, which is what we want anyhow
dateTime = dayTime.setJulianDay(julianStartDay+i);
pressure = dayForecast.getDouble(OWM_PRESSURE);
humidity = dayForecast.getInt(OWM_HUMIDITY);
windSpeed = dayForecast.getDouble(OWM_WINDSPEED);
windDirection = dayForecast.getDouble(OWM_WIND_DIRECTION);
// Description is in a child array called "weather", which is 1 element long.
// That element also contains a weather code.
JSONObject weatherObject =
dayForecast.getJSONArray(OWM_WEATHER).getJSONObject(0);
description = weatherObject.getString(OWM_DESCRIPTION);
weatherId = weatherObject.getInt(OWM_WEATHER_ID);
// Temperatures are in a child object called "temp". Try not to name variables
// "temp" when working with temperature. It confuses everybody.
JSONObject temperatureObject = dayForecast.getJSONObject(OWM_TEMPERATURE);
high = temperatureObject.getDouble(OWM_MAX);
low = temperatureObject.getDouble(OWM_MIN);
ContentValues weatherValues = new ContentValues();
weatherValues.put(WeatherEntry.COLUMN_LOC_KEY, locationId);
weatherValues.put(WeatherEntry.COLUMN_DATE, dateTime);
weatherValues.put(WeatherEntry.COLUMN_HUMIDITY, humidity);
weatherValues.put(WeatherEntry.COLUMN_PRESSURE, pressure);
weatherValues.put(WeatherEntry.COLUMN_WIND_SPEED, windSpeed);
weatherValues.put(WeatherEntry.COLUMN_DEGREES, windDirection);
weatherValues.put(WeatherEntry.COLUMN_MAX_TEMP, high);
weatherValues.put(WeatherEntry.COLUMN_MIN_TEMP, low);
weatherValues.put(WeatherEntry.COLUMN_SHORT_DESC, description);
weatherValues.put(WeatherEntry.COLUMN_WEATHER_ID, weatherId);
cVVector.add(weatherValues);
}
// add to database
if ( cVVector.size() > 0 ) {
ContentValues[] cvArray = new ContentValues[cVVector.size()];
cVVector.toArray(cvArray);
mContext.getContentResolver().bulkInsert(WeatherEntry.CONTENT_URI, cvArray);
}
// Sort order: Ascending, by date.
String sortOrder = WeatherEntry.COLUMN_DATE + " ASC";
Uri weatherForLocationUri = WeatherEntry.buildWeatherLocationWithStartDate(
locationSetting, System.currentTimeMillis());
// Students: Uncomment the next lines to display what what you stored in the bulkInsert
Cursor cur = mContext.getContentResolver().query(weatherForLocationUri,
null, null, null, sortOrder);
cVVector = new Vector<ContentValues>(cur.getCount());
if ( cur.moveToFirst() ) {
do {
ContentValues cv = new ContentValues();
DatabaseUtils.cursorRowToContentValues(cur, cv);
cVVector.add(cv);
} while (cur.moveToNext());
}
Log.d(LOG_TAG, "FetchWeatherTask Complete. " + cVVector.size() + " Inserted");
String[] resultStrs = convertContentValuesToUXFormat(cVVector);
return resultStrs;
} catch (JSONException e) {
Log.e(LOG_TAG, e.getMessage(), e);
e.printStackTrace();
}
return null;
}
#Override
protected String[] doInBackground(String... params) {
// If there's no zip code, there's nothing to look up. Verify size of params.
if (params.length == 0) {
return null;
}
String locationQuery = params[0];
// These two need to be declared outside the try/catch
// so that they can be closed in the finally block.
HttpURLConnection urlConnection = null;
BufferedReader reader = null;
// Will contain the raw JSON response as a string.
String forecastJsonStr = null;
String format = "json";
String units = "metric";
int numDays = 14;
try {
// Construct the URL for the OpenWeatherMap query
// Possible parameters are avaiable at OWM's forecast API page, at
// http://openweathermap.org/API#forecast
final String FORECAST_BASE_URL =
"http://api.openweathermap.org/data/2.5/forecast/daily?";
final String QUERY_PARAM = "q";
final String FORMAT_PARAM = "mode";
final String UNITS_PARAM = "units";
final String DAYS_PARAM = "cnt";
Uri builtUri = Uri.parse(FORECAST_BASE_URL).buildUpon()
.appendQueryParameter(QUERY_PARAM, params[0])
.appendQueryParameter(FORMAT_PARAM, format)
.appendQueryParameter(UNITS_PARAM, units)
.appendQueryParameter(DAYS_PARAM, Integer.toString(numDays))
.build();
URL url = new URL(builtUri.toString());
// Create the request to OpenWeatherMap, and open the connection
urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setRequestMethod("GET");
urlConnection.connect();
// Read the input stream into a String
InputStream inputStream = urlConnection.getInputStream();
StringBuffer buffer = new StringBuffer();
if (inputStream == null) {
// Nothing to do.
return null;
}
reader = new BufferedReader(new InputStreamReader(inputStream));
String line;
while ((line = reader.readLine()) != null) {
// Since it's JSON, adding a newline isn't necessary (it won't affect parsing)
// But it does make debugging a *lot* easier if you print out the completed
// buffer for debugging.
buffer.append(line + "\n");
}
if (buffer.length() == 0) {
// Stream was empty. No point in parsing.
return null;
}
forecastJsonStr = buffer.toString();
} catch (IOException e) {
Log.e(LOG_TAG, "Error ", e);
// If the code didn't successfully get the weather data, there's no point in attemping
// to parse it.
return null;
} finally {
if (urlConnection != null) {
urlConnection.disconnect();
}
if (reader != null) {
try {
reader.close();
} catch (final IOException e) {
Log.e(LOG_TAG, "Error closing stream", e);
}
}
}
try {
return getWeatherDataFromJson(forecastJsonStr, locationQuery);
} catch (JSONException e) {
Log.e(LOG_TAG, e.getMessage(), e);
e.printStackTrace();
}
// This will only happen if there was an error getting or parsing the forecast.
return null;
}
#Override
protected void onPostExecute(String[] result) {
if (result != null && mForecastAdapter != null) {
mForecastAdapter.clear();
for(String dayForecastStr : result) {
mForecastAdapter.add(dayForecastStr);
}
// New data is back from the server. Hooray!
}
}
}
As the stack trace says, you're trying to call moveToFirst() on a null reference. locationCursor is the only object you're calling that method on, so it must be null.
The docs for query() say that it can return null, so you'll should null check that object, and you should also check your invocation of query() to try to understand why it's returning null.
Clearly, in your addLocation method, the locationCursor you get by calling Cursor locationCursor = mContext.getContentResolver().query(...) is null, which causes the crash when you call locationCursor.moveToFirst() just after that.
I suggest to check out this problem's accepted answer. The problem might be that the query is empty.
In any case, you should always check whether the Cursor is null before you apply methods to it, and deal with that case somehow by alerting the user.
The method could look like this:
long addLocation(String locationSetting, String cityName, double lat, double lon) {
long locationId;
// First, check if the location with this city name exists in the db
Cursor locationCursor = mContext.getContentResolver().query(
WeatherContract.LocationEntry.CONTENT_URI,
new String[]{WeatherContract.LocationEntry._ID},
WeatherContract.LocationEntry.COLUMN_LOCATION_SETTING + " = ?",
new String[]{locationSetting},
null);
if (locationCursor != null) {
if (locationCursor.moveToFirst()) {
int locationIdIndex = locationCursor.getColumnIndex(WeatherContract.LocationEntry._ID);
locationId = locationCursor.getLong(locationIdIndex);
locationCursor.close();
return locationId;
}
}
// Now that the content provider is set up, inserting rows of data is pretty simple.
// First create a ContentValues object to hold the data you want to insert.
ContentValues locationValues = new ContentValues();
// Then add the data, along with the corresponding name of the data type,
// so the content provider knows what kind of value is being inserted.
locationValues.put(WeatherContract.LocationEntry.COLUMN_CITY_NAME, cityName);
locationValues.put(WeatherContract.LocationEntry.COLUMN_LOCATION_SETTING, locationSetting);
locationValues.put(WeatherContract.LocationEntry.COLUMN_COORD_LAT, lat);
locationValues.put(WeatherContract.LocationEntry.COLUMN_COORD_LONG, lon);
// Finally, insert location data into the database.
Uri insertedUri = mContext.getContentResolver().insert(
WeatherContract.LocationEntry.CONTENT_URI,
locationValues
);
// The resulting URI contains the ID for the row. Extract the locationId from the Uri.
locationId = ContentUris.parseId(insertedUri);
locationCursor.close();
// Wait, that worked? Yes!
return locationId;
}
I want to use Rally Rest Toolkit java to get all iterations between 2014-06-01 and 2014-06-08,then get all tasks under these iterations.How can I?
Any help would be great! Many Thanks!
To get tasks of iteration that falls within certain dates use this syntax:
taskRequest.setQueryFilter(new QueryFilter("Iteration.StartDate", ">=", "2014-06-01").and(new QueryFilter("Iteration.EndDate", "<=", "2014-06-08")));
and set workspace of the request:
taskRequest.setWorkspace("123456); //use your ObjectID
so that child iterations (from all projects in the workspace) with the same start and end dates are included in the results.
public static void main(String[] args) throws URISyntaxException, IOException {
String host = "https://rally1.rallydev.com";
String username = "user#co.com";
String password = "psw";
String workspaceRef = "/workspace/12352608129";
String applicationName = "ExampleFindTasks";
RallyRestApi restApi = null;
try{
restApi = new RallyRestApi(
new URI(host),
username,
password);
restApi.setApplicationName(applicationName);
QueryRequest taskRequest = new QueryRequest("Task");
taskRequest.setFetch(new Fetch("Name","FormattedID","Iteration","Project"));
taskRequest.setLimit(1000);
taskRequest.setScopedDown(true);
taskRequest.setScopedUp(false);
taskRequest.setWorkspace(workspaceRef);
taskRequest.setQueryFilter(
new QueryFilter("Iteration.StartDate", ">=", "2014-06-01").and(
new QueryFilter("Iteration.EndDate", "<=", "2014-06-08")));
QueryResponse taskQueryResponse = restApi.query(taskRequest);
int numberOfResults = taskQueryResponse.getTotalResultCount();
System.out.println(numberOfResults);
if(numberOfResults > 0){
for (int i=0;i<numberOfResults;i++){
JsonObject taskJsonObject = taskQueryResponse.getResults().get(i).getAsJsonObject();
System.out.println("Name: " + taskJsonObject.get("Name") + " " + "FormattedID: " +
taskJsonObject.get("FormattedID"));
JsonElement iteration = taskJsonObject.get("Iteration");
JsonElement project = taskJsonObject.get("Project");
try{
JsonObject iterationObject = iteration.getAsJsonObject().getAsJsonObject();
JsonObject projectObject = project.getAsJsonObject().getAsJsonObject();
System.out.println(iterationObject.get("Name"));
System.out.println(projectObject.get("Name"));
}
catch (java.lang.IllegalStateException ise) {
System.out.println("ise");
}
}
}
}
finally{
if (restApi != null) {
restApi.close();
}
}
}
i have a collection of raw text in a table in database, i need to replace some words in this collection using a set of words.
i put all the term to be replace and its substitutes in a text file as below
min=admin
lelet=lambat
lemot=lambat
nii=nih
ntu=itu
and so on.
i have successfully initiate a variabel of File and Scanner to read the collection of the term and its substitutes.
i loop all the dataset and save the raw text in a string
in the same loop
i loop all the term collection and save its row to a string name 'pattern', and split the pattern into two string named 'term' and 'replacer'
in this loop i initiate a new string which its value is the string from the dataset modified by replaceAll(term,replacer)
end loop for term collection
then i insert the new string to another table in database
end loop for dataset
i do it manualy as below
replaceAll("min","admin")
and its works but its really something to code it manually for almost 2000 terms to be replace it.
anyone ever face this kind of really something..
i really need a help now desperate :(
package sentimenrepo;
import javax.swing.*;
import java.sql.*;
import java.io.*;
//import java.util.HashMap;
import java.util.Scanner;
//import java.util.Map;
/**
*
* #author herman
*/
public class synonimReplaceV2 extends SwingWorker {
protected Object doInBackground() throws Exception {
new skripsisentimen.sentimenttwitter().setVisible(true);
Integer row = 0;
File synonimV2 = new File("synV2/catatan_kata_sinonim.txt");
String newTweet = "";
DB db = new DB();
Connection conn = db.dbConnect("jdbc:mysql://localhost:3306/tweet", "root", "");
try{
Statement select = conn.createStatement();
select.executeQuery("select * from synonimtweet");
ResultSet RS = select.getResultSet();
Scanner scSynV2 = new Scanner(synonimV2);
while(RS.next()){
row++;
String no = RS.getString("no");
String tweet = " "+ RS.getString("tweet");
String published = RS.getString("published");
String label = RS.getString("label");
clean2 cleanv2 = new clean2();
newTweet = cleanv2.cleanTweet(tweet);
try{
Statement insert = conn.createStatement();
insert.executeUpdate("INSERT INTO synonimtweet_v2(no,tweet,published,label) values('"
+no+"','"+newTweet+"','"+published+"','"+label+"')");
String current = skripsisentimen.sentimenttwitter.txtAreaResult.getText();
skripsisentimen.sentimenttwitter.txtAreaResult.setText(current+"\n"+row+"original : "+tweet+"\n"+newTweet+"\n______________________\n");
skripsisentimen.sentimenttwitter.lblStat.setText(row+" tweet read");
skripsisentimen.sentimenttwitter.txtAreaResult.setCaretPosition(skripsisentimen.sentimenttwitter.txtAreaResult.getText().length() - 1);
}catch(Exception e){
skripsisentimen.sentimenttwitter.lblStat.setText(e.getMessage());
}
skripsisentimen.sentimenttwitter.lblStat.setText(e.getMessage());
}
}catch(Exception e){
skripsisentimen.sentimenttwitter.lblStat.setText(e.getMessage());
}
return row;
}
class clean2{
public clean2(){}
public String cleanTweet(String tweet){
File synonimV2 = new File("synV2/catatan_kata_sinonim.txt");
String pattern = "";
String term = "";
String replacer = "";
String newTweet="";
try{
Scanner scSynV2 = new Scanner(synonimV2);
while(scSynV2.hasNext()){
pattern = scSynV2.next();
term = pattern.split("=")[0];
replacer = pattern.split("=")[1];
newTweet = tweet.replace(term, replacer);
}
}catch(Exception e){
e.printStackTrace();
}
System.out.println(newTweet+"\n"+tweet);
return newTweet;
}
}
}
update
ive just realize that the code actually works but only for the first row in database, the second row and so on stand still. here is i update the newest code i ve build
public class synonimReplaceV2 extends SwingWorker {
protected Object doInBackground() throws Exception {
new skripsisentimen.sentimenttwitter().setVisible(true);
Integer row = 0;
String newTweet = "";
DB db = new DB();
Connection conn = db.dbConnect("jdbc:mysql://localhost:3306/tweet", "root", "");
try{
Statement select = conn.createStatement();
select.executeQuery("select * from synonimtweet limit 2,10");
ResultSet RS = select.getResultSet();
FileReader readSyn = new FileReader("synV2/catatan_kata_sinonim.txt");
BufferedReader buffSyn = new BufferedReader(readSyn);
while(RS.next()){
row++;
String no = RS.getString("no");
String tweet = " "+ RS.getString("tweet");
String published = RS.getString("published");
String label = RS.getString("label");
String pattern = "";
while((pattern=buffSyn.readLine())!=null){
String patternTerm = pattern.split("=")[0];
String patternSubs = pattern.split("=")[1];
tweet = tweet.replaceAll("\\s"+patternTerm, patternSubs);
}
try{
Statement insert = conn.createStatement();
insert.executeUpdate("INSERT INTO synonimtweet_v2(no,tweet,published,label) values('"
+no+"','"+tweet+"','"+published+"','"+label+"')");
String current = skripsisentimen.sentimenttwitter.txtAreaResult.getText();
skripsisentimen.sentimenttwitter.txtAreaResult.setText(current+"\n"+row+"original : "+tweet+"\n"+newTweet+"\n______________________\n");
skripsisentimen.sentimenttwitter.lblStat.setText(row+" tweet read");
skripsisentimen.sentimenttwitter.txtAreaResult.setCaretPosition(skripsisentimen.sentimenttwitter.txtAreaResult.getText().length() - 1);
}catch(Exception e){
skripsisentimen.sentimenttwitter.lblStat.setText(e.getMessage());
}
}
}catch(Exception e){
skripsisentimen.sentimenttwitter.lblStat.setText(e.getMessage());
// System.out.println(e.getMessage());
}
Thread.sleep(100);
return row;
}
}
Opening the synonym file and iterating over 2,000 lines for every row in your ResultSet is a bit wasteful.
Load your synonyms into an in-memory Map once, keyed by unique misspelt term, then do a lookup on the map for every row in your result set, and replace as necessary.
Let us use both solutions to build a single solution for you:
First, you create a HashMap with all your keys:
public static HashMap<String, String> getMap() {
//your version would read from the file
HashMap<String,String> myMap=new HashMap<String,String>();
myMap.put("min", "admin");
myMap.put("lelet", "lambat");
myMap.put("lemot", "lambat");
myMap.put("nii", "nih");
myMap.put("ntu", "itu");
return(myMap);
}
Second, you create a pattern that contains all the keys in your hashmap:
public static String getPattern(HashMap<String,String> mapReplacement) {
String pattern="";
for (String s : mapReplacement.keySet()) {
if (!pattern.isEmpty()) {
pattern=pattern+"|";
}
pattern=pattern+s;
}
return(pattern);
}
Next, you can create a cleanTweet method that uses both structures you created:
public static String cleanTweet(String tweet, Pattern pattern,HashMap<String, String> myMap) {
String newTweet=tweet;
Matcher matcher = pattern.matcher(newTweet);
int start=0;
while (matcher.find()) {
String key=matcher.group();
String replacement=myMap.get(key);
if (replacement!=null) {
newTweet=newTweet.replace(key, replacement );
}
}
return(newTweet);
}
This might require some tweaking to perfect (I onyl tested a few cases), but the point is that you are going to iterate a single time in your keys and then iterate only on your tweets.
I hope it helps.
I didn't try, but it seems to me that you've almost got it - just replace this line:
newTweet = tweet.replace(term, replacer);
with this:
tweet = tweet.replaceAll(term, replacer);
As you're not using newTweet any more, return tweet:
return tweet;
You should also delete the newTweet declaration.
Also, you shouldn't read Scanner to read lines. Use FileReader instead.
thanks folks
i ve found the answer why the code is not working,
the txt file containing terms and its substitutes should be initiated each time the program read a row from database.
the code would be like this
public class synonimReplaceV2 extends SwingWorker {
protected Object doInBackground() throws Exception {
new skripsisentimen.sentimenttwitter().setVisible(true);
Integer row = 0;
String newTweet = "";
DB db = new DB();
Connection conn = db.dbConnect("jdbc:mysql://localhost:3306/tweet", "root", "");
try{
Statement select = conn.createStatement();
select.executeQuery("select * from synonimtweet limit 2,10");
ResultSet RS = select.getResultSet();
while(RS.next()){
row++;
FileReader readSyn = new FileReader("synV2/catatan_kata_sinonim.txt");
BufferedReader buffSyn = new BufferedReader(readSyn);
String no = RS.getString("no");
String tweet = " "+ RS.getString("tweet");
String published = RS.getString("published");
String label = RS.getString("label");
String pattern = "";
while((pattern=buffSyn.readLine())!=null){
String patternTerm = pattern.split("=")[0];
String patternSubs = pattern.split("=")[1];
tweet = tweet.replaceAll("\\s"+patternTerm, patternSubs);
}
try{
Statement insert = conn.createStatement();
insert.executeUpdate("INSERT INTO synonimtweet_v2(no,tweet,published,label) values('"
+no+"','"+tweet+"','"+published+"','"+label+"')");
String current = skripsisentimen.sentimenttwitter.txtAreaResult.getText();
skripsisentimen.sentimenttwitter.txtAreaResult.setText(current+"\n"+row+"original : "+tweet+"\n"+newTweet+"\n______________________\n");
skripsisentimen.sentimenttwitter.lblStat.setText(row+" tweet read");
skripsisentimen.sentimenttwitter.txtAreaResult.setCaretPosition(skripsisentimen.sentimenttwitter.txtAreaResult.getText().length() - 1);
}catch(Exception e){
skripsisentimen.sentimenttwitter.lblStat.setText(e.getMessage());
}
}
}catch(Exception e){
skripsisentimen.sentimenttwitter.lblStat.setText(e.getMessage());
// System.out.println(e.getMessage());
}
Thread.sleep(100);
return row;
}
}
but im actually want to apply the code in which rlinden made above, but cant figure it out how to call the cleanTweet function.