# NFCRequestProxy

Call the **NFCRequestProxy** SPI to proxy requests to the server.

## Method Signature

```java
interface NFCRequestProxy {
    String sendRequest(String type, String body);
}
```

## Request parameters

| Item | Type | Description | Required |
| --- | --- | --- | --- |
| type | String | Identify the type of this request. - GET\_PK: a public key retrieval interface. - APPLY: a card opening request. - TRANSFER: internal notification logic of the SDK. It is not recommended for the MPP to be aware of it. - SYNC\_TOKEN: a token status update request. - NOTIFY: a message retrieval request. | Required |
| body | String | The request body encapsulated within the SDK. **Note:** Do not modify this parameter. | Required |

## 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 null or no data is returned, the SDK will determine that the request has failed. | Required |

## Sample

```java
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);
                //Don't modify any response body
                return response;
            } else {
                is = httpURLConnection.getErrorStream();
                String response = readInputStream(is);
                Log.d(NFCHttpUrlConnectProxy.class.getName(), "POST Error Response Body :: " + response);
                //Don't 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();
    }
}
```

> **Note**: After the MPP client send a request to the MPP server through this SPI, the MPP server constructs a HTTPS request with the _body_, and calls the [**sdkProxy**](sdk_proxy) API to send the request to the Alipay+.