Androd Html Demo js与java相互调用

sample demo

1.首先创建一个简单的android工程

2.修改layout文件main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >

<WebView
android:id="@+id/web"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>

<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:gravity="bottom|right">

<Button
android:layout_height="wrap_content"
android:id="@+id/Button_Send"
android:text="javatojs"
android:layout_width="400px"/>
</LinearLayout>
</RelativeLayout>


3.创建activity java类

package com.example.myandroidproj;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.os.Bundle;
import android.os.Handler;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.webkit.JavascriptInterface;
import android.webkit.WebView;
import android.widget.Button;
import android.util.Log;


public class IChartJsDemoActivity extends Activity {

private String TGA="IChartJsDemoActivity";
private WebView webView;
private Button send;
private Handler handler;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
initUI();
}

public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.ichart_js_demo, menu);
return true;
}

@SuppressLint("JavascriptInterface")
public void initUI(){
handler = new Handler();

webView = (WebView)this.findViewById(R.id.web);
webView.getSettings().setBuiltInZoomControls(true);
webView.getSettings().setJavaScriptEnabled(true);
webView.loadUrl("file:///android_asset/index.html");
//放置页面js对象
webView.addJavascriptInterface(new AndroidJsTool(), "androidJsTool");

send = (Button) this.findViewById(R.id.Button_Send);
send.setOnClickListener(new OnClickListener(){
public void onClick(View v) {
JSONArray arr =new JSONArray();
for(int i=0;i<10;i++){
JSONObject json =new JSONObject();
try {
json.put("name", "jasion");
json.put("country", "中国");
json.put("mobile", "1882529831"+i);
arr.put(json);
} catch (JSONException e) {
Log.e(TGA, e.getMessage());
e.printStackTrace();
}

}
String paramStr = arr.toString();
//java 调用js
webView.loadUrl("javascript:initData("+paramStr+")");
}

});
}

final class AndroidJsTool{
//必须要加上这个注释,要不然html页面就访问不到这个方法
@JavascriptInterface
public void runOnAndroidJavaScript(final String str){
handler.post(new Runnable(){
public void run() {
//测试js调用java
Log.i(TGA, "hello world:"+str);
}
});
}
}
}


4.创建html文件 index.html,index.html文件要放在工程目录底下的assets里面

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<script>
</script>
</head>
<body>
<table id="personTable">
<tr>
<td style="color:blue;">名称</td>
<td style="color:blue;">国家</td>
<td style="color:blue;">电话</td>
</tr>
</table>
<input type="text" id="wordsInput" /><br/>
<input type="button" id="sayWords" onclick="sayWord()" value="send to java" />
</body>

<script>
function sayWord(){
var words=document.getElementById("wordsInput").value;
androidJsTool.runOnAndroidJavaScript(words);//调用android的函数
}

function initData(jsonData){
var jsonobjs = eval(jsonData);
var table = document.getElementById("personTable");
for(var y=0; y<jsonobjs.length; y++){
var tr = table.insertRow(table.rows.length); //添加一行
//添加三列
var td1 = tr.insertCell(0);
var td2 = tr.insertCell(1);
td2.align = "center";
var td3 = tr.insertCell(2);
td3.align = "center";
//设置列内容和属性
td1.innerHTML = jsonobjs[y].name;
td2.innerHTML = jsonobjs[y].country;
td3.innerHTML = jsonobjs[y].mobile;
}
}
</script>
</html>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
抱歉,作为AI语言模型,我无法使用Java语言进行编程。但是,我可以提供一些基本的步骤,帮助你开始使用chatgpt3.5 API。 1. 获取chatgpt3.5 API密钥。你需要在chatgpt3.5的官网上注册,并且申请一个API密钥。这个密钥将会被用于验证你的API请求和跟踪API使用情况。 2. 安装Java开发环境。如果你还没有安装Java开发环境,你需要先安装Java SE Development Kit(JDK)。 3. 创建一个新的Android项目。你可以使用Android Studio创建一个新的Android项目。 4. 添加chatgpt3.5 API依赖库。你需要在你的Android项目中添加chatgpt3.5 API依赖库,以便在代码中调用chatgpt3.5 API。 5. 编写Java代码。你可以在你的Android项目中编写Java代码,在代码中调用chatgpt3.5 API。代码应该包括以下步骤: a. 创建一个HTTP请求。你可以使用Java的HttpURLConnection类创建一个HTTP请求,以便向chatgpt3.5 API发送请求。 b. 设置HTTP请求头。在HTTP请求中,你需要设置一些HTTP请求头,以便chatgpt3.5 API能够识别你的请求。例如,你需要设置“Authorization”头,将你的API密钥传递给API服务器。 c. 发送HTTP请求。一旦你设置了HTTP请求头,你就可以发送HTTP请求,并等待API服务器的响应。 d. 处理API响应。一旦API服务器返回响应,你需要解析响应并处理它。API响应可能是一个JSON格式的字符串,你需要使用JavaJSON库将其解析成一个对象,然后在你的应用程序中使用它。 6. 运行你的Android应用程序。一旦你完成了代码编写和调试,你可以在你的Android设备或模拟器上运行你的应用程序,测试它是否正常工作。 这些是使用Java调用chatgpt3.5 API的基本步骤。具体的实现方式可能因编程语言、API版本和开发环境而有所不同。如果你需要更具体的帮助,请查看chatgpt3.5 API文档和Java编程指南。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值