NFCRequestProxy
The MPP app calls NFCRequestProxy SPI to proxy requests to the server.
Note:
- After the MPP client sends a request to the MPP server through this SPI, the MPP server constructs a HTTPS request with the body, and calls the sdkProxy API to send the request to the Alipay+.
- Your implementation must handle all request types listed above. If an unrecognized type is received, do not return
nullor an empty string, as doing so will cause the SDK to treat the request as failed with no indication of the error.
Method signature
copy
interface NFCRequestProxy {
String sendRequest(String type, String body);
}Request parameters
Item | Type | Description | Required |
type | String | Identify the type of this request. Valid values are:
| Yes |
body | String | The request body encapsulated within the SDK.
| Yes |
Response parameters
Item | Type | Description | Required |
/ | String | The data returned by the server. The returned data is a string without a specific name. The data must be returned to the SDK entirely. If | Yes |
Sample
copy
package com.example.alipaynfcexample;
import android.util.Log;
import androidx.annotation.NonNull;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.iap.android.nfc.listener.spi.NFCRequestProxy;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
public class NFCHttpUrlConnectProxy implements NFCRequestProxy {
public static String BASE_URL = "";
public String customerId = "";
public NFCHttpUrlConnectProxy() {
}
@Override
public String sendRequest(String type, String body) {
OutputStream os = null;
InputStream is = null;
try {
HttpURLConnection httpURLConnection = getHttpURLConnection();
Log.d(NFCHttpUrlConnectProxy.class.getName(), "nfcProxy URL :: " + BASE_URL);
Log.d(NFCHttpUrlConnectProxy.class.getName(), "POST Request CustomerId :: " + customerId);
Log.d(NFCHttpUrlConnectProxy.class.getName(), ("POST Request Body:: " + body));
os = httpURLConnection.getOutputStream();
//Convert the string body to byte array
os.write(getBodyData(body));
httpURLConnection.connect();
int responseCode = httpURLConnection.getResponseCode();
if (responseCode == 200) {
is = httpURLConnection.getInputStream();
String response = readInputStream(is);
Log.d(NFCHttpUrlConnectProxy.class.getName(), "POST Response Body :: " + response);
//Dont modify any response body
return response;
} else {
is = httpURLConnection.getErrorStream();
String response = readInputStream(is);
Log.d(NFCHttpUrlConnectProxy.class.getName(), "POST Error Response Body :: " + response);
//Dont modify any response body
return response;
}
} catch (Exception e) {
Log.d(NFCHttpUrlConnectProxy.class.getName(), ("Exception: " + e.getMessage()));
} finally {
try {
if (os != null) {
os.close();
}
if (is != null) {
is.close();
}
} catch (IOException e) {
Log.d(NFCHttpUrlConnectProxy.class.getName(), ("IOException: " + e.getMessage()));
}
}
return null;
}
private @NonNull HttpURLConnection getHttpURLConnection() throws IOException {
URL url = new URL(BASE_URL);
HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
httpURLConnection.setRequestMethod("POST");
httpURLConnection.setDoOutput(true);
httpURLConnection.setDoInput(true);
httpURLConnection.setUseCaches(false);
httpURLConnection.setRequestProperty("Content-Type", "application/json");
httpURLConnection.setRequestProperty("User-Agent", "Android");
httpURLConnection.setConnectTimeout(30000);
httpURLConnection.setReadTimeout(30000);
return httpURLConnection;
}
private byte[] getBodyData(String originBody) {
JSONObject jsonObject = JSON.parseObject(originBody);
return jsonObject.toJSONString().getBytes();
}
private String readInputStream(InputStream stream) throws IOException {
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
byte[] chunk = new byte[2048];
int length;
while ((length = stream.read(chunk)) != -1) {
if (length == chunk.length) {
outputStream.write(chunk);
continue;
}
byte[] lastChunk = new byte[length];
System.arraycopy(chunk, 0, lastChunk, 0, length);
outputStream.write(lastChunk);
}
return outputStream.toString();
}
}