android - 从 Android 中的 json webservice 获取的 Webview 中的 HTML 数据显示不正确?

标签 android html json web-services webview

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

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

最佳答案

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

关于android - 从 Android 中的 json webservice 获取的 Webview 中的 HTML 数据显示不正确?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21620732/

相关文章:

javascript - Ajax 响应中的错误检查

android-ndk 构建无法链接 boost 系统

javascript - CSS 相当于 JavaScript 的 document.write?

Java 1.4 String.replaceAll 单引号问题

javascript - 如何在当前单击的按钮旁边附加用户名?

html - 网页周围的边框?

mysql - 在 json 列中保存数字或字符串而无需指定键?这可能吗?

android - 单例构造函数使我的应用程序崩溃

java - 使用 Picasso 加载图像

android - 以编程方式确定 Android 应用程序是否已付款