【问题标题】:HTML data not display correct in Webview gettinng from json webservice in android?HTML 数据在从 android 中的 json webservice 获取的 Webview 中显示不正确?
【发布时间】:2026-01-23 04:20:07
【问题描述】:

我正在使用 json webservice 从服务器获取 HTML 数据并在 webview 中显示 在 iphone 中可以完美显示屏幕尺寸,但在 android 中显示不完美 在这里,我放下了 android 和 iphone 的 web 服务链接和代码以及屏幕截图。

HomeActivity.java

public class HomeActivity extends Activity 
{   
     WebView webview;   

     ImageView imagemenu;    


@Override
public void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_home);
    imagemenu=(ImageView)findViewById(R.id.imagemenu);
    copyFile(HomeActivity.this,"verdana.ttf");
    new HomeAsynctask().execute("");
    webview = (WebView) findViewById(R.id.homewebview);
    webview.getSettings().setJavaScriptEnabled(true);
    webview.getSettings().setLoadWithOverviewMode(true);
    webview.getSettings().setUseWideViewPort(true);
    webview.getSettings().setBuiltInZoomControls(true);
    webview.getSettings().setSupportZoom(true);


    imagemenu.setOnClickListener(new OnClickListener() 
    {

        @Override
        public void onClick(View v) 
        {

            Intent i = new Intent(HomeActivity.this,HomeListActivity.class);
            i.setFlags(i.FLAG_ACTIVITY_NO_ANIMATION);
            startActivity(i);

        }

    });

}


// Asynctask for getting the home data from url
public class HomeAsynctask extends AsyncTask<String, String,String>
{
    String detail;

    @Override
    protected void onPreExecute() 
    {   
           // loader    

    }

    @Override
    protected String doInBackground(String... params) 
    {
        try 
        {
            JsonParser jparser = new JsonParser();              
            String url="http://www.bridge.co.at/webservices/services.php?method=content&uid=127";               
            String homedata=jparser.getdata(url);

            Log.e("Home Data","----->"+homedata);

            JSONObject jobject = new JSONObject(homedata);
            JSONArray jarray =jobject.getJSONArray(ClassVariable.HOME.CONTENT);

            detail=jarray.getJSONObject(0).get(ClassVariable.HOME.DETAIL).toString();

            Log.e("Detail","----->"+detail);

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

        return detail;
    }

    @Override
    protected void onPostExecute(String result) 
    {   
        String htmlData=getHtmlData(HomeActivity.this,result);
        webview.loadDataWithBaseURL(null,htmlData,"text/html","utf-8","about:blank");
    }



}

private boolean copyFile(Context context,String fileName) 
{
    boolean status = false;        
    try 
    { 
        FileOutputStream out = context.openFileOutput(fileName, Context.MODE_PRIVATE);
        InputStream in = context.getAssets().open(fileName);
        // Transfer bytes from the input file to the output file
        byte[] buf = new byte[1024];
        int len;
        while ((len = in.read(buf)) > 0) {
            out.write(buf, 0, len);
        }
        // Close the streams
        out.close();
        in.close();
        status = true;
    } 
    catch (Exception e) 
    {
        System.out.println("Exception in copyFile:: "+e.getMessage());
        status = false;
    }

    System.out.println("copyFile Status:: "+status);

    return status;

}

private String getHtmlData(Context context, String data)
{

    String head = "<head><style>@font-face {font-family: 'verdana';src: url('file:///android_asset/fonts/verdana.ttf');}body {width=600;height=1024;margin:10px;font-family:'verdana';font-size:12px}</style></head>";
    String htmlData= "<html>"+head+"<body>"+data+"</body></html>" ;

    return htmlData;
}

}

【问题讨论】:

  • IOS 使用此代码 [wbView loadHTMLString:[NSString stringWithFormat:@"
    %@
    "[detailArray objectAtIndex:0] valueForKey:@"detail"]] baseURL:nil];

标签: android html json web-services webview


【解决方案1】:

您是否尝试过使用 Html.fromHtml(yourData)?这里也给出了一些相关的答案。 Set TextView text from html-formatted string resource in XML

【讨论】:

  • 但是对于 webview.loadDataWithBaseURL(null,htmlData,"text/html","utf-8",null) 中不允许的跨区字符串给出警告;
  • 你到底做了什么?你试过这个 webview.loadDataWithBaseURL(null,Html.fromHtml(htmlData),"text/html","utf-8",null);
  • WebView类型的loadDataWithBaseURL(String, String, String, String, String)方法不适用于参数(null, Spanned, String, String, null)
  • 几年前我做过类似的事情,但现在我没有代码,也不记得我做了什么。回家后我会添加评论。
  • 这是我在我的项目中所做的code final String mimeType = "text/html";最终字符串编码 = "UTF-8"; wvDesc = (WebView)findViewById(R.id.wvDesc); wvDesc.getSettings().setLayoutAlgorithm(LayoutAlgorithm.SINGLE_COLUMN); String desc = c.getDescription(arrData[i]); wvDesc.loadDataWithBaseURL("", desc, mimeType, encoding, "");code