1

InputStream 和 OutputStream 声明:

public OutputStream out = null; 
public InputStream inputStr = null;

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");
    out = socket.getOutputStream();
    inputStr = socket.getInputStream();
}

定时器任务代码:

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() 只是将 Byte Array 转换为 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);
    }
}

这是我的 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;
}

下面是 ConvertByteArrayToString() 和 ConvertStringToByteArray() 方法的代码:

public String ConvertByteArrayToString(byte[] b) { 
    // byte[] to string
    String input = new String(b);
    return input;
}

public byte[] ConvertStringToByteArray(String str) { 
    // string to byte[]
    byte[] bytes = str.getBytes();
    return bytes;
}

这是我看到的 logcat 输出:

 E/AndroidClient(17109): Start Repeat Timer
 E/AndroidClient(17109): Before inputJSON String
 D/WindowManager(  110): Home Key Test: false :false

任何正确方向的帮助或指示将不胜感激。

4

1 回答 1

0

I hope this helps, I have your code like this and seems to be working,

package com.example.stackoverflow;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.util.Timer;
import java.util.TimerTask;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;

public class MainActivity extends Activity {
   private static final String LOG_TAG = "TEMP";
   InputStream inputStr;
   String inputJSON = null;
   Socket socket = new Socket();
   String outputJSONserv = "asdasd";

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_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");
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.activity_main, menu);
    return true;
}

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");

        try {
            inputJSON = ConvertByteArrayToString(readBytes(inputStr));
            sendBytes(ConvertStringToByteArray(inputJSON), 0,
                    ConvertStringToByteArray(inputJSON).length);
        } catch (IOException e) {
            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);
    }
}

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);
    }
}

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;
}

public String ConvertByteArrayToString(byte[] b) {
    // byte[] to string
    String input = new String(b);
    return input;
}

public byte[] ConvertStringToByteArray(String str) {
    // string to byte[]
    byte[] bytes = str.getBytes();
    return bytes;
}

}

and here is the logcat output:

08-27 14:26:15.515: E/TEMP(23005): Start Repeat Timer 08-27 14:26:15.515: E/TEMP(23005): In RepeatingTask() 08-27 14:26:15.515: E/TEMP(23005): Before inputJSON String 08-27 14:26:15.523: W/System.err(23005): java.net.SocketException: Socket is not connected 08-27 14:26:15.523: W/System.err(23005): at java.net.Socket.checkOpenAndCreate(Socket.java:645) 08-27 14:26:15.523: W/System.err(23005): at java.net.Socket.getInputStream(Socket.java:339) 08-27 14:26:15.523: W/System.err(23005): at com.example.stackoverflow.MainActivity.readBytes(MainActivity.java:129) 08-27 14:26:15.523: W/System.err(23005): at com.example.stackoverflow.MainActivity$RepeatingTask.(MainActivity.java:50) 08-27 14:26:15.523: W/System.err(23005): at com.example.stackoverflow.MainActivity.onCreate(MainActivity.java:29) 08-27 14:26:15.523: W/System.err(23005): at android.app.Activity.performCreate(Activity.java:4465) 08-27 14:26:15.523: W/System.err(23005): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049) 08-27 14:26:15.523: W/System.err(23005): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920) 08-27 14:26:15.523: W/System.err(23005): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981) 08-27 14:26:15.523: W/System.err(23005): at android.app.ActivityThread.access$600(ActivityThread.java:123) 08-27 14:26:15.523: W/System.err(23005): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147) 08-27 14:26:15.523: W/System.err(23005): at android.os.Handler.dispatchMessage(Handler.java:99) 08-27 14:26:15.523: W/System.err(23005): at android.os.Looper.loop(Looper.java:137) 08-27 14:26:15.523: W/System.err(23005): at android.app.ActivityThread.main(ActivityThread.java:4424) 08-27 14:26:15.523: W/System.err(23005): at java.lang.reflect.Method.invokeNative(Native Method) 08-27 14:26:15.523: W/System.err(23005): at java.lang.reflect.Method.invoke(Method.java:511) 08-27 14:26:15.523: W/System.err(23005): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784) 08-27 14:26:15.523: W/System.err(23005): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551) 08-27 14:26:15.523: W/System.err(23005): at dalvik.system.NativeStart.main(Native Method) 08-27 14:26:15.523: E/TEMP(23005): After inputJSON String:null 08-27 14:26:15.523: E/TEMP(23005): Started Repeat Timer 08-27 14:26:15.531: E/TEMP(23005): IN REPEATINGTHREAD-INPUTJSON 08-27 14:26:15.531: W/System.err(23005): java.net.SocketException: Socket is not connected 08-27 14:26:15.531: W/System.err(23005): at java.net.Socket.checkOpenAndCreate(Socket.java:645) 08-27 14:26:15.531: W/System.err(23005): at java.net.Socket.getOutputStream(Socket.java:381) 08-27 14:26:15.531: W/System.err(23005): at com.example.stackoverflow.MainActivity.sendBytes(MainActivity.java:117) 08-27 14:26:15.531: W/System.err(23005): at com.example.stackoverflow.MainActivity$RepeatingTask.run(MainActivity.java:88) 08-27 14:26:15.531: W/System.err(23005): at java.util.Timer$TimerImpl.run(Timer.java:284) 08-27 14:26:15.531: E/TEMP(23005): IN REPEATINGTHREAD-INPUTJSON3:null 08-27 14:26:15.578: D/libEGL(23005): loaded /system/lib/egl/libGLES_android.so 08-27 14:26:15.578: D/libEGL(23005): loaded /vendor/lib/egl/libEGL_POWERVR_SGX540_120.so 08-27 14:26:15.578: D/libEGL(23005): loaded /vendor/lib/egl/libGLESv1_CM_POWERVR_SGX540_120.so 08-27 14:26:15.585: D/libEGL(23005): loaded /vendor/lib/egl/libGLESv2_POWERVR_SGX540_120.so 08-27 14:26:15.609: D/OpenGLRenderer(408): Flushing caches (mode 1) 08-27 14:26:15.632: D/OpenGLRenderer(408): Flushing caches (mode 0) 08-27 14:26:15.687: D/OpenGLRenderer(23005): Enabling debug mode 0 08-27 14:26:15.914: I/ActivityManager(179): Displayed com.example.stackoverflow/.MainActivity: +498ms 08-27 14:26:18.531: E/TEMP(23005): IN REPEATINGTHREAD-INPUTJSON 08-27 14:26:18.531: W/System.err(23005): java.net.SocketException: Socket is not connected 08-27 14:26:18.531: W/System.err(23005): at java.net.Socket.checkOpenAndCreate(Socket.java:645) 08-27 14:26:18.531: W/System.err(23005): at java.net.Socket.getOutputStream(Socket.java:381) 08-27 14:26:18.531: W/System.err(23005): at com.example.stackoverflow.MainActivity.sendBytes(MainActivity.java:117) 08-27 14:26:18.531: W/System.err(23005): at com.example.stackoverflow.MainActivity$RepeatingTask.run(MainActivity.java:88) 08-27 14:26:18.531: W/System.err(23005): at java.util.Timer$TimerImpl.run(Timer.java:284) 08-27 14:26:18.531: E/TEMP(23005): IN REPEATINGTHREAD-INPUTJSON3:null 08-27 14:26:21.523: E/TEMP(23005): IN REPEATINGTHREAD-INPUTJSON 08-27 14:26:21.523: W/System.err(23005): java.net.SocketException: Socket is not connected 08-27 14:26:21.531: W/System.err(23005): at java.net.Socket.checkOpenAndCreate(Socket.java:645) 08-27 14:26:21.531: W/System.err(23005): at java.net.Socket.getOutputStream(Socket.java:381) 08-27 14:26:21.531: W/System.err(23005): at com.example.stackoverflow.MainActivity.sendBytes(MainActivity.java:117) 08-27 14:26:21.531: W/System.err(23005): at com.example.stackoverflow.MainActivity$RepeatingTask.run(MainActivity.java:88) 08-27 14:26:21.531: W/System.err(23005): at java.util.Timer$TimerImpl.run(Timer.java:284) 08-27 14:26:21.531: E/TEMP(23005): IN REPEATINGTHREAD-INPUTJSON3:null 08-27 14:26:23.781: I/ActivityManager(179): No longer want com.android.keychain (pid 22891): hidden #16 08-27 14:26:24.531: E/TEMP(23005): IN REPEATINGTHREAD-INPUTJSON 08-27 14:26:24.531: W/System.err(23005): java.net.SocketException: Socket is not connected 08-27 14:26:24.531: W/System.err(23005): at java.net.Socket.checkOpenAndCreate(Socket.java:645) 08-27 14:26:24.531: W/System.err(23005): at java.net.Socket.getOutputStream(Socket.java:381) 08-27 14:26:24.531: W/System.err(23005): at com.example.stackoverflow.MainActivity.sendBytes(MainActivity.java:117) 08-27 14:26:24.531: W/System.err(23005): at com.example.stackoverflow.MainActivity$RepeatingTask.run(MainActivity.java:88) 08-27 14:26:24.531: W/System.err(23005): at java.util.Timer$TimerImpl.run(Timer.java:284) 08-27 14:26:24.531: E/TEMP(23005): IN REPEATINGTHREAD-INPUTJSON3:null 08-27 14:26:27.531: E/TEMP(23005): IN REPEATINGTHREAD-INPUTJSON 08-27 14:26:27.531: W/System.err(23005): java.net.SocketException: Socket is not connected 08-27 14:26:27.531: W/System.err(23005): at java.net.Socket.checkOpenAndCreate(Socket.java:645) 08-27 14:26:27.531: W/System.err(23005): at java.net.Socket.getOutputStream(Socket.java:381) 08-27 14:26:27.531: W/System.err(23005): at com.example.stackoverflow.MainActivity.sendBytes(MainActivity.java:117) 08-27 14:26:27.531: W/System.err(23005): at com.example.stackoverflow.MainActivity$RepeatingTask.run(MainActivity.java:88) 08-27 14:26:27.531: W/System.err(23005): at java.util.Timer$TimerImpl.run(Timer.java:284) 08-27 14:26:27.531: E/TEMP(23005): IN REPEATINGTHREAD-INPUTJSON3:null 08-27 14:26:29.828: D/dalvikvm(19877): GC_CONCURRENT freed 470K, 5% free 13211K/13767K, paused 4ms+3ms 08-27 14:26:30.179: D/Finsky(19877): [1] 5.onFinished: Installation state replication succeeded. 08-27 14:26:30.531: E/TEMP(23005): IN REPEATINGTHREAD-INPUTJSON 08-27 14:26:30.531: W/System.err(23005): java.net.SocketException: Socket is not connected 08-27 14:26:30.531: W/System.err(23005): at java.net.Socket.checkOpenAndCreate(Socket.java:645) 08-27 14:26:30.531: W/System.err(23005): at java.net.Socket.getOutputStream(Socket.java:381) 08-27 14:26:30.531: W/System.err(23005): at com.example.stackoverflow.MainActivity.sendBytes(MainActivity.java:117) 08-27 14:26:30.531: W/System.err(23005): at com.example.stackoverflow.MainActivity$RepeatingTask.run(MainActivity.java:88) 08-27 14:26:30.531: W/System.err(23005): at java.util.Timer$TimerImpl.run(Timer.java:284) 08-27 14:26:30.531: E/TEMP(23005): IN REPEATINGTHREAD-INPUTJSON3:null 08-27 14:26:33.523: E/TEMP(23005): IN REPEATINGTHREAD-INPUTJSON 08-27 14:26:33.523: W/System.err(23005): java.net.SocketException: Socket is not connected 08-27 14:26:33.523: W/System.err(23005): at java.net.Socket.checkOpenAndCreate(Socket.java:645) 08-27 14:26:33.523: W/System.err(23005): at java.net.Socket.getOutputStream(Socket.java:381) 08-27 14:26:33.523: W/System.err(23005): at com.example.stackoverflow.MainActivity.sendBytes(MainActivity.java:117) 08-27 14:26:33.531: W/System.err(23005): at com.example.stackoverflow.MainActivity$RepeatingTask.run(MainActivity.java:88) 08-27 14:26:33.531: W/System.err(23005): at java.util.Timer$TimerImpl.run(Timer.java:284) 08-27 14:26:33.531: E/TEMP(23005): IN REPEATINGTHREAD-INPUTJSON3:null 08-27 14:26:36.523: E/TEMP(23005): IN REPEATINGTHREAD-INPUTJSON 08-27 14:26:36.523: W/System.err(23005): java.net.SocketException: Socket is not connected 08-27 14:26:36.531: W/System.err(23005): at java.net.Socket.checkOpenAndCreate(Socket.java:645) 08-27 14:26:36.531: W/System.err(23005): at java.net.Socket.getOutputStream(Socket.java:381) 08-27 14:26:36.531: W/System.err(23005): at com.example.stackoverflow.MainActivity.sendBytes(MainActivity.java:117) 08-27 14:26:36.531: W/System.err(23005): at com.example.stackoverflow.MainActivity$RepeatingTask.run(MainActivity.java:88) 08-27 14:26:36.531: W/System.err(23005): at java.util.Timer$TimerImpl.run(Timer.java:284) 08-27 14:26:36.531: E/TEMP(23005): IN REPEATINGTHREAD-INPUTJSON3:null 08-27 14:26:39.523: E/TEMP(23005): IN REPEATINGTHREAD-INPUTJSON 08-27 14:26:39.523: W/System.err(23005): java.net.SocketException: Socket is not connected 08-27 14:26:39.531: W/System.err(23005): at java.net.Socket.checkOpenAndCreate(Socket.java:645) 08-27 14:26:39.531: W/System.err(23005): at java.net.Socket.getOutputStream(Socket.java:381) 08-27 14:26:39.531: W/System.err(23005): at com.example.stackoverflow.MainActivity.sendBytes(MainActivity.java:117) 08-27 14:26:39.531: W/System.err(23005): at com.example.stackoverflow.MainActivity$RepeatingTask.run(MainActivity.java:88) 08-27 14:26:39.531: W/System.err(23005): at java.util.Timer$TimerImpl.run(Timer.java:284) 08-27 14:26:39.531: E/TEMP(23005): IN REPEATINGTHREAD-INPUTJSON3:null 08-27 14:26:42.523: E/TEMP(23005): IN REPEATINGTHREAD-INPUTJSON 08-27 14:26:42.531: W/System.err(23005): java.net.SocketException: Socket is not connected 08-27 14:26:42.531: W/System.err(23005): at java.net.Socket.checkOpenAndCreate(Socket.java:645) 08-27 14:26:42.531: W/System.err(23005): at java.net.Socket.getOutputStream(Socket.java:381) 08-27 14:26:42.531: W/System.err(23005): at com.example.stackoverflow.MainActivity.sendBytes(MainActivity.java:117) 08-27 14:26:42.531: W/System.err(23005): at com.example.stackoverflow.MainActivity$RepeatingTask.run(MainActivity.java:88) 08-27 14:26:42.531: W/System.err(23005): at java.util.Timer$TimerImpl.run(Timer.java:284) 08-27 14:26:42.531: E/TEMP(23005): IN REPEATINGTHREAD-INPUTJSON3:null 08-27 14:26:45.523: E/TEMP(23005): IN REPEATINGTHREAD-INPUTJSON 08-27 14:26:45.531: W/System.err(23005): java.net.SocketException: Socket is not connected 08-27 14:26:45.531: W/System.err(23005): at java.net.Socket.checkOpenAndCreate(Socket.java:645) 08-27 14:26:45.531: W/System.err(23005): at java.net.Socket.getOutputStream(Socket.java:381) 08-27 14:26:45.531: W/System.err(23005): at com.example.stackoverflow.MainActivity.sendBytes(MainActivity.java:117) 08-27 14:26:45.531: W/System.err(23005): at com.example.stackoverflow.MainActivity$RepeatingTask.run(MainActivity.java:88) 08-27 14:26:45.531: W/System.err(23005): at java.util.Timer$TimerImpl.run(Timer.java:284) 08-27 14:26:45.531: E/TEMP(23005): IN REPEATINGTHREAD-INPUTJSON3:null 08-27 14:26:48.523: E/TEMP(23005): IN REPEATINGTHREAD-INPUTJSON 08-27 14:26:48.523: W/System.err(23005): java.net.SocketException: Socket is not connected 08-27 14:26:48.523: W/System.err(23005): at java.net.Socket.checkOpenAndCreate(Socket.java:645) 08-27 14:26:48.531: W/System.err(23005): at java.net.Socket.getOutputStream(Socket.java:381) 08-27 14:26:48.531: W/System.err(23005): at com.example.stackoverflow.MainActivity.sendBytes(MainActivity.java:117) 08-27 14:26:48.531: W/System.err(23005): at com.example.stackoverflow.MainActivity$RepeatingTask.run(MainActivity.java:88) 08-27 14:26:48.531: W/System.err(23005): at java.util.Timer$TimerImpl.run(Timer.java:284) 08-27 14:26:48.531: E/TEMP(23005): IN REPEATINGTHREAD-INPUTJSON3:null 08-27 14:26:51.117: D/dalvikvm(366): GC_CONCURRENT freed 443K, 5% free 13141K/13703K, paused 6ms+3ms 08-27 14:26:51.531: E/TEMP(23005): IN REPEATINGTHREAD-INPUTJSON 08-27 14:26:51.531: W/System.err(23005): java.net.SocketException: Socket is not connected 08-27 14:26:51.531: W/System.err(23005): at java.net.Socket.checkOpenAndCreate(Socket.java:645) 08-27 14:26:51.531: W/System.err(23005): at java.net.Socket.getOutputStream(Socket.java:381) 08-27 14:26:51.531: W/System.err(23005): at com.example.stackoverflow.MainActivity.sendBytes(MainActivity.java:117) 08-27 14:26:51.531: W/System.err(23005): at com.example.stackoverflow.MainActivity$RepeatingTask.run(MainActivity.java:88) 08-27 14:26:51.531: W/System.err(23005): at java.util.Timer$TimerImpl.run(Timer.java:284) 08-27 14:26:51.531: E/TEMP(23005): IN REPEATINGTHREAD-INPUTJSON3:null 08-27 14:26:54.523: E/TEMP(23005): IN REPEATINGTHREAD-INPUTJSON 08-27 14:26:54.523: W/System.err(23005): java.net.SocketException: Socket is not connected 08-27 14:26:54.531: W/System.err(23005): at java.net.Socket.checkOpenAndCreate(Socket.java:645) 08-27 14:26:54.531: W/System.err(23005): at java.net.Socket.getOutputStream(Socket.java:381) 08-27 14:26:54.531: W/System.err(23005): at com.example.stackoverflow.MainActivity.sendBytes(MainActivity.java:117) 08-27 14:26:54.531: W/System.err(23005): at com.example.stackoverflow.MainActivity$RepeatingTask.run(MainActivity.java:88) 08-27 14:26:54.531: W/System.err(23005): at java.util.Timer$TimerImpl.run(Timer.java:284) 08-27 14:26:54.531: E/TEMP(23005): IN REPEATINGTHREAD-INPUTJSON3:null 08-27 14:26:57.531: E/TEMP(23005): IN REPEATINGTHREAD-INPUTJSON 08-27 14:26:57.531: W/System.err(23005): java.net.SocketException: Socket is not connected 08-27 14:26:57.531: W/System.err(23005): at java.net.Socket.checkOpenAndCreate(Socket.java:645) 08-27 14:26:57.531: W/System.err(23005): at java.net.Socket.getOutputStream(Socket.java:381) 08-27 14:26:57.531: W/System.err(23005): at com.example.stackoverflow.MainActivity.sendBytes(MainActivity.java:117) 08-27 14:26:57.531: W/System.err(23005): at com.example.stackoverflow.MainActivity$RepeatingTask.run(MainActivity.java:88) 08-27 14:26:57.531: W/System.err(23005): at java.util.Timer$TimerImpl.run(Timer.java:284) 08-27 14:26:57.531: E/TEMP(23005): IN REPEATINGTHREAD-INPUTJSON3:null

于 2012-08-27T20:39:21.197 回答