我目前在我的可编程恒温器应用程序方面遇到问题。我运行了该应用程序,但没有以可接受的格式获取所需的数据。这些是我的作业指导和使用的代码。
在NetBeans中打开ProgrammableThermostat项目,然后编写采用标准Java编码约定以构建u03a1用户案例中指定的显示驱动程序的Java类:
第1部分–创建类并构建显示驱动程序
在NetBeans中打开ProgrammableThermostat项目,然后编写采用标准Java编码约定以构建u03a1用户案例中指定的显示驱动程序的Java类:
执行以下操作:
您的代码将需要使用Web服务读取以JSON格式存储的温度数据,以访问Capella Web服务器上存储的JSON信息。您将创建一个网络查询来检索数据。查询结果将作为JSON数据返回。 开发可从Web服务读取温度数据的Java网络类/方法。 开发将原始温度数据转换为Java对象的Java类/方法。 构建驱动程序以在Java控制台上显示温度数据。请注意以下几点: 您无需创建GUI。您的数据应显示在NetBeans Java控制台中。
http://media.capella.edu/BBCourse_Production/IT4774/temperature.json
我已经有了json-simple 1.1.1和org.json包。
我的代码:
package programmable.thermostat;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.ProtocolException;
import java.net.URL;
import java.util.Scanner;
import org.json.JSONArray;
import org.json.JSONObject;
import org.json.simple.parser.JSONParser;
public class ProgrammableThermostat {
public static void main(String[] args) throws MalformedURLException, ProtocolException, IOException, org.json.simple.parser.ParseException{
//Need to string info to initialize variable for later use
String info = null;
//Here we have the http of the site that was given to us
URL url = new URL("http://media.capella.edu/BBCourse_Production/IT4774/temperature.json");
//Forms an http connection to the URL called conn and opens it
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
//sets get request type
conn.setRequestMethod("GET");
//Opens connection to API
conn.connect();
//Gets the response code for format
int responseCode = conn.getResponseCode();
//if response code not 200 throws runtime exception
if(responseCode != 200)
throw new RuntimeException("HttpResponseCode: " +responseCode);
/* else the scanner opens the URL stream and reads in the code line by line*/
else
{
Scanner sc = new Scanner(url.openStream());
while(sc.hasNext()) {
info += sc.nextLine();
}
System.out.println("JSON data in string format!");
System.out.println(info);
sc.close();
}
JSONParser parse = new JSONParser();
JSONObject object = (JSONObject)parse.parse(info);
JSONArray thermostatInfo = (JSONArray)object.get("results");
//get data for results array
for(int i = 0; i < thermostatInfo.length(); i++)
{
JSONObject jsonObject_1 = (JSONObject)thermostatInfo.get(i);
System.out.println("Items in result array!");
System.out.println("Identifier: " + jsonObject_1.get("identifier"));
System.out.println("\nName: " + jsonObject_1.get("name"));
System.out.println("\nThermostat Time: " + jsonObject_1.get("thermostatTime"));
System.out.println("\nutcTime: " + jsonObject_1.get("utcTime"));
System.out.println("\nRuntime: " + jsonObject_1.get("runtime"));
System.out.println("\nStatus: " + jsonObject_1.get("status"));
}
}
}
执行此项目有两个预期结果。读取JSON数据的每一行并将其解析为不同的值以进行显示。第二部分是在屏幕上很好地显示数据。
我的应用程序的输出是:
JSON data in string format!
null{ "identifier": "318324702718", "name": "ProgrammableThermostat", "thermostatTime": "2015-02-11 15:58:03", "utcTime": "2015-02-11 20:58:03", "runtime": { "actualTemperature": 711, "actualHumidity": 42, }, "status": { "code": 0, "message": "" }}
Exception in thread "main" Unexpected token LEFT BRACE({) at position 4.
at org.json.simple.parser.JSONParser.parse(JSONParser.java:146)
at org.json.simple.parser.JSONParser.parse(JSONParser.java:81)
at org.json.simple.parser.JSONParser.parse(JSONParser.java:75)
at programmable.thermostat.ProgrammableThermostat.main(ProgrammableThermostat.java:52)
C:\Users\Deb\AppData\Local\NetBeans\Cache\8.2\executor-snippets\run.xml:53: Java returned: 1
BUILD FAILED (total time: 4 seconds)
对此将提供任何帮助!谢谢大家。