POST JSON to Server and Receive Response

I have a class, called FindMeARestaurantDAO, which contains methods that will make network calls to a server with AsyncTask inner classes in my Activity. I am having issues with my POST request Method, which is as follows:

@Override
    public String findMeARestaurant(List<CheckboxDTO> filters) {
        String inputLine;
        String errors;
        String result;
        try
        {
            // For each CheckboxDTO, get the Id and add it to JSONArray
            JSONArray checkboxJSONArray = new JSONArray();
            for (CheckboxDTO checkbox : filters)
            {
                try
                {
                    // Create JSONObject
                    JSONObject object = new JSONObject();
                    // Build the object
                    object.put("id", checkbox.getId());
                    // Add object to JSONArray
                    checkboxJSONArray.put(object);
                }
                catch (Exception e)
                {
                    e.printStackTrace();
                }

            }

            // Put JSONArray into wrapping JSONObject
            JSONObject serverObject = new JSONObject();
            try
            {
                // Create wrapping JSONObject
                serverObject.put("filtersIds", checkboxJSONArray);
            }
            catch (Exception e)
            {
                e.printStackTrace();
            }

            // Create URL object to hold URL
            URL findMeARestaurantURL = new URL(findMeARestaurantsURL);
            // Create connection to server
            HttpURLConnection connection = (HttpURLConnection) findMeARestaurantURL.openConnection();

            // Set request method and timeouts
            connection.setRequestMethod(FIND_RESTAURANT_REQUEST_METHOD);
            connection.setRequestProperty("Content-Type", "application/json; utf-8");
            connection.setRequestProperty("Accept", "application/json");
            connection.setChunkedStreamingMode(0);
            connection.setDoOutput(true);
            connection.setReadTimeout(READ_TIMEOUT);
            connection.setConnectTimeout(CONNECTION_TIMEOUT);

            // Connect to server
            connection.connect();

            // Create Writer
            Writer writer = new BufferedWriter(new OutputStreamWriter(connection.getOutputStream()));
            writer.write(String.valueOf(serverObject));

            // Close Writer
            writer.close();


            // Create InputStreamReader to read response from server
            InputStreamReader streamReader = new InputStreamReader(connection.getInputStream(), "utf-8");

            // Create BufferedReader to read through InputStream
            BufferedReader reader = new BufferedReader(streamReader);
            // Create StringBuilder to hold our result
            StringBuilder stringBuilder = new StringBuilder();

            // Check if the line read is null
            while ((inputLine = reader.readLine()) != null){
                stringBuilder.append(inputLine);
            }

            // Close out InputStream and BufferedReader
            reader.close();
            streamReader.close();

            // Set result to stringBuilder
            result = stringBuilder.toString();
            connection.disconnect();
        }
        catch (IOException e)
        {
            e.printStackTrace();
            return null;
        }

        return result;
    }

The method is POST and it appears to send the JSON serverObject to the server when I run my application, but it fails on the InputStreamReader and returns a FileNotFoundException. The server was set up by a partner for this project and says this portion of the API should be working. Am I missing something for the POST request? Do I need to be doing something differently for reading the server’s response? Any help would be greatly appreciated.

Leave a Reply

Your email address will not be published. Required fields are marked *