Asked
Active
Viewed 4,936 times
3

i am getting HTML data from server using json webservice and display in webview in iphone is display perfectly with screen size but in android is not display prefect here i put down the webservice link and code and screen shot of android and 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;
}

}

Mahesh
  • 1,559
  • 6
  • 27
  • 57
  • IOS use this code [wbView loadHTMLString:[NSString stringWithFormat:@"
    %@
    "[detailArray objectAtIndex:0] valueForKey:@"detail"]] baseURL:nil];
    – Mahesh Feb 07 '14 at 06:32

1 Answers1

1

Did you try using Html.fromHtml(yourData)? Also there are some relevant answers given here. Set TextView text from html-formatted string resource in XML.

Community
  • 1
  • 1
virtualpathum
  • 753
  • 2
  • 11
  • 23
  • but is give warning for spanned string not allow in webview.loadDataWithBaseURL(null,htmlData,"text/html","utf-8",null); – Mahesh Feb 07 '14 at 06:43
  • what exactly you did? Did you try this webview.loadDataWithBaseURL(null,Html.fromHtml(htmlData),"text/html","utf-8",null); – virtualpathum Feb 07 '14 at 07:08
  • The method loadDataWithBaseURL(String, String, String, String, String) in the type WebView is not applicable for the arguments (null, Spanned, String, String, null) – Mahesh Feb 07 '14 at 07:15
  • I did a similar thing few years back but right now I don't have the code and can't remember exactly what I did. I'll add a comment once I get back home. – virtualpathum Feb 07 '14 at 07:33
  • This is what I did in my project `code` final String mimeType = "text/html"; final String encoding = "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` – virtualpathum Feb 10 '14 at 01:45