inputstream restful api is returning null



有人知道InputStream responseBody = conn.getInputStream();为什么给我null吗?

基本上,这里的错误出现在代码的前几行。我有一个restful 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;
}

首先,很抱歉英语不好。

基本上,空指针异常很容易找到这个错误的来源。

检查您的连接是否成功连接。以下是参考代码。

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()

最新更新