4

我想通过JSONArrayFromActivityHTML 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>

我应该如何从中获取HTMLActivity

4

3 回答 3

0

将文件的位置显式写入 HTML 文件中的每个位置

file:///android_asset/Cjs/dhxscheduler_mobile_rec.js
于 2012-10-22T12:48:56.460 回答
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 数组。

于 2012-11-05T05:04:09.793 回答
-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();
    }

请申请此代码

于 2012-10-30T09:34:18.323 回答