2014-02-07 25 views
3

我使用json webservice從服務器獲取HTML數據,並在web瀏覽器中顯示 在iphone中完美顯示屏幕尺寸,但在android中不顯示知情者 這裏我放下了web服務鏈接和代碼以及android和iphone的屏幕截圖。HTML數據在Webview中顯示不正確從android中的json webservice gettinng?

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

}

+0

IOS使用此代碼 [wbView loadHTMLString:[的NSString stringWithFormat:@ 「<體的iframe寬度= \ 」1024 \「 高度= \ 」597 \「>

%@
」[detailArray objectAtIndex:0] valueForKey:@ 「detail」]] baseURL:nil]; – Mahesh

回答

0

你嘗試使用Html.fromHtml(yourData)?還有一些相關的答案在這裏給出。 Set TextView text from html-formatted string resource in XML

+0

,但是在webview.loadDataWithBaseURL(null,htmlData,「text/html」,「utf-8」,null)中不允許跨越的字符串發出警告。 – Mahesh

+0

你究竟做了什麼?你試過這個webview.loadDataWithBaseURL(null,Html.fromHtml(htmlData),「text/html」,「utf-8」,null); – virtualpathum

+0

WebView類型中的loadDataWithBaseURL(String,String,String,String,String)方法不適用於參數(null,Spanned,String,String,null) – Mahesh