我想将JSONArray
从Activity
传递到HTML Script
我正在尝试以下方式,但我无法获取数据
HTML
的活动: -
public class DayViewJSActivity extends Activity {
WebView webviewobj;
String jsurl, eventobj;
JSONArray jArray = new JSONArray();
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
webviewobj = (WebView) findViewById(R.id.webView1);
webviewobj.getSettings().setJavaScriptEnabled(true);
webviewobj.addJavascriptInterface(new calendarInterface(),
"Androidfunction");
webviewobj.loadUrl("file:///android_asset/caljs.html");
JSONObject jobject = new JSONObject();
try {
jobject.put("id", 1);
jobject.put("start_date", "2012-10-22 09:00:00");
jobject.put("end_date", "2012-10-22 10:00:00");
jobject.put("text", "Demo scheduled");
jobject.put("details:", "");
} catch (JSONException e) {
e.printStackTrace();
}
jArray.put(jobject);
jsurl = "javascript:setData('" + jArray + "')";
System.out.println("JSURL IS :" + jsurl);
webviewobj.loadUrl(jsurl);
}
final class calendarInterface {
calendarInterface() {
}
public void run() {
webviewobj.loadUrl("javascript:setData( " + jArray + ")");
}
}
HTML: -
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
<meta name = "viewport" content = "initial-scale = 1.0, maximum-scale = 1.0, user-scalable = no">
<script src="Cjs/dhxscheduler_mobile_rec.js" type="text/javascript"></script>
<link rel="stylesheet" type="text/css" href="Cjs/dhxscheduler_mobile_rec.css">
<title></title>
<script type="text/javascript">
var eventData = null;
function setData(eventobj)
{
eventData = eventobj;
}
dhx.ready(function(){
dhx.ui.fullScreen();
dhx.ui(
{
view:"scheduler",
id:"scheduler"
}
);
var data = eventData;
$$("scheduler").$$("buttons").setValue("day");
$$("scheduler").parse(data);
});
</script>
</head>
<body>
</body>
</html>
我应该如何从HTML
获取Activity
中的值。
答案 0 :(得分:0)
将文件的位置显式地写在HTML文件中,例如
file:///android_asset/Cjs/dhxscheduler_mobile_rec.js
答案 1 :(得分:0)
在这里,简单来说,假设您有以下活动代码和接口,请查看以下代码......
public class htmldecoder extends Activity implements OnClickListener,TextWatcher
{
Button btsubmit; // this button in your xml file
WebView wvbrowser;
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.htmldecoder);
btsubmit=(Button)findViewById(R.id.btsubmit);
btsubmit.setOnClickListener(this);
wvbrowser=(WebView)findViewById(R.id.wvbrowser);
wvbrowser.setWebViewClient(new HelloWebViewClient());
wvbrowser.getSettings().setJavaScriptEnabled(true);
wvbrowser.getSettings().setPluginsEnabled(true);
wvbrowser.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
MyJavaScriptInterface myinterface=new MyJavaScriptInterface();
wvbrowser.addJavascriptInterface(myinterface,"interface");
webView.loadUrl("file:///android_asset/simple.html"); //use one html file for //testing put your html file in assets. Make sure that you done JavaScript methods to get //values for html content in html file .
}
public void onClick(View v)
{
if(btsubmit==v)
{
webView.loadUrl("javascript:showalert()");// call javascript method.
//wvbr
}
}
final class MyJavaScriptInterface {
MyJavaScriptInterface() {
}
public void sendValueFromHtml(String value) {
System.out.println("Here is the value from html::"+value);
}
}
}
如果您查看此代码,您会发现,我已将界面(MyJavaScriptInterface)添加到webview
wvbrowser.addJavascriptInterface(myinterface,"interface");
现在你的javascript需要用html编写
<script type="text/javascript">
//<![CDATA[
var n1;
function callme(){
n1=document.getElementById("FacadeAL").value;
}
function showalert(){
window.interface.sendValueFromHtml(n1);// this method calling the method of interface which //you attached to html file in android. // & we called this showalert javasript method on //submmit buttton click of android.
}
//]]>
</script>
&安培;确保在html中调用如下所示的callme
<input name="FacadeAL" id="FacadeAL" type="text" size="5" onblur="callme()"/>
希望,你会明白它的运作方式。愿这可以帮助你传递你的json数组。
答案 2 :(得分:-1)
String htmlResp = httpGet(link);
public static String httpGet(String url) {
HttpParams httpParameters = new BasicHttpParams();
HttpConnectionParams.setConnectionTimeout(httpParameters, 0);
HttpConnectionParams.setSoTimeout(httpParameters, 0);
DefaultHttpClient httpclient = new DefaultHttpClient(httpParameters);
try {
final String USER_AGENT = "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.2.8) Gecko/20100722 Firefox/3.6.8 (.NET CLR 3.5.30729)";
// final String ACCEPT_CHARSET = "ISO-8859-1,utf-8;q=0.7,*;q=0.7";
final String ACCEPT_CHARSET = "utf-8";
// final String ACCEPT =
// "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
final String ACCEPT = "text/html";
// HttpGet httpget = new HttpGet(url);
HttpGet httpget = new HttpGet(url);
// httpget.addHeader("User-Agent", "ie 6");
httpget.addHeader("User-Agent", USER_AGENT);
httpget.addHeader("Accept", ACCEPT);
httpget.addHeader("Accept-Charset", ACCEPT_CHARSET);
httpget.setParams(httpParameters);
HttpResponse response = httpclient.execute(httpget);
HttpEntity entity = response.getEntity();
return generateString(entity.getContent());
// return getResponseBody(response);
} catch (IOException e) {
e.printStackTrace();
return null;
}
}
private static String generateString(InputStream stream) {
StringBuilder sb = new StringBuilder();
InputStreamReader reader = null;
try {
reader = new InputStreamReader(stream, "utf-8");
BufferedReader buffer = new BufferedReader(reader);
String cur;
while ((cur = buffer.readLine()) != null) {
sb.append(cur + "\n");
}
} catch (UnsupportedEncodingException e1) {
e1.printStackTrace();
return null;
} catch (IOException e) {
e.printStackTrace();
return null;
} finally {
if (reader != null) {
try {
reader.close();
reader = null;
} catch (IOException e) {
e.printStackTrace();
}
}
if (stream != null) {
try {
stream.close();
stream = null;
} catch (IOException e) {
e.printStackTrace();
}
}
}
return sb.toString();
}
请申请此代码