开发者

Problem with strings containing " " in Stream.Publish, Android, Facebook

I'm trying to post on facebook wall through my adapted FBConnect (something similar to this http://www.mobisoftinfotech.com/blog/android/845/) and I'm having a issue with strings that contain ""

public void postToWall(String message){
     Bundle parameters = new Bundle();
     parameters.putString("message", message);

     String attachment;

     if (website == null) {
      attachment = "{\"name\":\""+ name + "\"," +
   "\"description\":\""+ description + "\"" +       
   "}";
     }
     else {
      attachment = "{\"name\":\""+ name + "\"," +
       "\"href\":\"" + website + "\"," +
       "\"description\":\""+ description + "\"" +       
       "}";
     }
     parameters.putString("attachment", attachment);

E.G. if description is 'lorem ipsum' it works quite fine but for instance if description is 'lorem ipsum "blhe" bazooka' t开发者_开发百科han it doesn't work.

I already tried to encode the attachment this way

parameters.putString("attachment", URLEncoder.encode(attachment));

but I get the same result. EDIT: actually with URLEncoder it doesn't work at all..

Any ideas on how to solve this?

Thank you.


Try using escapeHtml from apache commons lang: http://commons.apache.org/lang/api-2.4/org/apache/commons/lang/StringEscapeUtils.html#escapeHtml%28java.io.Writer,%20java.lang.String%29 and apply that method to all variables that possibly can contain unwanted characters.

attachment = "{\"name\":\""+ StringEcapeUtils.escapeHtml(name) + "\"," + "\"description\":\""+ StringEcapeUtils.escapeHtml(description) + "\"" +
"}";

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜