无法在我的Android应用程序中替换

时间:2013-01-09 05:46:55

标签: android json

android版:4.2 我的示例代码是:

   try {
   //HttpResponse response = httpClient.execute(httpGet, localContext);
   HttpResponse response = httpClient.execute(httpGet, localContext);
   HttpEntity entity = response.getEntity();
   text = getASCIIContentFromEntity(entity);

   text=text.replaceAll("&lt;", "<").replace("&gt;", ">").replace("&nbsp;", " ");

   int start=text.indexOf("<message>");
   start=start+9;
   int end=text.indexOf("</message>");
   text=text.substring(start, end);



   JSONArray ja = new JSONArray(text) ;
       // ITERATE THROUGH AND RETRIEVE CLUB FIELDS
    int n = ja.length();
    for (int i = 0; i < 1; i++) {
        // GET INDIVIDUAL JSON OBJECT FROM JSON ARRAY
     JSONObject jo = ja.getJSONObject(i);

     title+= jo.getString("Title")+",";
     url= jo.getString("URL");
     desc= jo.getString("Description");                             
    }
 } catch (Exception e) {
         return e.getLocalizedMessage();
}

问题:Varible desc(即我的json中的描述)在其内容中包含**&nbsp;**。我已使用以下代码将HTML转换为android中的字符串:

Spanned marked_up = Html.fromHtml(results);
tv2.setText(marked_up.toString(),BufferType.SPANNABLE);

仍然没有取代**&nbsp;**。 帮助我任何人PLZ。 谢谢你提前。

6 个答案:

答案 0 :(得分:1)

marked_up.toString().replaceAll("&nbsp","");

答案 1 :(得分:0)

使用tv2.setText(marked_up)代替tv2.setText(marked_up.toString(),BufferType.SPANNABLE);

答案 2 :(得分:0)

public static final String unescapeHTML(String s, int f){
        String [][] escape = {{  "&nbsp;"   , " " }};
        int i, j, k;

        i = s.indexOf("&", f);
        if (i > -1) {
            j = s.indexOf(";" ,i);
            f = i + 1;
                if (j > i) {
                  String temp = s.substring(i , j + 1);
                  k = 0;
                  while (k < escape.length) {
                    if (escape[k][0].equals(temp)) break;
                    else k++;
                  }
                  if (k < escape.length) {
                    s = s.substring(0 , i) + escape[k][1] + s.substring(j + 1);
                    return unescapeHTML(s, f); 
                  }
               }
         }   
         return s;
    }

将此功能用作text = unescapeHTML(text,0);

答案 3 :(得分:0)

使用此方法,

Html.fromHtml(text);
title+= Html.fromHtml(jo.getString("Title"))+",";

答案 4 :(得分:0)

尝试将以下行desc= jo.getString("Description");更改为:

desc= Html.fromHtml(jo.getString("Description"));

答案 5 :(得分:-1)

我认为您必须从服务器端删除&amp; nbsp,&lt;,&gt; ,因为每次都必须检查不同的节点....所以更改webservice的服务器端代码......这对你最好......