【发布时间】:2021-10-23 23:44:57
【问题描述】:
所以我使用的是 Android Studio (Java),我必须与使用 Apache (xampp) 制作的网站进行通信。我放入了一个 json 对象 3 个变量。其中两个是字符串,另一个是我使用网站上的代码制作的签名 - https://developer.android.com/training/articles/keystore。
错误“Java.lang 类型的值
这是我的代码:
Android 工作室:
KeyStore ks = null;
byte[] signature = null;
try {
ks = KeyStore.getInstance("AndroidKeyStore");
ks.load(null);
KeyStore.Entry entry = ks.getEntry(alias, null);
if (!(entry instanceof KeyStore.PrivateKeyEntry)) {
Log.w("DEBUG_EC", "Not an instance of a PrivateKeyEntry");
}
Signature s = Signature.getInstance("SHA256withECDSA");
s.initSign(((KeyStore.PrivateKeyEntry) entry).getPrivateKey());
s.update(data.getBytes());
signature = s.sign();
} catch (KeyStoreException | CertificateException | NoSuchAlgorithmException | IOException | UnrecoverableEntryException | InvalidKeyException | SignatureException e) {
e.printStackTrace();
}
JSONObject object2 = new JSONObject();
try {
object2.put("id", data);
object2.put("key", key);
object2.put("signature", signature);
} catch (Exception e) {
Log.d("ID_DEBUG","Error");
e.printStackTrace();
}
JsonObjectRequest jsonObjectRequest2 = new JsonObjectRequest(Request.Method.POST, url, object2, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
Log.d("ID_DEBUG","String Response : "+ response.toString());
try {
String resultados = response.getString("result_text");
if(resultados.equals("missing parameters")){
Toast.makeText(getApplicationContext(), "Error!", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(getApplicationContext(),"Sucess!", Toast.LENGTH_SHORT).show();
}
} catch (JSONException e) {
Log.d("ID_DEBUG","Error");
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override public void onErrorResponse(VolleyError error) {
Log.e("ID_DEBUG", error.toString());
}
});
queue.add(jsonObjectRequest2);
我决定把部分php代码。 PHP:
<?php
function json_response($message = null, $code = 200) {
// clear the old headers
header_remove();
// set the actual code
http_response_code($code);
// set the header to make sure cache is forced
header("Cache-Control: no-cache, no-store");
header("Pragma: no-cache");
header("Expires: Sat, 26 Jul 1997 05:00:00 GMT"); // Date in the past
// treat this as json
header('Content-Type: application/json');
$status = array(
200 => '200 OK',
400 => '400 Bad Request',
422 => 'Unprocessable Entity',
500 => '500 Internal Server Error'
);
// ok, validation error, or failure
header('Status: '.$status[$code]);
// return the encoded json
return json_encode($message,JSON_UNESCAPED_SLASHES);
}
if (empty($_POST)) {
$_POST = json_decode(file_get_contents("php://input"), true) ? : [];
}
(some code ...)
$res = array();
if(empty($_POST['key']) || empty($_POST['signature'])) {
$res['result_code']=400;
$res['result_text']='missing parameters';
echo json_response($res);
} else {
$key=$_POST['key'];
$signature=$_POST['signature'];
$pubkeyid = openssl_pkey_get_public($key);
// state whether signature is okay or not
$ok = openssl_verify($data, $signature, $pubkeyid);
if ($ok == 1) {
if($key != NULL || $signature != NULL){
(some code ...)
$final_result = array();
$final_result['result_code']='200';
$final_result['result_text']='OK';
echo json_response($final_result);
}
} elseif ($ok == 0) {
$res['result_code']=400;
$res['result_text']='invalid signature';
echo json_response($res);
} else {
$res['result_code']=400;
$res['result_text']='error checking signature';
echo json_response($res);
}
// free the key from memory
openssl_free_key($pubkeyid);
}
}
?>
【问题讨论】:
-
看起来您在期望 JSON 时返回 HTML。
-
我不明白它是如何或在哪里发生的。我把我的PHP代码,它可能会有所帮助。