導航:首頁 > 操作系統 > androidviewpost

androidviewpost

發布時間:2022-08-07 15:23:22

❶ JS怎樣調用android本地原生方法

在android中調用本地js文件里的方法並得到返回值其方法如下:

Android中內置了WebKit模塊,而該模塊的java層視圖類就是WebView,所有需要使用Web瀏覽器功能的Android都需要創建該視圖類對象顯示和處理請求的網路資源。目前WebKit支持Http、Https、Ftp和JavaScript請求。下面是在Android中調用JavaScript方法以及如何在js中調用本地方法。

1、在Assets下放一個簡單的html文件jstest.html

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html40/strict.dtd">
<HTML>
<HEAD>
<meta name="viewport" content="width=device-width, target-densitydpi=device-dpi" />
<META http-equiv="Content-Type" content="text/html; charset=UTF-8">
<script>
function showMsg(){
alert("hello world!");
}
function showMsgInAndroid(){
myjs.showMsg('hello in android!');
}
</script>
</HEAD>
<BODY>
<span>測試js使用</span>

<button id='btntest' onclick='showMsgInAndroid()'>調用android方法</button>
</BODY>
</HTML>
2、布局文件main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
android:id="@+id/rl_main"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
>
<WebView
android:id="@+id/wv_test"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_above="@+id/btn_showmsg"/>
<Button
android:id="@+id/btn_showmsg"
android:layout_width="200dip"
android:layout_height="40dip"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:text="調用html中js方法"/>
</RelativeLayout>

3、然後是Activity,MainActivity.java
package com.harold.jstest;

import com.harold.base.JSKit;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.view.View.OnClickListener;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.widget.Button;

public class MainActivity extends Activity {

private WebView mWebView;
private Button btnShowInfo;
private JSKit js;
private Handler mHandler = new Handler();

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//初始化控制項
mWebView = (WebView) findViewById(R.id.wv_test);
btnShowInfo = (Button) findViewById(R.id.btn_showmsg);
//實例化js對象
js = new JSKit(this);
//設置參數
mWebView.getSettings().setBuiltInZoomControls(true);
//內容的渲染需要webviewChromClient去實現,設置webviewChromClient基類,解決js中alert不彈出的問題和其他內容渲染問題
mWebView.setWebChromeClient(new WebChromeClient());
mWebView.getSettings().setJavaScriptEnabled(true);
//把js綁定到全局的myjs上,myjs的作用域是全局的,初始化後可隨處使用
mWebView.addJavascriptInterface(js, "myjs");

mWebView.loadUrl("file:///android_asset/jstest.html");

btnShowInfo.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
mHandler.post(new Runnable() {
@Override
public void run() {
//調用 HTML 中的javaScript 函數
mWebView.loadUrl("javascript:showMsg()");
}
});
}
});
}

}

4、最後是綁定全局js的類JSKit.java
package com.harold.base;

import android.widget.Toast;

import com.harold.jstest.MainActivity;

public class JSKit {
private MainActivity ma;

public JSKit(MainActivity context) {
this.ma = context;
}

public void showMsg(String msg) {
Toast.makeText(ma, msg, Toast.LENGTH_SHORT).show();
}
}

例子比較簡單,代碼里都加了注釋,這里就不多說了,本示例用的本地的html,如果訪問網路中的網頁,別忘記在AndroidManifest.xml中加許可權
<uses-permission android:name="android.permission.INTERNET"/>

❷ android中handler.post();和view.post();有經驗的開發者受累解答下~~~

AsyncTask 分前台任務和後台任務, 後台任務是通過線程池實現的, 運行時你可以通過ddms查看

handler 處理方式很單一, 就是個消息處理. 處理線程是調用loop()方法的那條.
就想了這么多了...

❸ 如何在 Android 上用 Post 提交大量的數據

在 Android 上用 Post 提交大量的數據方法:
1.Android中實現
activity_main.xml部分
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >

<TextView
android:id="@+id/lblPostResult"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/butPost"
android:layout_centerHorizontal="true"
android:text="提交結果" />

<Button
android:id="@+id/butPost"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="25dp"
android:text="提交測試" />

</RelativeLayout>

//import部分
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import android.util.Log;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;
import java.util.Date;
import java.text.SimpleDateFormat;

//public class MainActivity extends Activity 部分
private Button m_butPost;
m_butPost=(Button)findViewById(R.id.butPost);
m_butPost.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
butPost_OnClick(v);
}
});

//提交測試
private void butPost_OnClick(View v){
//請求參數鍵-值對
String strRecSmsMsg="收簡訊測試";
//提交
RecSmsToPost(strRecSmsMsg);
openToast("提交測試完成");
}
//收到簡訊 後 提交
private void RecSmsToPost(String strRecSmsMsg){
String strNowDateTime=getNowDateTime("yyyy-MM-dd|HH:mm:ss");//當前時間
//參數
Map<String,String> params = new HashMap<String,String>();
params.put("RECSMSMSG", strRecSmsMsg);
//params.put("name", "李四");

//伺服器請求路徑
String strUrlPath = "http://192.168.1.9:80/JJKSms/RecSms.php" +"?DateTime=" + strNowDateTime;
String strResult=HttpUtils.submitPostData(strUrlPath,params, "utf-8");
m_lblPostResult.setText(strResult);

//openToast("提交完成");
}
//獲取當前時間
private String getNowDateTime(String strFormat){
if(strFormat==""){
strFormat="yyyy-MM-dd HH:mm:ss";
}
Date now = new Date();
SimpleDateFormat df = new SimpleDateFormat(strFormat);//設置日期格式
return df.format(now); // new Date()為獲取當前系統時間
}
//彈出消息
private void openToast(String strMsg){
Toast.makeText(this, strMsg, Toast.LENGTH_LONG).show();
}

HttpUtils 類部分
新建 類文件 HttpUtils 其中代碼如下:
import java.net.HttpURLConnection;
import java.net.URL;
import java.io.OutputStream;
import java.io.InputStream;
import java.util.Map;
import java.io.IOException;
import java.net.URLEncoder;
import java.io.ByteArrayOutputStream;

public class HttpUtils {
/*
* Function : 發送Post請求到伺服器
* Param : params請求體內容,encode編碼格式
*/
public static String submitPostData(String strUrlPath,Map<String, String> params, String encode) {

byte[] data = getRequestData(params, encode).toString().getBytes();//獲得請求體
try {

//String urlPath = "http://192.168.1.9:80/JJKSms/RecSms.php";
URL url = new URL(strUrlPath);

HttpURLConnection httpURLConnection = (HttpURLConnection)url.openConnection();
httpURLConnection.setConnectTimeout(3000); //設置連接超時時間
httpURLConnection.setDoInput(true); //打開輸入流,以便從伺服器獲取數據
httpURLConnection.setDoOutput(true); //打開輸出流,以便向伺服器提交數據
httpURLConnection.setRequestMethod("POST"); //設置以Post方式提交數據
httpURLConnection.setUseCaches(false); //使用Post方式不能使用緩存
//設置請求體的類型是文本類型
httpURLConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
//設置請求體的長度
httpURLConnection.setRequestProperty("Content-Length", String.valueOf(data.length));
//獲得輸出流,向伺服器寫入數據
OutputStream outputStream = httpURLConnection.getOutputStream();
outputStream.write(data);

int response = httpURLConnection.getResponseCode(); //獲得伺服器的響應碼
if(response == HttpURLConnection.HTTP_OK) {
InputStream inptStream = httpURLConnection.getInputStream();
return dealResponseResult(inptStream); //處理伺服器的響應結果
}
} catch (IOException e) {
//e.printStackTrace();
return "err: " + e.getMessage().toString();
}
return "-1";
}

/*
* Function : 封裝請求體信息
* Param : params請求體內容,encode編碼格式
*/
public static StringBuffer getRequestData(Map<String, String> params, String encode) {
StringBuffer stringBuffer = new StringBuffer(); //存儲封裝好的請求體信息
try {
for(Map.Entry<String, String> entry : params.entrySet()) {
stringBuffer.append(entry.getKey())
.append("=")
.append(URLEncoder.encode(entry.getValue(), encode))
.append("&");
}
stringBuffer.deleteCharAt(stringBuffer.length() - 1); //刪除最後的一個"&"
} catch (Exception e) {
e.printStackTrace();
}
return stringBuffer;
}

/*
* Function : 處理伺服器的響應結果(將輸入流轉化成字元串)
* Param : inputStream伺服器的響應輸入流
*/
public static String dealResponseResult(InputStream inputStream) {
String resultData = null; //存儲處理結果
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
byte[] data = new byte[1024];
int len = 0;
try {
while((len = inputStream.read(data)) != -1) {
byteArrayOutputStream.write(data, 0, len);
}
} catch (IOException e) {
e.printStackTrace();
}
resultData = new String(byteArrayOutputStream.toByteArray());
return resultData;
}

}

2.伺服器端的准備
在伺服器端我採用wamp方式,當然其它方式也可以。 創建RecSms.php 文件 內容如下:
<?php

require_once ('Log/LogHelper.php');

echo "你好" . "post </br>";

foreach($_REQUEST as $k=>$v){
echo $k;echo "--";
echo $v;echo "</br>";
}

WriteLog('你好 RecSms.php ---------');

foreach($_POST as $k=>$v){
WriteLog( $k .'--' .$v);
}
foreach($_GET as $k=>$v){
WriteLog( $k .'--' .$v);
}

?>

將提交的數據寫入Log\Log.php文件中。
其中LogHelper.php 為寫日誌文件,代碼文件如下:
<?php

function WriteLog($msg){
$fp = fopen("Log\Log.php", "a");//文件被清空後再寫入
if($fp)
{
date_default_timezone_set('asia/chongqing');
$time=date("H:i:s",strtotime("now"));
$flag=fwrite($fp, $time ." ".$msg ." \r\n");
fclose($fp);
}
}
?>

所有代碼已寫好。
開啟 wamp ,在Android中點擊 提交測試 則 在Log.php文件寫入提交的數據

❹ android webview中的loadUrl方法是get請求還是post

get請求,因為查詢api可以看到有個postUrl方法。查看源碼可以確定

❺ android 怎麼在oncreate中獲得view的坐標

這個問題大家肯定遇到過不止一次,其實很簡單,解決它也很容易,但是咱們追求的畢竟不是解決它,而是找到幾種方法去解決,並且這么解決的原理是什麼。
這里列出4種解決方案:
Activity/View#onWindowFocusChanged
這個函數的含義是:view已經初始化完畢了,寬/高已經准備好了,這個時候去獲取寬高是可以成功獲取的。但是需要注意的是onWindowFocusChanged函數會被調用多次,當Activity的窗口得到焦點和失去焦點時均會被調用一次,如果頻繁地進行onResume和onPause,那麼onWindowFocusChanged也會被頻繁地調用。
@Override
public void onWindowFocusChanged(boolean hasFocus) {
super.onWindowFocusChanged(hasFocus);
L.i("onWindowFocusChanged : v_view1.getWidth():" + v_view1.getWidth()
+ " v_view1.getHeight():" + v_view1.getHeight());
}123456

view.post(runnable)
通過post可以將一個runnable投遞到消息隊列的尾部,然後等待UI線程Looper調用此runnable的時候,view也已經初始化好了。
v_view1.post(new Runnable() {
@Override
public void run() {
L.i("post(Runnable) : v_view1.getWidth():" + v_view1.getWidth()
+ " v_view1.getHeight():" + v_view1.getHeight());
}
});1234567

ViewTreeObserver
使用ViewTreeObserver的眾多回調可以完成這個功能,比如使用OnGlobalLayoutListener這個介面,當view樹的狀態發生改變或者view樹內部的view的可見性發生改變時,onGlobalLayout方法將被回調,因此這是獲取view的寬高一個很好的時機。需要注意的是,伴隨著view樹的狀態改變等,onGlobalLayout會被調用多次。
v_view.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
L.i("ViewTreeObserver : v_view1.getWidth():" + v_view1.getWidth()
+ " v_view1.getHeight():" + v_view1.getHeight());
}
});1234567

再來詳細介紹一下ViewTreeObserver這個類,這個類是用來注冊當view tree全局狀態改變時的回調監聽器,這些全局事件包括很多,比如整個view tree視圖的布局,視圖繪制的開始,點擊事件的改變等等。還有千萬不要在應用程序中實例化ViewTreeObserver對象,因為該對象僅是由視圖提供的。
ViewTreeObserver類提供了幾個相關函數用來添加view tree的相關監聽器:
public void addOnDrawListener (ViewTreeObserver.OnDrawListener listener)
該函數為api 16版本中添加,作用是注冊在該view tree將要繪制時候的回調監聽器,注意該函數和相關的remove函數不能在監聽器回調的onDraw()中調用。
public void (ViewTreeObserver.OnGlobalFocusChangeListener listener)
該函數用來注冊在view tree焦點改變時候的回調監聽器。
public void addOnGlobalLayoutListener (ViewTreeObserver.OnGlobalLayoutListener listener)
該函數用來注冊在該view tree中view的全局布局屬性改變或者可見性改變時候的回調監聽器。
public void addOnPreDrawListener (ViewTreeObserver.OnPreDrawListener listener)
該函數用來注冊當view tree將要被繪制時候(view 的 onDraw 函數之前)的回調監聽器。
public void addOnScrollChangedListener (ViewTreeObserver.OnScrollChangedListener listener)
該函數用來注冊當view tree滑動時候的回調監聽器,比如用來監聽ScrollView的滑動狀態。
public void addOnTouchModeChangeListener (ViewTreeObserver.OnTouchModeChangeListener listener)
該函數用來注冊當view tree的touch mode改變時的回調監聽器,回調函數onTouchModeChanged (boolean isInTouchMode)中的isInTouchMode為該view tree的touch mode狀態。
public void addOnWindowAttachListener (ViewTreeObserver.OnWindowAttachListener listener)
api 18添加,該函數用來注冊當view tree被附加到一個window上時的回調監聽器。
public void (ViewTreeObserver.OnWindowFocusChangeListener listener)
api 18添加,該函數用來注冊當window中該view tree焦點改變時候的回調監聽器。
而且對應每一個add方法都會有一個remove方法用來刪除相應監聽器。

view.measure(int widthMeasureSpec, int heightMeasureSpec)
通過手動對view進行measure來得到view的寬/高,這種情況比較復雜,這里要分情況處理,根據view的layoutparams來分:
match_parent
直接放棄,無法measure出具體的寬/高。原因很簡單,根據view的measure過程,構造此種MeasureSpec需要知道parentSize,即父容器的剩餘空間,而這個時候我們無法知道parentSize的大小,所以理論上不可能測量處view的大小。
wrap_content
int widthMeasureSpec = View.MeasureSpec.makeMeasureSpec((1<<30)-1, View.MeasureSpec.AT_MOST);
int heightMeasureSpec = View.MeasureSpec.makeMeasureSpec((1<<30)-1, View.MeasureSpec.AT_MOST);
v_view1.measure(widthMeasureSpec, heightMeasureSpec);123

注意到(1<<30)-1,我們知道MeasureSpec的前2位為mode,後面30位為size,所以說我們使用最大size值去匹配該最大化模式,讓view自己去計算需要的大小。
具體的數值(dp/px)
這種模式下,只需要使用具體數值去measure即可,比如寬/高都是100px:
int widthMeasureSpec = View.MeasureSpec.makeMeasureSpec(100, View.MeasureSpec.EXACTLY);
int heightMeasureSpec = View.MeasureSpec.makeMeasureSpec(100, View.MeasureSpec.EXACTLY);
v_view1.measure(widthMeasureSpec, heightMeasureSpec);123

源碼和結果
demo代碼如下
xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="@+id/v_view1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@mipmap/ic_launcher"/>

<View
android:id="@+id/v_view2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@mipmap/ic_launcher"/>

</LinearLayout>12345678910111213141516171819

activity:
public class MainActivity extends BaseActivity{
private View v_view1;
private View v_view2;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

v_view1 = findViewById(R.id.v_view1);
v_view2 = findViewById(R.id.v_view2);

L.i("normal: v_view1.getWidth():" + v_view1.getWidth()
+ " v_view1.getHeight():" + v_view1.getHeight());

v_view1.post(new Runnable() {
@Override
public void run() {
L.i("post(Runnable) : v_view1.getWidth():" + v_view1.getWidth()
+ " v_view1.getHeight():" + v_view1.getHeight());
}
});

v_view1.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
L.i("ViewTreeObserver : v_view1.getWidth():" + v_view1.getWidth()
+ " v_view1.getHeight():" + v_view1.getHeight());
}
});

int widthMeasureSpec = View.MeasureSpec.makeMeasureSpec((1<<30)-1, View.MeasureSpec.AT_MOST);
int heightMeasureSpec = View.MeasureSpec.makeMeasureSpec((1<<30)-1, View.MeasureSpec.AT_MOST);
v_view1.measure(widthMeasureSpec, heightMeasureSpec);
L.i("measure : v_view1.getMeasuredWidth():" + v_view1.getMeasuredWidth()
+ " v_view1.getMeasuredHeight():" + v_view1.getMeasuredHeight());
v_view2.measure(widthMeasureSpec, heightMeasureSpec);
L.i("measure : v_view2.getMeasuredWidth():" + v_view2.getMeasuredWidth()
+ " v_view2.getMeasuredHeight():" + v_view2.getMeasuredHeight());

}

@Override
public void onWindowFocusChanged(boolean hasFocus) {
super.onWindowFocusChanged(hasFocus);
L.i("onWindowFocusChanged : v_view1.getWidth():" + v_view1.getWidth()
+ " v_view1.getHeight():" + v_view1.getHeight());
}
}
041424344454647484950

log日誌:
I/[PID:2659]: [TID:1] MainActivity.onCreate(line:28): normal: v_view1.getWidth():0 v_view1.getHeight():0
I/[PID:2659]: [TID:1] MainActivity.onCreate(line:50): measure : v_view1.getMeasuredWidth():144 v_view1.getMeasuredHeight():144
I/[PID:2659]: [TID:1] MainActivity.onCreate(line:53): measure : v_view2.getMeasuredWidth():16777215 v_view2.getMeasuredHeight():16777215
I/[PID:2659]: [TID:1] 2.onGlobalLayout(line:42): ViewTreeObserver : v_view1.getWidth():144 v_view1.getHeight():144
I/[PID:2659]: [TID:1] 1.run(line:34): post(Runnable) : v_view1.getWidth():144 v_view1.getHeight():144
I/[PID:2659]: [TID:1] MainActivity.onWindowFocusChanged(line:61): onWindowFocusChanged : v_view1.getWidth():144 v_view1.getHeight():144123456

界面:

小的為view_1,大的為view_2,從log日誌中就發現有問題了:view_2視圖使用measure之後計算出來的寬高是錯誤的,所以View類的視圖使用measure計算出來的結果是不準確的,這點需要特別特別注意。

❻ android webview 有獲取到表單提交數據嗎

是可以獲取到的,可以通過java的對象,將數據傳遞到java裡面,回調取得值。

❼ 請教android直接post請求登錄地址成功後,webview還是現實登錄界面

兩種情況,1是登錄沒有真的成功,2是登錄後跳轉有問題
仔細檢查代碼,調試

❽ android httppost類在哪

雖然在登錄系統中使用了Web Service與服務端進行交互。但是在傳遞大量的數量時,Web Service顯得有些笨拙。在本節將介紹移動電子相冊中使用的另外一種與資料庫交互的方法。直接發送HTTP GET或POST請求。這就要用到HttpGet、HttpPost以及HttpURLConnection這些類。
15.3.1 HttpGet類和HttpPost類
本節將介紹Android SDK集成的Apache HttpClient模塊。要注意的是,這里的Apache HttpClient模塊是HttpClient 4.0(org.apache.http.*),而不是Jakarta Commons HttpClient 3.x(org.apache.commons.httpclient.*)。
在HttpClient模塊中用到了兩個重要的類:HttpGet和HttpPost。這兩個類分別用來提交HTTP GET和HTTP POST請求。為了測試本節的例子,需要先編寫一個Servlet程序,用來接收HTTP GET和HTTP POST請求。讀者也可以使用其他服務端的資源來測試本節的例子。
假設192.168.17.81是本機的IP,客戶端可以通過如下的URL來訪問服務端的資源:
http://192.168.17.81:8080/querybooks/QueryServlet?bookname=開發
在這里bookname是QueryServlet的請求參數,表示圖書名,通過該參數來查詢圖書信息。
現在我們要通過HttpGet和HttpPost類向QueryServlet提交請求信息,並將返回結果顯示在TextView組件中。
無論是使用HttpGet,還是使用HttpPost,都必須通過如下3步來訪問HTTP資源。
1.創建HttpGet或HttpPost對象,將要請求的URL通過構造方法傳入HttpGet或HttpPost對象。
2.使用DefaultHttpClient類的execute方法發送HTTP GET或HTTP POST請求,並返回HttpResponse對象。
3.通過HttpResponse介面的getEntity方法返回響應信息,並進行相應的處理。

閱讀全文

與androidviewpost相關的資料

熱點內容
ipadminipdf閱讀 瀏覽:504
文件夾無限制壓縮會不會降低內存 瀏覽:410
榮耀怎樣創建文件夾 瀏覽:629
如何用本機登陸遠程伺服器地址 瀏覽:680
黃小鴨解壓文具盒 瀏覽:670
女程序員的轉行方法 瀏覽:881
東風啟辰車聯網安裝文件夾 瀏覽:524
華為怎麼設置app時間鎖 瀏覽:660
後宮app視頻怎麼下載 瀏覽:525
如何把圖片轉換從PDF格式 瀏覽:259
重寫和重載的區別java 瀏覽:234
expressvpnandroid 瀏覽:84
儲存卡被加密怎麼解除 瀏覽:169
地球怎麼壓縮直徑 瀏覽:780
金鏟鏟之戰伺服器爆滿怎麼進 瀏覽:160
同仁堂pdf 瀏覽:935
如何編譯原理課程教材 瀏覽:730
單片機控制顯示器 瀏覽:777
頂好花app下載怎麼找不到 瀏覽:989
手機命令大全 瀏覽:809