2012-08-27 76 views
2

我想弄清楚爲什麼我的inputstream工作不正常。我嘗試連接到服務器並獲取JSON字符串並將其保存到變量inputJSON中,但inputJOSN爲空,因爲我的輸入流不能正常工作。這行代碼:InputStream問題 - Android

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

下面是我對onCrea代碼TE():

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

任何幫助,將不勝感激。

+0

這是(InputStream的)*網絡*流?什麼是'is.available()'的*輸出*? – adatapost

回答

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

或返回的byte [],

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; 

}

+0

我想讀字節不是一個字符串 – user268397

0

在多個Java平臺上,我發現InputStream.available()是不可靠的。如果你看看the Android API docs,它說的是一樣的。我會盡量避免這種方法。