My OutputStreamWrite refuses to append to my file. It only overwrites the first line constantly.
The String lightRowValues are sent from another method that goes through a table and gets one row at a time, sends that row data here, where that row is written to this file. Then the method loops back and gets the next row. SO I should have a list of rows but instead only have one line of the very last entry.
public static void appendToLtCSV(String lightRowValues, String CSVFinalFileName){
try {
csvfos = new FileOutputStream(CSVFinalFileName, true);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
OutputStreamWriter sensorCSVWriter = new OutputStreamWriter(csvfos);
try {
sensorCSVWriter.append(lightRowValues);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
sensorCSVWriter.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
I have set the append flag to true and still the same problem....
Related
I am creating a game where in the client I am supposed to get some arrays from a server and reveal them into the screen. I can press the button X times to get X arrays.
Currently I am using this code
rollDiceButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
rollDice();
try {
int[] tempArray = new GetDiceTask().execute(socket).get();
printDice(tempArray,pDice);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ExecutionException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
but if I don't like this array and I want another one I cannot use the AsyncTask cause I get the common error for Cannot execute task: the task has already been executed (a task can be executed only once).
This is my code for AsyncTask.
#Override
protected int[] doInBackground(Socket...params) {
Socket soc = params[0];
try {
ObjectInputStream ois = new ObjectInputStream(soc.getInputStream());
int[] tempArray = (int[]) (ois.readObject());
return tempArray;
} catch (IOException | ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
Is there a way that I can take multiple arrays without that exception?
Error:
android.security.KeyChainException: java.lang.IllegalStateException: uid 10111 doesn't have permission to access the requested alias
Code:
new Thread(new Runnable() {
public void run() {
try {
X509Certificate[] myCertificates=KeyChain.getCertificateChain(MainActivity.this, "ServerCertificate");
if(myCertificates!=null)
{
System.out.println("myCertificates size "+myCertificates.length);
for(int i=0;i<myCertificates.length;i++)
{
System.out.println("myCertificates i= "+i+" "+myCertificates[i]);
}
}
} catch (KeyChainException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}).start();
The KeyChain class requires the application to call choosePrivateKeyAlias() at least once after the application is installed before calling getPrivateKey() or getCertificateChain(). So even if one knows the alias beforehand, the choosePrivateKeyAlias() must be called at least once, otherwise there is no trust established between the app and the internal database that KeyChain uses.
I need to show in a listview content of a local xml file, which contains item and subitem, can anyone cite an example of how to do? I found examples that take a xml file from a URL, but want to do it locally. Thanks!
try it .it will help you.
private void xmlPullParser()
{
XmlPullParser xpp = getResources().getXml(R.xml.words);
try {
while (xpp.getEventType()!=XmlPullParser.END_DOCUMENT)
{
if (xpp.getEventType()==XmlPullParser.START_TAG)
{
if (xpp.getName().equals("word"))
items.add(xpp.getAttributeValue(0));
}
xpp.next();
}
} catch (XmlPullParserException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
make folder at res like
-
I'm using a custom adapter and wrapping it around the cwac-endless adapter. The problem is that the wrapping condition is being ignored and the method inside the cacheInBackground() is being called infinitely. I'm attaching the concerned code.Please suggest me a solution for this. Thank you.
#Override
protected boolean cacheInBackground() {
SystemClock.sleep(100); // pretend to do work
try {
msg=getMsgs();
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ParserConfigurationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SAXException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Log.d("count", " "+ getWrappedAdapter().getCount());
return(getWrappedAdapter().getCount()<100);
}
#Override
protected void appendCachedData() {
if (getWrappedAdapter().getCount()<100) {
#SuppressWarnings("unchecked")
MsgAdapter a=(MsgAdapter)getWrappedAdapter();
for(String s:msg)
{
Log.d("msg", s);
}
}
}
}
I fixed the error it had to do with the logic of my getCount() in my custom adapter. Fixing it made the code work perfectly.
My original goal is to build my own modal dialog. At some point, I have to run an inner loop, which would do really close to what GetMessage/PostMessage do in Win32, if you got Win32 experience then you are quite familiar with this. The inner loop will block current workflow but still process events. The pseudo code would be like,
private void doModal() {
doSth();
// start loop and process events
while (!isQuit) {
Message msg = nextMessage();
// process all wanted msgs, and simply discard all unexpected msgs
if (isWantedMsg) {
sendToTarget(msg);
}
}
}
I've looked into source code, Looper.loop(), which was,
public static final void loop() {
Looper me = myLooper();
MessageQueue queue = me.mQueue;
while (true) {
Message msg = queue.next(); // might block
if (msg != null) {
if (msg.target == null) {
// No target is a magic identifier for the quit message.
return;
}
msg.target.dispatchMessage(msg);
msg.recycle();
}
}
}
Basically I'd like to write such a loop, then I'm able to receive all msgs and process or drop them accordingly. Unfortunately, MessageQueue belongs to package android.os, I have no privilege to access most of its interfaces. Activity.dispatchTouchEvent is just a handler, not my case.
How could I do? Thanks.
==========================SOLUTION=====================================
I solved it by reflection, I exactly copied source of Looper.loop(), see below,
private void startModal() {
Class clsMsgQueue = null;
Method nextMethod = null;
Class clsMsg = null;
mQuitModal = false;
MessageQueue queue = Looper.myQueue();
clsMsgQueue = queue.getClass();
try {
nextMethod = clsMsgQueue.getDeclaredMethod("next", new Class[]{});
} catch (SecurityException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (NoSuchMethodException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
nextMethod.setAccessible(true);
while (!mQuitModal) {
Message msg = null;
try {
msg = (Message)nextMethod.invoke(queue, new Object[]{});
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InvocationTargetException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if (msg != null) {
clsMsg = msg.getClass();
Field targetFiled = null;
try {
targetFiled = clsMsg.getDeclaredField("target");
} catch (SecurityException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (NoSuchFieldException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
targetFiled.setAccessible(true);
Handler target = null;
try {
target = (Handler) targetFiled.get(msg);
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if (target == null) {
// No target is a magic identifier for the quit message.
mQuitModal = true;
}
target.dispatchMessage(msg);
msg.recycle();
}
}
}
When dialog was dismissed, mQuitModal was set to true either.
If don't care much about performance issue, it worked.
Sorry, Android deliberately does not support nested event loops like this. You just will have to structure your code a different way -- for dialogs you will typically start the dialog, return to the event loop, and implement callbacks to handle the result from it.