First of all, sorry my poor english, please. I m trying to write a function, that sends an information about whatching video to the server. When server is unreachable, it writes it to the file and sends it later. So, i m sending file name to my function, loading file with unsended names, makeing an array of names and sending all of them one by one. If any error appear, writing them back to file.
问题在于,当我开始保存档案时,发现的首个象征就消失了。 axaclty:在书面或读物上。
我的法典 (其中包括一些非驻地行动。) 用于测试:
void sendLog (String name) {
String FILENAME = "Log";
String strToSave = "";
String logString = null;
//opening file, just to see what it cantains:
FileInputStream fis;
try {
fis = openFileInput(FILENAME);
fis.read();
ByteArrayOutputStream content = new ByteArrayOutputStream();
int readBytes = 0;
byte[] sBuffer = new byte[512];
while ((readBytes = fis.read(sBuffer)) != -1) {
content.write(sBuffer, 0, readBytes);
}
logString = new String(content.toByteArray());
fis.close();
Log.d ("Log", "Loaded log: "+logString+" ");
} catch (IOException e) {
e.printStackTrace();
}
//saving empty file.
FileOutputStream fos;
try {
Log.d ("Log", "Saving log: "+strToSave+" ");
fos = openFileOutput(FILENAME, Context.MODE_PRIVATE);
fos.write(strToSave.getBytes());
fos.close();
} catch (IOException e) {
Log.d ("File WRITE", "can t write Log file");
e.printStackTrace();
}
//opening it agane, allready empty.
try {
fis = openFileInput(FILENAME);
fis.read();
ByteArrayOutputStream content = new ByteArrayOutputStream();
int readBytes = 0;
byte[] sBuffer = new byte[512];
while ((readBytes = fis.read(sBuffer)) != -1) {
content.write(sBuffer, 0, readBytes);
}
logString = new String(content.toByteArray());
fis.close();
Log.d ("Log", "Loaded log: "+logString+" ");
} catch (IOException e) {
e.printStackTrace();
}
int cnt;
//splitting loaded string to array
if (logString.length()>1) {
logString = logString+name+",";
Log.d ("Log", "spliting log. length: "+logString.length());
log = logString.split(",");
long length = logString.length();
long length2 = 0;
cnt = 0;
//sorry for this code. Is there a better solution to know, how many strings in array?
for (int i = 0; length2<length; i++) {
length2 += playList[i].length()+1;
cnt++;
}
}
else {
log = new String[1];
log[0] = name+",";
cnt = 1;
}
//sending them one by one
for (int i = 0; i<cnt; i++) { //playList[i].substring(2, playList[i].length()-1)
Log.d ("Log", "Sending log: "+log[i].substring(0, log[i].length()-1) + " ");
HttpClient client = new DefaultHttpClient();
HttpGet request = new HttpGet("server link here"
+log[i].substring(0, log[i].length()-1)); //need to exclude ","
try {
HttpResponse response = client.execute(request);
} catch (ClientProtocolException e) {
//adding unsended name to string if any errors
strToSave = strToSave+log[i];
e.printStackTrace();
} catch (IOException e) {
strToSave = strToSave+log[i];
e.printStackTrace();
}
}
//here should be empty string, but for testing i m adding some:
if (strToSave.length() == 0) strToSave = strToSave+log[0];//strToSave = " ";
//saving it to file
try {
Log.d ("Log", "Saving log: "+strToSave+" ");
fos = openFileOutput(FILENAME, Context.MODE_PRIVATE);
fos.write(strToSave.getBytes());
fos.close();
} catch (IOException e) {
Log.d ("File WRITE", "can t write Log file");
e.printStackTrace();
}
}
因此,这正是DDMS中的标志:
10-31 09:23:17.446: DEBUG/Log(2571): Loaded log: UB_MENUS,
10-31 09:23:17.446: DEBUG/Log(2571): Saving log:
10-31 09:23:17.446: DEBUG/Log(2571): Loaded log:
10-31 09:23:17.446: DEBUG/Log(2571): Sending log: UPLOAD_SCREEN
10-31 09:23:17.746: DEBUG/Log(2571): Saving log: UPLOAD_SCREEN,
10-31 09:23:39.386: DEBUG/Log(2571): Loaded log: PLOAD_SCREEN,
10-31 09:23:39.386: DEBUG/Log(2571): Saving log:
10-31 09:23:39.386: DEBUG/Log(2571): Loaded log:
10-31 09:23:39.386: DEBUG/Log(2571): Sending log: APP_BUTTON
10-31 09:23:39.646: DEBUG/Log(2571): Saving log: APP_BUTTON,
......
等等。