кто-нибудь знает, почему InputStream responseBody = conn.getInputStream();
дает мне ноль?
в принципе, ошибка здесь в первых нескольких строках кода.У меня есть спокойная API-ссылка XML, которую я пытаюсь проанализировать как объект JSON.
И logcat не очень полезен.Любая помощь будет принята с благодарностью
спасибо
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;
}