gpt4 book ai didi

php - 如何在android studio上使用webview获取json请求?

转载 作者:行者123 更新时间:2023-11-29 10:31:10 24 4
gpt4 key购买 nike

我在尝试从 Web View 上的提交获取 JsonRequest 时遇到了一些问题。我看过一些代码,但它们都不起作用,所以到目前为止我是从零开始的。我知道这个问题重复了,但我还是不明白。

我的 html.php

<?php
require_once('../Con/Conexion.php');
$username = $_POST['userName'];
$passw = $_POST['userPwd'];
$statement=mysqli_prepare($connection,"SELECT idTrabajador,`Nombre del Trabajador`,`Unidades por Vender` FROM `tablausuario` WHERE `Usuario`=? AND Contrasena=?");
mysqli_stmt_bind_param($statement,"ss",$username,$passw);
mysqli_stmt_execute($statement);
mysqli_stmt_store_result($statement);
mysqli_stmt_bind_result($statement,$idTrabajador,$name,$uxv);
$response = array();
$response["success"] = false;
while(mysqli_stmt_fetch($statement)){
$response["success"] = true;
$response["idTrabajador"] = $idTrabajador;
$response["Nombre del Trabajador"] = $name;
$response["Unidades por Vender"] = $uxv;
}
echo json_encode($response);
mysqli_close($connection);
?>

我知道要调用并URL Web查看其

    WebView web = (WebView) findViewById(R.id.miVisor3);
web.setWebViewClient(new ConsultaFacturas.MyWebViewClient());
WebSettings settings = web.getSettings();
settings.setJavaScriptEnabled(true);
web.loadUrl(URL_LISTA_CLIENTE);

但是,我怎样才能使用 webview 登录系统获得这个 Json 响应

try {
JSONObject jsonResponse = new JSONObject(response);
boolean success = jsonResponse.getBoolean("success");
if (success) {
String idTra = jsonResponse.getString("idTrabajador");
String naMe = jsonResponse.getString("Nombre del Trabajador");
String uxv = jsonResponse.getString("Unidades por Vender");
Intent openLog = new Intent(LoginActivity.this, MenuPrincipal.class);
openLog.putExtra("idTrabajador", idTra);
openLog.putExtra("Nombre del Trabajador", naMe);
openLog.putExtra("Unidades por Vender", uxv);
LoginActivity.this.startActivity(openLog);
} else {
AlertDialog.Builder builder = new AlertDialog.Builder(LoginActivity.this);
builder.setMessage("Error de Login, Verifique su conexion o los datos ingresados")
.setNegativeButton("Reintentar", null).create().show();

}
} catch (JSONException e) {
e.printStackTrace();
}

*更新*

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

WebView web = (WebView) findViewById(R.id.miVisor30);
web.setWebViewClient(new LoginActivity.MyWebViewClient());
WebSettings settings = web.getSettings();
settings.setJavaScriptEnabled(true);
web.addJavascriptInterface(new WebViewResultListener(), "ANDROID");**SHOWS ERROR**
ADNROID.onResult(request.status, request.responseText);**SHOWS ERROR**
web.loadUrl(URL_LISTA_CLIENTE);


}

private class MyWebViewClient extends WebViewClient {
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
}

public class WebViewResultListener {
@JavascriptInterface
public void onResult(int code, String response) {
if (code == HttpURLConnection.HTTP_OK) {
try {
JSONObject jsonResponse = new JSONObject(response);
boolean success = jsonResponse.getBoolean("success");
if (success) {
String idTra = jsonResponse.getString("idTrabajador");
String naMe = jsonResponse.getString("Nombre del Trabajador");
String uxv = jsonResponse.getString("Unidades por Vender");
Intent openLog = new Intent(LoginActivity.this, MenuPrincipal.class);
openLog.putExtra("idTrabajador", idTra);
openLog.putExtra("Nombre del Trabajador", naMe);
openLog.putExtra("Unidades por Vender", uxv);
LoginActivity.this.startActivity(openLog);
} else {
AlertDialog.Builder builder = new AlertDialog.Builder(LoginActivity.this);
builder.setMessage("Error de Login, Verifique su conexion o los datos ingresados")
.setNegativeButton("Reintentar", null).create().show();

}
} catch (JSONException e) {
e.printStackTrace();
}
} else {
Log.e("WebViewResultListener", "Error code=" + code);
}
}

}

@Override
public void onBackPressed() {
super.onBackPressed();
finish();
}
}

最佳答案

您无法直接从 WebView 读取 JSON 响应,但您可以使用 JavascriptInterface 将其从 WebView 内的 JavaScript 传递到 Java。

首先,创建JavascriptInterface类:

public static class WebViewResultListener {

@JavascriptInterface
public void onResult(int code, String response) {
if (code == HttpURLConnection.HTTP_OK) {
JSONObject jsonResponse = new JSONObject(response);
// and so on
} else {
Log.e("WebViewResultListener", "Error code=" + code);
}
}
}

并将其添加到WebView:

web.addJavascriptInterface(new WebViewResultListener(), "ANDROID");

WebView (JavaScript) 中,您要求用户输入用户名和密码。

WebView (JavaScript) 中,您将这些用户名和密码发送到您的服务器 (PHP) 并监听结果。当您收到结果时,将其从 JavaScript 传递到 Java:

ADNROID.onResult(request.status, request.responseText);

其中ANDROID是由addJavascriptInterface()方法添加的JavascriptInterface的名称。

关于php - 如何在android studio上使用webview获取json请求?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47384738/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com