2

我试图弄清楚为什么我的输入流无法正常工作。我正在尝试连接到服务器并获取 JSON 字符串并将其保存到变量 inputJSON 中,但是 inputJOSN 为空,因为我的输入流无法正常工作。这行代码:

inputJSON = ConvertByteArrayToString(getBytesFromInputStream(inputStr));

似乎无法正常工作,我不确定为什么?我确实认为我的 getBytesFromInputStream() 方法中的长度变量存在问题。如果有,我不确定是不是这样或如何解决它。

这是我的 getBytesFromInputStream() 方法的代码:

public static byte[] getBytesFromInputStream(InputStream is)
        throws IOException {

        // Get the size of the file
        long length = is.available();
        Log.e(LOG_TAG, "INPUTSTREAM LENGTH:"+length); 
        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;
}

这是我用来设置 inputJSON 的线程,该线程在 onCreate() 中启动:

     public RepeatingThread() {
     }

     @Override
     public void run() { 
          try {
              //outputstrwr.write(outputJSONserv);  //UNCOMMENT IF NEED TO SEND DATA TO GET JSON BACK
              inputJSON = ConvertByteArrayToString(getBytesFromInputStream(inputStr));
          } catch (IOException e) {
              // TODO Auto-generated catch block
              e.printStackTrace();
          }

          Log.e(LOG_TAG, "IN REPEATINGTHREAD-INPUTJSON:" + inputJSON);
          refreshViewModels(inputJSON);
          mHandler.postDelayed(this, 3000);       
     }
}

这是 ConvertByteArrayToString() 的代码:

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

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

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

        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.start();
    Log.e(LOG_TAG, "Started Repeat Thread");
}

refreshViewModels() 的代码:

    public void refreshViewModels(String inputJSON) {

        try {
            ListView servicesListView = (ListView) this
                    .findViewById(R.id.profilesListView);

            String[] from = new String[] { "profilePic", "neighborName",
                    "tagLine" };
            int[] to = new int[] { R.id.avatar, R.id.username, R.id.email };

            // prepare the list of all records
            List<HashMap<String, Object>> fillMaps = new ArrayList<HashMap<String, Object>>();
            List<Profile> profiles = new ArrayList<Profile>();

            // Clear the position mapping list and reset it
            this.posMap.clear();
            Log.i(LOG_TAG, "NEW inputJSON: " + inputJSON);
            inputJSON = getPayloadStr(inputJSON);
            Log.i(LOG_TAG, "NEW inputJSON2: " + inputJSON);
            JSONArray profileArray = new JSONArray(inputJSON);

            for (int i=0; i<profileArray.length(); i++) { 
                JSONObject jsonObject = profileArray.getJSONObject(i);
                Gson gson = new Gson(); 
                Profile p = gson.fromJson(gson.toJson(jsonObject).toString(), Profile.class);
                profiles.add(p);
            }

            int pos = 0;

            // Creates the fillMaps list for the listAdapter
            Log.i(LOG_TAG, "Profiles size: " + profiles.size());
            //showToast("Profiles size: " + profiles.size());
            for (Profile p : profiles) {
                // Create mapping for list adapter
                HashMap<String, Object> map = new HashMap<String, Object>();
                map.put("profilePic",
                        p.getAttributeValue("photo")== null? "Not Set" : p
                                .getAttributeValue("photo"));
                map.put("neighborName",
                        p.getAttributeValue("Name") == null? "Not Set" : p
                                .getAttributeValue("Name"));
                map.put("tagLine",
                        p.getAttributeValue("TagLine") == null? "Not Set" : p
                                .getAttributeValue("TagLine"));
                fillMaps.add(map);

                // Reset the postion mapping
                this.posMap.put(pos++, p);

            }

            ListAdapter servicesListAdapter = new myAdapter(this, fillMaps,
                    R.layout.listitem, from, to);
            servicesListView.setAdapter(servicesListAdapter);

        } catch (Exception e) {
            Log.e(LOG_TAG, "Error making list adapter: " + e.getMessage());
        }

    }

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

任何帮助,将不胜感激。

4

2 回答 2

0

在多个 Java 平台上,我发现 InputStream.available() 不可靠。如果您查看Android API 文档,它会说同样的话。我会尽量避免这种方法。

于 2012-08-27T03:59:04.123 回答
0

首先,您应该必须验证InputStream(是否有数据 - 不要依赖available()方法。)并且您正在一次读取整个 流,因此无需使用 while循环。

我建议你使用BufferedReader

public static String getStringFromInputStream(InputStream is)
        throws IOException {
   StringBuffer sb=new StringBuffer();
   BufferedReader reader=null;
   String line=null;
   try{
      reader=new BufferedReader(new InputStreamReader(is));
      while( (line=reader.readLine()) !=null){
           sb.append(line);
      }
   }catch(Exception ex){
       //
   }finally{
      if(reader!=null){
          try{
              reader.close();
          }catch(Exception ex) { }
      }
   }
  return sb.toString();
}

或者返回字节[],

public static byte[] getBytesFromInputStream(InputStream is)

    .....
    ByteArrayOutputStream bos=new ByteArrayOutputStream();
    BufferedInputStream bin=new BufferedInputStream(is);

    int numRead=0;
    byte []bytes=new byte[1024];
    while( (numRead=bin.read(bytes))>0)
    {
        bos.write(bytes, 0, numRead);
    }
    bos.flush();
    byte []totalBytes=bos.toByteArray();
    .....
    return totalBytes;

}

于 2012-08-27T03:59:21.313 回答