能解决问题的码农是个好码农,能高效处理问题的AI是个好AI
需求:
提取返回结果中字段alarmlevelname 不等于1的结果。
public static String ReadTagValue(String tagid, Date starttime,Date endtime)
{
try {
String appkey = "11";
String appsecret = "11";
java.sql.Statement statement;
SimpleDateFormat _dateformat = new SimpleDateFormat("yyyy-MM-dd");
String _mindate = _dateformat.format(starttime);
String _maxdate = _dateformat.format(endtime);
String _strurl = "http://ip:1111/api/test/" + tagid + "?MinDate="+_mindate+"&MaxDate="+_maxdate;
URL url = new URL(_strurl);
HttpURLConnection conn = (HttpURLConnection)url.openConnection();
conn.setRequestMethod("GET");
conn.setConnectTimeout(15000);
conn.setReadTimeout(60000);
String stringNow = _dateformat.format(new Date());
String uuid = UUID.randomUUID().toString();
String _raw = appkey+stringNow+uuid;
String _sig = RongzhiToDM.HMACSHA256(_raw, appsecret);
conn.setRequestProperty("Time", stringNow );
conn.setRequestProperty("Appkey", appkey);
conn.setRequestProperty("RequestId", uuid);
conn.setRequestProperty("Sign", _sig);
conn.connect();
int responseCode = conn.getResponseCode();
if(responseCode == 200)
{
InputStream is = conn.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(is,"UTF-8"));
StringBuffer sbf = new StringBuffer();
String temp = null;
while((temp = br.readLine()) != null)
{
sbf.append(temp);
sbf.append("\r\n");
}
String result = sbf.toString();
return result;
}
else
{
String result = "{ ERROR:"+ responseCode+", MSG:";
InputStream is = conn.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(is,"UTF-8"));
StringBuffer sbf = new StringBuffer();
String temp = null;
while((temp = br.readLine()) != null)
{
sbf.append(temp);
sbf.append("\r\n");
}
result += sbf.toString();
result +="}";
return result;
}
}
catch(Exception ex)
{
return "ERROR(API):"+ex.getMessage();
}
}
使用Cursor解决问题。
第一步
创建main.java文件
第二步
粘贴上面的代码
第三步
Ctrl+k 输入 提取alarmlevelname 不等于1的数据
AI帮助优化代码并完成了代码编写
private static String readInputStream(BufferedReader br) throws IOException {
StringBuilder sb = new StringBuilder();
String temp = null;
while ((temp = br.readLine()) != null) {
sb.append(temp);
sb.append("\r\n");
}
return sb.toString();
}
public static String ReadTagValue(String tagid, Date starttime, Date endtime) {
try {
String appkey = "11";
String appsecret = "11";
SimpleDateFormat _dateformat = new SimpleDateFormat("yyyy-MM-dd");
String _mindate = _dateformat.format(starttime);
String _maxdate = _dateformat.format(endtime);
String _strurl = "http://ip:1111/api/test"+"?MinDate=" + _mindate + "&MaxDate=" + _maxdate;
URL url = new URL(_strurl);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
conn.setConnectTimeout(15000);
conn.setReadTimeout(60000);
String stringNow = _dateformat.format(new Date());
String uuid = UUID.randomUUID().toString();
String _raw = appkey + stringNow + uuid;
String _sig = RongzhiToDM.HMACSHA256(_raw, appsecret);
conn.setRequestProperty("Time", stringNow);
conn.setRequestProperty("Appkey", appkey);
conn.setRequestProperty("RequestId", uuid);
conn.setRequestProperty("Sign", _sig);
conn.connect();
int responseCode = conn.getResponseCode();
if (responseCode == 200) {
try (InputStream is = conn.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(is, "UTF-8"))) {
String result = readInputStream(br);
JSONArray jsonArray = new JSONArray(result);
JSONArray filteredArray = new JSONArray();
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject jsonObject = jsonArray.getJSONObject(i);
if (!jsonObject.getString("alarmlevelname").equals("1")) {
filteredArray.put(jsonObject);
}
}
return filteredArray.toString();
}
} else {
try (InputStream is = conn.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(is, "UTF-8"))) {
String result = "{ ERROR:" + responseCode + ", MSG:";
result += readInputStream(br);
result += "}";
return result;
}
}
}catch(Exception ex) {
return "ERROR(API):"+ex.getMessage();
}
}