0

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

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

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

它仍然没有取代**&nbsp;**. 请帮助我。先感谢您。

4

6 回答 6

1
marked_up.toString().replaceAll("&nbsp","");
于 2013-01-09T05:49:02.217 回答
0

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

于 2013-01-09T05:51:29.107 回答
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);

于 2013-01-09T06:16:40.140 回答
0

使用这种方法,

Html.fromHtml(text);
title+= Html.fromHtml(jo.getString("Title"))+",";
于 2013-01-09T06:46:39.670 回答
0

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

desc= Html.fromHtml(jo.getString("Description"));
于 2013-01-10T05:43:39.940 回答
-1

我认为你应该从服务器端删除 ,<,>因为你必须每次检查不同的节点......所以改变服务器端的 webservice 代码......这对你来说是最好的......

于 2013-01-09T05:58:09.080 回答