在HTML网页视图数据未显示正确的android系统中的JSON web服务gettinng? [英] HTML data not display correct in Webview gettinng from json webservice in android?
问题描述
我是从服务器使用JSON web服务和显示的WebView获取HTML数据
在iphone是完全符合屏幕尺寸,但在Android中显示不显示prefect
在这里,我放下手中的web服务链接以及Android和iPhone code和屏幕截图。
HomeActivity.java
块引用>公共类HomeActivity扩展活动
{
的WebView的WebView; ImageView的imagemenu;
@覆盖
公共无效的onCreate(捆绑savedInstanceState)
{
super.onCreate(savedInstanceState);
的setContentView(R.layout.activity_home);
imagemenu =(ImageView的)findViewById(R.id.imagemenu);
copyFile(HomeActivity.thisverdana.ttf);
新HomeAsynctask()执行();
网页流量=(的WebView)findViewById(R.id.homewebview);
webview.getSettings()setJavaScriptEnabled(真)。
。webview.getSettings()setLoadWithOverviewMode(真);
。webview.getSettings()setUseWideViewPort(真);
。webview.getSettings()setBuiltInZoomControls(真);
。webview.getSettings()setSupportZoom(真);
imagemenu.setOnClickListener(新OnClickListener()
{ @覆盖
公共无效的onClick(视图v)
{ 意图I =新意图(HomeActivity.this,HomeListActivity.class);
i.setFlags(i.FLAG_ACTIVITY_NO_ANIMATION);
startActivity(ⅰ); } });}
// AsyncTask的用于获取URL家数据
公共类HomeAsynctask扩展的AsyncTask<字符串,字符串,字符串>
{
串细节; @覆盖
在preExecute保护无效()
{
//装载机 } @覆盖
保护字符串doInBackground(字符串... PARAMS)
{
尝试
{
JsonParser jparser =新JsonParser();
字符串URL =http://www.bridge.co.at/webservices/services.php?method=content&uid=127;
字符串homedata = jparser.getdata(URL); Log.e(家庭数据,----->中+ homedata); JSONObject的jobject =新的JSONObject(homedata);
JSONArray jarray = jobject.getJSONArray(ClassVariable.HOME.CONTENT); 细节= jarray.getJSONObject(0)获得(ClassVariable.HOME.DETAIL)的ToString(); Log.e(详细信息,----->中+详细); }
赶上(例外五)
{
e.printStackTrace();
} 返回细节;
} @覆盖
保护无效onPostExecute(字符串结果)
{
字符串htmlData = getHtmlData(HomeActivity.this,结果);
webview.loadDataWithBaseURL(NULL,htmlData,text / html的,UTF-8,关于:空白);
}}私人布尔copyFile(上下文的背景下,字符串文件名)
{
布尔状态= FALSE;
尝试
{
FileOutputStream中出= context.openFileOutput(文件名,Context.MODE_PRIVATE);
InputStream的时间= context.getAssets()打开(文件名)。
//传输从输入文件到输出文件字节
字节[] buf中=新的字节[1024];
INT LEN;
而((LEN = in.read(BUF))大于0){
out.write(BUF,0,LEN);
}
//关闭流
out.close();
附寄();
状态= TRUE;
}
赶上(例外五)
{
的System.out.println(异常的copyFile ::+ e.getMessage());
状态= FALSE;
} 的System.out.println(copyFile状态::+状态); 返回状态;}私人字符串getHtmlData(上下文的背景下,字符串数据)
{ 线头=&LT; HEAD&GT;&LT;风格&GT; @字体面{FONT-FAMILY:'宋体'; SRC:URL('文件:///android_asset/fonts/verdana.ttf');}体{width=600;height=1024;margin:10px;font-family:'verdana';font-size:12px}</style></head>\";
字符串htmlData =&LT; HTML和GT;+头+&LT;身体GT;+数据+&LT; /身体GT;&LT; / HTML&gt;中; 返回htmlData;
}}
解决方案你有没有尝试使用Html.fromHtml(yourData)?也有在这里给出了一些相关的答案。 <一href=\"http://stackoverflow.com/questions/3235131/set-textview-text-from-html-formatted-string-resource-in-xml\">Set从XML HTML格式的字符串资源的TextView文本。
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; }
}
解决方案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.
这篇关于在HTML网页视图数据未显示正确的android系统中的JSON web服务gettinng?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!