PayPal Android Step 2: Async Server Request


The complete sample code for this application (Android + Node server) is available in the PayPal Developer Github repository.

At this point the PayPal future payments button has been clicked, we have an auth code and metadata ID from the PayPal SDK, and we need to pass those on to our server to complete the future payment process.

In the background process below, we are doing a few things:

  • We set up the URI that for our server to be, which is hitting the /fpstore endpoint of our server running on localhost.
  • The JSON object that will be sent through is then set up, which contains the auth code and metadata ID.
  • The connection is then made. In the case of a successful request (200 / 201 range) we can expect a response back from the server. We read that response and then return it.
  • Lastly, we have a onPostExecute(...) method set up to handle that returned server string. In the case of this example, it's simply logged.
public class ServerRequest extends AsyncTask<String, Void, String> {
    protected String doInBackground(String[] params){
        HttpURLConnection connection = null;
            //set connection to connect to /fpstore on localhost
            URL u = new URL("");
            connection = (HttpURLConnection) u.openConnection();

            //set configuration details
            connection.setRequestProperty("Content-Type", "application/json");
            connection.setRequestProperty("Accept", "application/json");

            //set server post data needed for obtaining access token
            String json = "{\"code\": \"" + params[0] + "\", \"metadataId\": \"" + params[1] + "\"}";
            Log.i("JSON string", json);

            //set content length and config details
            connection.setRequestProperty("Content-length", json.getBytes().length + "");

            //send json as request body
            OutputStream outputStream = connection.getOutputStream();

            //connect to server

            //look for 200/201 status code for received data from server
            int status = connection.getResponseCode();
            switch (status){
                case 200:
                case 201:
                    //read in results sent from the server
                    BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
                    StringBuilder sb = new StringBuilder();
                    String line;
                    while ((line = bufferedReader.readLine()) != null){
                        sb.append(line + "\n");

                    //return received string
                    return sb.toString();

        } catch (MalformedURLException ex) {
            Log.e("HTTP Client Error", ex.toString());
        } catch (IOException ex) {
            Log.e("HTTP Client Error", ex.toString());
        } catch (Exception ex) {
            Log.e("HTTP Client Error", ex.toString());
        } finally {
            if (connection != null) {
                } catch (Exception ex) {
                    Log.e("HTTP Client Error", ex.toString());
        return null;

    protected void onPostExecute(String message){
        //log values sent from the server - processed payment
        Log.i("HTTP Client", "Received Return: " + message);