我遇到了 onResponse(JSONObject response) 不断返回 null 的问题。
我已经记录了 JSONObject.toString 作为请求(POST)中的主体传递,粘贴到 Postman 中,它可以工作;我从服务器(JSON 对象)得到有效响应。我也尝试过 StringRequest 但我没有任何运气。
这是我的PHP:
public function decryptTest2($param) {
$key = sodium_hex2bin($param['key']);
$nonce = sodium_hex2bin($param['nonce']);
$chiper = sodium_hex2bin($param['chiper']);
header('Content-Type: application/json; charset=utf-8');
$plaintext = sodium_crypto_secretbox_open($chiper, $nonce, $key);
if ($plaintext === false) {
throw new Exception("Bad ciphertext");
$json['error'] = "Not able to decrypt.";
echo json_encode($json['error']);
}
$json['success'] = array("decryptedText" => $plaintext);
echo json_encode($json['success']);
}
爪哇:
public void encryptRequest(String input) throws JSONException{
Encrypt e1 = new Encrypt();
SodiumAndroid sodium = new SodiumAndroid();
final LazySodiumAndroid lazySodium = new LazySodiumAndroid(sodium, StandardCharsets.UTF_8);
SecretBox.Lazy secretBoxLazy = (SecretBox.Lazy) lazySodium;
final byte[] nonce = lazySodium.randomBytesBuf(SecretBox.NONCEBYTES);
final Key key = secretBoxLazy.cryptoSecretBoxKeygen();
final String encryptedText = e1.encrypt(input, nonce, key);
JSONObject postMethod = new JSONObject();
postMethod.put("method", "decryptTest2");
JSONObject postParams = new JSONObject();
postParams.put("key", key.getAsHexString());
postParams.put("nonce", lazySodium.sodiumBin2Hex(nonce));
postParams.put("chiper", encryptedText);
postMethod.put("params", postParams);
final String requestBody = postMethod.toString();
//{"method":"decryptTest2","params":{"key":"E9C0828178CEC39A8AD713F12E36203AA31C8F4885B6AE55B0C033D592CE9075","nonce":"94B683D94504D86D3FA654652545CAD3AA3F4863640ABD91","chiper":"8ED8B09269A0CABE6F8F4D5C851B4B35E987892956EF7E"}}
//System.out.println("json: " + requestBody);
String URL = "http://api.domain.com/index.php";
JsonObjectRequest req = new JsonObjectRequest( Request.Method.POST, URL, postMethod,
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
System.out.println("json response: " + response);
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(getApplicationContext(), error.getMessage(),
Toast.LENGTH_LONG).show();
System.out.println("json response: " + error.getMessage());
}
}) {
@Override
public String getBodyContentType() {
return "application/json; charset=utf-8";
}
@Override
public Map<String, String> getHeaders() throws AuthFailureError {
Map<String, String> params = new HashMap<String, String>();
params.put("Content-Type", "application/json; charset=utf-8");
return params;
}
@Override
public byte[] getBody() {
try {
return requestBody == null ? null : requestBody.getBytes("utf-8");
} catch (UnsupportedEncodingException uee) {
VolleyLog.wtf("Unsupported Encoding while trying to get the bytes of %s using %s",
requestBody, "utf-8");
return null;
}
}
@Override
protected Response<JSONObject> parseNetworkResponse(NetworkResponse response) {
String responseString;
JSONObject jsonObject = null;
if (response != null) {
try {
responseString = new String(response.data, HttpHeaderParser.parseCharset(response.headers));
jsonObject = new JSONObject(responseString);
} catch (Exception e) {
e.printStackTrace();
}
}
return Response.success(jsonObject, HttpHeaderParser.parseCacheHeaders(response));
}
};
req.setRetryPolicy(new DefaultRetryPolicy(5000,
DefaultRetryPolicy.DEFAULT_MAX_RETRIES,
DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
requestQueue.add(req);
}
我究竟做错了什么?