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");
}
タイマータスクコード:
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はInputStream
、バイトを読み取り、それらを文字列に変換するために使用されます。
ConvertByteArrayToString()
を使用してデータを送信できるように、バイト配列をに変換し、をに変換するだけString
です。ConvertStringToByteArray()
String
Byte[]
sendBytes(byte[], int, int)
例外がスローされることなく、コードがこのtry/catchステートメントでスタックする理由を理解しようとしています。
try {
inputJSON = ConvertByteArrayToString(readBytes(inputStr));
sendBytes(ConvertStringToByteArray(inputJSON), 0, ConvertStringToByteArray(inputJSON).length);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
これが私の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);
}
}
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);
}
これが私の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;
}
正しい方向へのヘルプやポインタをいただければ幸いです。チャットによる助けも大歓迎です。必要に応じて、ファイル全体をチャットセッションで投稿または表示できます。