代码被挂起来调用getBytesFromInputStream()的方法

时间:2012-08-30 22:54:31

标签: java android

我不确定我的代码有什么问题,但每当我对getBytesFromInputStream()进行方法调用时,它都会根据logcat停止它们,我不知道为什么。在RepeatingTask TimerTask代码中调用getBytesFromInputStream()方法。这是代码:

变量声明:

public Socket socket = null;
public PrintWriter outputstrwr;
public OutputStream out = null; 
public DataOutputStream dataOutputStream = null;
public DataInputStream dataInputStream = null;
public DataInputStream dis = null;
public DataOutputStream dos = null;

public static InputStream inputStr = null;
private String inputJSON = "";
private String outputJSON = "";
private String outputJSONserv = "";

onCreate方法():

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    Log.e(LOG_TAG, "Before OnCreate() Try");
    try {
        Log.e(LOG_TAG, "In OnCreate() Try");
        socket = new Socket("23.23.175.213", 9000);  //Port 1337
        Log.e(LOG_TAG, "Created Socket");
        dataOutputStream = new DataOutputStream(socket.getOutputStream());
        Log.e(LOG_TAG, "Created DataOutputStream");
        dataInputStream = new DataInputStream(socket.getInputStream());
        Log.e(LOG_TAG, "Created DataInputStream");

        //out = new OutputStream();
        out = socket.getOutputStream();
        inputStr = socket.getInputStream();

        //Thread readjsonthrd = new Thread(new ReadJSONThread());
        //inputstrrd = new InputStreamReader(socket.getInputStream());

        p = new Profile();
        Log.e(LOG_TAG, "Created Profile Instance");

        //Gets the local profile via JSON and converts into Profile type
        Gson gson = new Gson();
        Log.e(LOG_TAG, "Created Gson Instance" + "GetProfileJSONStr:" + p.getProfileJSONStr());
        p = gson.fromJson(p.getProfileJSONStr(), Profile.class);
        setProfile(p);
        Log.e(LOG_TAG, "Converted Profile to JSON");

        //Gson gson = new Gson();
        Log.e(LOG_TAG, "Before: outputJSON = gson.toJson(p);");
        outputJSON = gson.toJson(p).toString();
        outputJSON = removeExcessStr(outputJSON);
        Log.e(LOG_TAG, "ProfilePicStr Base64:"+p.getProfilePicStr());

        outputJSON = outputJSON.replace("Name","name");
        outputJSON = outputJSON.replace("TagLine","message");
        outputJSON = outputJSON.replace("Title","title");
        outputJSON = outputJSON.replace("Company", "company");
        outputJSON = outputJSON.replace("Industry","industry");
        outputJSON = outputJSON.replace("WhatIDo","whatido");
        outputJSON = outputJSON.replace("WhoDoIWantToMeet","meetwho");
        outputJSON = outputJSON.replace("WHOOZNEAR_PROFILEPIC","photo");
        outputJSON = outputJSON.replaceAll("[c][o][n][t][e][n][t][:][/][/][a-zA-Z0-9]+[/][a-zA-Z0-9]+[/][a-zA-Z0-9]+[/][a-zA-Z0-9]+[/][a-zA-Z0-9]+", getPicBase64Str()); /*"helloworld2"*/

        if (!outputJSON.contains(",\"photo\":")) { 
            outputJSON = outputJSON.replace("}",",\"photo\":"+"\"IconnexUs\"}");
            outputJSON = outputJSON.replace("}",",\"photo\":"+"\""+getPicBase64Str()+"\"}");
            outputJSON = outputJSON.replace("}",",\"status\":\"enabled\"}");
        }
        else { 
            outputJSON = outputJSON.replace("}",",\"status\":\"enabled\"");
        }

        outputJSONserv = "{\"to\":\"broadcast\",\"type\":\"1\",\"payload\":"+outputJSON+"}";

        Log.e(LOG_TAG, "Created outputJSON:" + outputJSON);
        Log.e(LOG_TAG, "Created outputJSON Server:" + outputJSONserv);
        JSONObject outObject = new JSONObject();
        try {
            outObject.put("photo", "hello");
            outObject.put("type", "50");
            outObject.put("payload", outputJSON);
            outputJSON = gson.toJson(outObject).toString();
        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        Log.e(LOG_TAG, "Value of PROFILEPIC STRING FROM PROFILEMAP: "+profileMap.get("WHOOZNEAR_PROFILEPIC"));
        p.setProfilePicStr(ConvertandSetImagetoBase64(profileMap.get("WHOOZNEAR_PROFILEPIC")));
        //String headerJSON = gson.toJson(outObject).toString();
        outputJSON = outputJSON.substring(nthOccurrence(outputJSON, '{', 2)-1, nthOccurrence(outputJSON, '}', 1)-1);

        String input = "["+"Ryan"+"[";
        //"[foo".replaceAll(Pattern.quote("["), "\""); 
        String result = input.replaceAll(Pattern.quote("["), "\"");
        Log.e(LOG_TAG, "REGEX REPLACEALL:"+result);

        outputstrwr = new PrintWriter(new OutputStreamWriter(socket.getOutputStream()), true);
        outputstrwr.write(outputJSONserv);

        Log.e(LOG_TAG, "Base64 String:"+p.getProfilePicStr());
        Log.e(LOG_TAG, "Sent dataOutputStream");
    } catch (UnknownHostException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    Log.e(LOG_TAG, "Before initEventHandlers");
    initEventHandlers();
    Log.e(LOG_TAG, "After initEventHandlers");
    //refreshViewModels();

    /*Log.e(LOG_TAG, "Start Repeat Thread");
    rt = new Thread(new RepeatingThread());
    //rt = new RepeatingThread();
    rt.start();
    Log.e(LOG_TAG, "Started Repeat Thread");*/

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

TimerTask RepeatingTask(这是调用方法的地方):

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 {
                Log.e(LOG_TAG, "Hello String:"+inputStr);
                sendBytes(ConvertStringToByteArray(outputJSONserv), 0, ConvertStringToByteArray(outputJSONserv).length);
                Log.e(LOG_TAG, "Hello String2:"+hello);
                inputJSON = ConvertByteArrayToString(getBytesFromInputStream(inputStr));
                Log.e(LOG_TAG, "Hello String3:"+inputJSON);
            } 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(getBytesFromInputStream(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);   //UNCOMMENT THIS AFTER I GET IT WORKING
     }
}

getBytesFromInputStream():

public byte[] getBytesFromInputStream(InputStream is) throws IOException {
        //BEGINNING OF ADDED CODE FOR LENGTH
        //dis = new DataInputStream(is);
        //int length = dis.readInt();
        dataInputStream = new DataInputStream(is);
        int length = dataInputStream.readInt();
        //END OF ADDED CODE FOR LENGTH

        // Get the size of the file
        //int length = is.available();

        if (length > Integer.MAX_VALUE) {
        // File is too large
        }

        // Create the byte array to hold the data
        byte[] bytes = new byte[(int) length];

        // Read in the bytes
        int offset = 0;
        int numRead = 0;
        while (offset < bytes.length
        && (numRead = is.read(bytes, offset, bytes.length - offset)) >= 0) {
        offset += numRead;
        }
        // Ensure all the bytes have been read in
        if (offset < bytes.length) {
        throw new IOException("Could not completely stream ");
        }
        // Close the input stream and return bytes
        is.close();
        return bytes;
}

ConvertByteArrayToString():

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

似乎代码挂断了这一行:

inputJSON = ConvertByteArrayToString(getBytesFromInputStream(inputStr));
TimerTask RepeatingTask代码中的

。任何代码或指针都会有所帮助。

0 个答案:

没有答案