-1

anyone knows why InputStream responseBody = conn.getInputStream(); gives me null ?

basically, the error here is in the first few lines of code. I have a restful API XML link that i am trying to parse as a JSON object.

And the logcat isn't being very helpful. Any help would be greatly appreciated

thank you

ArrayList<Camera> cameras = new ArrayList<>();


        try {
            url = new URL("http://192.168.1.6:50323/Cam_Sql/webresources/com.mycompany.cam_sql.camerasfrench/1/250");
        } catch (MalformedURLException e) {
            e.printStackTrace();
        }

        URLConnection conn = null;

           try {
            conn = url.openConnection();

            InputStream responseBody = conn.getInputStream();
            InputStreamReader responseBodyReader = new InputStreamReader(responseBody, "UTF-8");
            JSONObject jsonObj = null;

            jsonObj = XML.toJSONObject(responseBodyReader.toString());

            JsonReader jsonReader = new JsonReader(responseBodyReader);
             jsonReader.beginObject(); // Start processing the JSON object

            String cameraLong = null;
            String cameraId = null;
            String cameraName = null;
            String cameraLat = null;

            while (jsonReader.hasNext()) { // Loop through all keys
                String key = jsonReader.nextName(); // Fetch the next key

                if (key.equals("camId")) { // Check if desired key
                    // Fetch the value as a String
                    cameraId = jsonReader.nextString();
                } else if (key.equals("camName")) {
                    cameraName = jsonReader.nextString();

                } else if (key.equals("cameraLong")) {
                    cameraLong = jsonReader.nextString();

                } else if (key.equals("cameraLat")) {
                    cameraLat = jsonReader.nextString();


                    cameras.add(new Camera(cameraName, cameraId, cameraLong, cameraLat));

                    // Do something with the value
                    // ...

                    break; // Break out of the loop
                } else {
                    jsonReader.skipValue(); // Skip values of other keys
                }
            }

        } catch (JSONException e) {
            System.out.println(e.toString());
        }
   }

    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }


    /*
    StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
    StrictMode.setThreadPolicy(policy);
    String db = null;
    try {

        Class.forName("net.sourceforge.jtds.jdbc.Driver");
        Connection dbCon = DriverManager.getConnection("jdbc:jtds:sqlserver://traffic-cam.database.windows.net:1433/Android;user=tyler@traffic-cam;password=Password!;");
       // db = dbCon.toString();
        int i = 0; //iterator
        int rows = 0;

        Statement stmt = dbCon.createStatement();
        String query = "SELECT COUNT(*) FROM CamerasFrench;";
        ResultSet rs = stmt.executeQuery(query);
        if(rs.next()){
            rows = Integer.parseInt(rs.getString(1));           //gets the amount of rows in database
        }
        //camInfo = new String[rows][4];

        stmt = dbCon.createStatement();
        query = "SELECT * FROM CamerasFrench;";
        rs = stmt.executeQuery(query);
        while(rs.next()){  //goes through every row, puts the data into the 2d array
            String cameraName = rs.getString("cam_name");
            String cameraLong = rs.getString("cam_longitude");
            String cameraLat = rs.getString("cam_latitude");
            String cameraId = rs.getString("cam_id");

            if (getResources().getConfiguration().locale.getLanguage() == "fr") {
                cameraName = rs.getString("cam_frName");
            }
            cameras.add(new Camera(cameraName, cameraId, cameraLong, cameraLat));
            //i++;
            //System.out.println("List Size: "+cameras.size());
        }
    }
    catch (Exception e){
        System.out.println(e.getMessage());
    }
    */
    if (cameras.size() > 0) {
        Collections.sort(cameras, new Comparator<Camera>() {
            @Override
            public int compare(final Camera object1, final Camera object2) {
                return object1.getCameraName().compareTo(object2.getCameraName());
            }
        });
    }
    return cameras;
}
BitcoinKing
  • 875
  • 7
  • 11

1 Answers1

0

First of all sorry for bad English.

Basically, null pointer exception is very easy to find where this error came from.

Check your connection is successfully connected or not. below is reference code.

HttpURLConnection urlConnection = (HttpURLConnection)url.openConnection();
// this is option.
urlConnection.setReadTimeout(30000);
urlConnection.setConnectTimeout(30000);
urlConnection.setRequestMethod("GET");
urlConnection.setUseCaches(false);
urlConnection.setAllowUserInteraction(false);
urlConnection.setRequestProperty("Content-Type", "application/json");
int responceCode = urlConnection.getResponseCode();

if (responceCode == HttpURLConnection.HTTP_OK) {} // using here. conn.getInputStream()
Wooyoung Tyler Kim
  • 484
  • 2
  • 9
  • 27