2012-06-20 30 views
0

我不明白:在android中,我沒有得到完整的字符串返回我輸入到stringbody。我檢查過這個字符串是完全保存的,但是當它傳遞給stringbody的時候它就會中斷。在同一個應用程序的桌面版本中,結果是成功的。但在Android中,字符串末尾的隨機部分會丟失。如果我嘗試在丟失後保存結果字符串,則該字符串會保存在同一個位置。我不使用任何口音或特殊字符。 的例子,從測試:StringBody沒有得到完整的字符串

第一次寫:

這是一個測試生物與我是書面方式在旅途中隨機消息。 如果這被削減,就像我期待的事情將被留在 了。讓我們看看會發生什麼。

一個保存保存此:

這是一個測試生物與我是書面方式在旅途中隨機消息。 如果這個被削減像我期待的東西將被排除在外

的第二次節省:

這是一個測試生物與我是書面方式在旅途中隨機消息。 如果這個被削減像我

的代碼是

protected void gravarPerfil() throws UnsupportedEncodingException { 

    MultipartEntity entity = new MultipartEntity(); 
    StringBody sAcerca = null, sLoc = null; 

    DefaultHttpClient client = new DefaultHttpClient(); 
    if (cookie != null) { 
     client.getCookieStore().addCookie(cookie); 
    } 

    HttpPost post = new HttpPost(address + "/dinamicas/editarPerfil"); 

    FileBody imagebin = null; 

    if (imgfile != null) { 
     imagebin = new FileBody(imgfile); 
    } 

    Log.v("msg", acerca.getText().toString()); 

這裏是我的filebody犯規加載整個字符串

sAcerca = new StringBody(acerca.getText().toString()); 
    sLoc = new StringBody(local.getText().toString()); 


    try { 

     entity.addPart("acerca", sAcerca); 
     entity.addPart("localizacao", sLoc); 
     if (imgfile != null) { 
      entity.addPart("foto", imagebin); 
     } 

     post.setEntity(entity); 

     HttpResponse response = client.execute(post); 


    } catch (Exception e) { 
     e.printStackTrace(); 
    } 
+0

我以前有過類似的問題。我完全從模擬器/設備上卸載了應用程序,然後再次嘗試 - 然後就沒事了。一旦進入藍月亮,事情就會被破壞,應用程序擦除會修復它。 – CelticParser

+0

卸載,重新安裝,創建新的模擬器,從Apache中刪除罐子,並安裝相同版本的新的...沒有什麼作品。 :\ – Lucky

+0

它仍然可能是你的服務器(在Android和服務器之間)所以[我認爲這可能是你需要的](http://stackoverflow.com/questions/8505811/image-gets-distorted-sometime-while-上傳) - 與您的問題類似,但在發送多部分數據時使用圖像。 – CelticParser

回答

0

沒有任何合理的解決方案,所以我周圍應用散步。我發現刪除的結尾總是在5 - 13個字符之間...所以我在字符串的末尾添加了15#,然後在服務器端刪除。這並不漂亮,但它的工作原理。

0

你的意思是,最終的日誌行部分給你一些奇怪的東西?:

Log.v("msg", response.toString()); 

,因爲它會。

試試這個:

InputStream responseStream = response..getEntity().getContent(); 
String textResponse = StreamConverter.convertStreamToString(responseStream); 
Log.v("msg", "Received:"+ textResponse); 


public class StreamConverter { 

    public static String convertStreamToString(InputStream inputStream) throws IOException { 
     if (inputStream != null) { 
      Writer writer = new StringWriter(); 

      char[] buffer = new char[1024]; 
      try { 
       Reader reader = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"),1024); 
       int n; 
       while ((n = reader.read(buffer)) != -1) { 
        writer.write(buffer, 0, n); 
       } 
      } finally { 
       inputStream.close(); 
      } 
      return writer.toString(); 
     } else { 
      return ""; 
     } 
    } 
} 
+0

我的問題是保存字符串到filebody。傳遞給filebody的字符串丟失了一些數據。在eclipse中運行的服務器也能正常工作,並且沒有錯誤。我在服務器上收到的消息是剪切版本。 – Lucky

相關問題