public class Http_Read_PLC_Value {
public static void main(String args[]){
String nextLine;
URL url = null;
URLConnection urlConn = nullone
InputStreamReader inStream = null;
BufferedReader buff = null;
try{
url = new URL("http://api.enda.com/devive/mac_address/password/read/MW/0");
urlConn = url.openConnection();
inStream = new InputStreamReader( urlConn.getInputStream());
buff= new BufferedReader(inStream);
while (true){
nextLine =buff.readLine();
if (nextLine !=null){
System.out.println(nextLine);
}
else break;
}
}
catch(MalformedURLException e) { System.out.println("Check URL :" + e.toString() ); }
catch(IOException e1) { System.out.println("Can't read : "+ e1.toString() ); }
}
}
控制台的输出:{"Result":[1572],"Status":"OK"}
bracked的值在0和0之间变化。 28800和布尔值。该值有时是一个字符(如[2]),有时是五个字符(如[27924]),有时[true]或[false]。当我运行该prg时,我需要一个字符串值的bracked。我怎么能得到?
答案 0 :(得分:0)
while (true){
nextLine =buff.readLine();
if (nextLine !=null){
System.out.println(nextLine);
}
else break;
// I added that line and solved my problem
System.out.println(nextLine.substring((Integer.valueOf(nextLine.indexOf('[')) + 1),(Integer.valueOf(nextLine.indexOf(']')))));
}
}
catch(MalformedURLException e) { System.out.println("Check URL :" + e.toString() ); }
控制台的输出:{"结果":[18627],"状态":"确定"} 18627
感谢您的关注