onCreate()
code starting the TimerTask
:
onCreate()代码启动TimerTask:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Log.e(LOG_TAG, "Start Repeat Timer");
TimerTask task = new RepeatingTask();
Timer timer = new Timer();
timer.scheduleAtFixedRate(task, 0, 3000);
Log.e(LOG_TAG, "Started Repeat Timer");
}
Timer Task Code:
计时器任务代码:
public class RepeatingTask extends TimerTask {
//private int len = 0;
//private byte[] input = new byte[len];
public RepeatingTask() {
Log.e(LOG_TAG, "In RepeatingTask()");
Log.e(LOG_TAG, "Before inputJSON String");
String hello = "hello world";
//String inputJSON = getStringFromBuffer(new InputStreamReader(socket.getInputStream()));
try {
inputJSON = ConvertByteArrayToString(readBytes(inputStr));
sendBytes(ConvertStringToByteArray(inputJSON), 0, ConvertStringToByteArray(inputJSON).length);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//Convert
Log.e(LOG_TAG, "After inputJSON String:" + inputJSON);
//LOOK HERE FIRST
//inputJSON is what is received back from the server - Take the inputJSON
//String and use regular expressions HERE to remove all the other characters in the
//string except the payload JSON.
//refreshViewModels(inputJSON);
}
@Override
public void run() {
/*try {
Log.e(LOG_TAG, "IN REPEATINGTHREAD-INPUTJSON");
//outputstrwr.write(outputJSONserv); //UNCOMMENT IF NEED TO SEND DATA TO GET JSON BACK
//inputJSON = ConvertByteArrayToString(getBytesFromInputStream(inputStr));
inputJSON = ConvertByteArrayToString(getFileBytes(inputStr));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Log.e(LOG_TAG, "IN REPEATINGTHREAD-INPUTJSON2:" + inputJSON);
refreshViewModels(inputJSON);*/
try {
Log.e(LOG_TAG, "IN REPEATINGTHREAD-INPUTJSON");
//outputstrwr.write(outputJSONserv); //UNCOMMENT IF NEED TO SEND DATA TO GET JSON BACK
//byte[] = myByteArray = readBytes(inputStr);
sendBytes(ConvertStringToByteArray(outputJSONserv), 0, ConvertStringToByteArray(outputJSONserv).length);
//sendBytes(myByteArray, 0, myByteArray.length);
Log.e(LOG_TAG, "AFTER SENDING DATA");
//inputJSON = ConvertByteArrayToString(getBytesFromInputStream(inputStr));
inputJSON = ConvertByteArrayToString(readBytes(inputStr));
Log.e(LOG_TAG, "IN REPEATINGTHREAD-INPUTJSON2:" + inputJSON);
}
catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Log.e(LOG_TAG, "IN REPEATINGTHREAD-INPUTJSON3:" + inputJSON);
refreshViewModels(inputJSON);
}
}
inputStr is an InputStream
used to read the bytes and convert them into a string.
inputStr是一个InputStream,用于读取字节并将其转换为字符串。
ConvertByteArrayToString()
just converts the byte array to String
& ConvertStringToByteArray()
converts a String
into a Byte[]
so that the data can be sent using sendBytes(byte[], int, int)
.
ConvertByteArrayToString()只是将字节数组转换为String&ConvertStringToByteArray()将String转换为Byte [],以便可以使用sendBytes(byte [],int,int)发送数据。
I'm trying to figure out why my code gets stuck in this try/catch statement without any exceptions being thrown:
我试图弄清楚为什么我的代码卡在这个try / catch语句中而没有抛出任何异常:
try {
inputJSON = ConvertByteArrayToString(readBytes(inputStr));
sendBytes(ConvertStringToByteArray(inputJSON), 0, ConvertStringToByteArray(inputJSON).length);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Here is my sendBytes()
method:
这是我的sendBytes()方法:
public void sendBytes(byte[] myByteArray, int start, int len) throws IOException {
if (len < 0)
throw new IllegalArgumentException("Negative length not allowed");
if (start < 0 || start >= myByteArray.length)
throw new IndexOutOfBoundsException("Out of bounds: " + start);
// Other checks if needed.
// May be better to save the streams in the support class;
// just like the socket variable.
OutputStream out = socket.getOutputStream();
DataOutputStream dos = new DataOutputStream(out);
dos.writeInt(len);
if (len > 0) {
dos.write(myByteArray, start, len);
}
}
Here is the getPayloadStr()
method:
这是getPayloadStr()方法:
public String getPayloadStr(String profileString) {
Log.e("LOG_TAG", "Profile Str:"+profileString);
Pattern pattern = Pattern.compile(".*?payload\":(.*)\\}");
Log.e("LOG_TAG", "I got here 1");
Matcher matcher = pattern.matcher(profileString);
Log.e("LOG_TAG", "I got here 12");
//Matcher m = responseCodePattern.matcher(firstHeader);
matcher.matches();
matcher.groupCount();
//matcher.group(0);
Log.e("LOG_TAG", "I got here 2"+matcher.group(1));
return matcher.group(1);
}
Here is my readBytes()
method:
这是我的readBytes()方法:
public byte[] readBytes(InputStream in) throws IOException {
// Again, probably better to store these objects references in the support class
in = socket.getInputStream();
DataInputStream dis = new DataInputStream(in);
int len = dis.readInt();
byte[] data = new byte[len];
if (len > 0) {
dis.readFully(data);
}
return data;
}
Any help or pointers in the right direction would be appreciated. Any help via chat would be greatly appreciated too. Whole file could be posted or viewed in chat session if needed.
任何帮助或指向正确的方向将不胜感激。通过聊天提供的任何帮助也将非常感激。如果需要,可以在聊天会话中发布或查看整个文件。
1 个解决方案
#1
0
My idea it is :
我的想法是:
- leave it emprty the conrructor and set a breakpoint to run() -hopefully hitted, or you will see the IN REPEATINGTHREAD-INPUTJSON message in log. If not, than I don't know where is the problem, because the code it seems to be ok.
- if you see, than in the contructor is the problem( problem1 isolated)
将它置于conruructor并设置一个断点来运行()-hopefully hitted,或者你会在日志中看到IN REPEATINGTHREAD-INPUTJSON消息。如果不是,我不知道问题在哪里,因为代码似乎没问题。
如果你看到,比在构造函数中是问题(problem1被隔离)
I think here it is the problem:
我认为这是问题所在:
int len = dis.readInt();
int len = dis.readInt();
reading it is a blocking method, so your code in Even dispatcher thread or Main thread or Ui thread, - forgot what name is that on that platform- is waiting probably, until he can read 4 bytes.
读取它是一种阻塞方法,所以你的代码在Even调度程序线程或主线程或Ui线程中, - 忘记该平台上的名称 - 可能正在等待,直到他可以读取4个字节。
Based on comment and code request here it is:
根据评论和代码请求,它是:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Log.e(LOG_TAG, "Start Repeat Timer");
Thread thInitializer = new Thread() {
@Override
public void run() {
TimerTask task = new RepeatingTask(); // here will block until is readed from socket, but will not block the UI
//after read is done you can set breakpoint to next statement, but it will repet the run method of the RepeatingTask in each 3 sec
Timer timer = new Timer();
timer.scheduleAtFixedRate(task, 0, 3000);
Log.e(LOG_TAG, "Started Repeat Timer");
}
};
thInitializer.start();
// UI initialization is done, background thread is running an trying to initialize the network stuff
}
#1
0
My idea it is :
我的想法是:
- leave it emprty the conrructor and set a breakpoint to run() -hopefully hitted, or you will see the IN REPEATINGTHREAD-INPUTJSON message in log. If not, than I don't know where is the problem, because the code it seems to be ok.
- if you see, than in the contructor is the problem( problem1 isolated)
将它置于conruructor并设置一个断点来运行()-hopefully hitted,或者你会在日志中看到IN REPEATINGTHREAD-INPUTJSON消息。如果不是,我不知道问题在哪里,因为代码似乎没问题。
如果你看到,比在构造函数中是问题(problem1被隔离)
I think here it is the problem:
我认为这是问题所在:
int len = dis.readInt();
int len = dis.readInt();
reading it is a blocking method, so your code in Even dispatcher thread or Main thread or Ui thread, - forgot what name is that on that platform- is waiting probably, until he can read 4 bytes.
读取它是一种阻塞方法,所以你的代码在Even调度程序线程或主线程或Ui线程中, - 忘记该平台上的名称 - 可能正在等待,直到他可以读取4个字节。
Based on comment and code request here it is:
根据评论和代码请求,它是:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Log.e(LOG_TAG, "Start Repeat Timer");
Thread thInitializer = new Thread() {
@Override
public void run() {
TimerTask task = new RepeatingTask(); // here will block until is readed from socket, but will not block the UI
//after read is done you can set breakpoint to next statement, but it will repet the run method of the RepeatingTask in each 3 sec
Timer timer = new Timer();
timer.scheduleAtFixedRate(task, 0, 3000);
Log.e(LOG_TAG, "Started Repeat Timer");
}
};
thInitializer.start();
// UI initialization is done, background thread is running an trying to initialize the network stuff
}