2017-06-04 20:15:04 +02:00
/ * *
* smmdbapi query
2017-06-05 17:16:39 +02:00
* api query , return all courses as ArrayList
2017-06-04 20:15:04 +02:00
* /
package application ;
import java.io.BufferedReader ;
import java.io.IOException ;
import java.io.InputStreamReader ;
import java.net.URL ;
import java.util.ArrayList ;
import com.eclipsesource.json.Json ;
import com.eclipsesource.json.JsonArray ;
import com.eclipsesource.json.JsonObject ;
import com.eclipsesource.json.JsonValue ;
import datatypes.SmmdbApiDataType ;
public class SmmdbApiQuery {
2017-06-05 17:16:39 +02:00
private String URL = " http://smmdb.ddns.net/api/getcourses? " ;
2017-06-04 20:15:04 +02:00
public SmmdbApiQuery ( ) {
//Auto-generated constructor stub
}
2017-06-05 17:16:39 +02:00
//start api query
2017-06-04 20:15:04 +02:00
public ArrayList < SmmdbApiDataType > startQuery ( ) {
ArrayList < Integer > courseIDs = new ArrayList < > ( ) ;
ArrayList < SmmdbApiDataType > course = new ArrayList < > ( ) ;
String output = " " ;
try {
2017-06-05 17:16:39 +02:00
URL apiUrl = new URL ( URL ) ;
2017-06-04 20:15:04 +02:00
BufferedReader ina = new BufferedReader ( new InputStreamReader ( apiUrl . openStream ( ) ) ) ;
output = ina . readLine ( ) ;
ina . close ( ) ;
} catch ( IOException e ) {
2017-06-05 17:16:39 +02:00
System . out . println ( " error while making api request or reading response " ) ;
2017-06-04 20:15:04 +02:00
e . printStackTrace ( ) ;
}
2017-08-25 14:57:15 +02:00
System . out . println ( URL ) ;
System . out . println ( " { \" courses \" : " + output + " } " ) ;
2017-06-05 17:16:39 +02:00
2017-08-25 14:57:15 +02:00
String apiOutput = " { \" courses \" : " + output + " } " ;
2017-06-04 20:15:04 +02:00
2017-08-25 14:57:15 +02:00
JsonArray items = Json . parse ( apiOutput ) . asObject ( ) . get ( " courses " ) . asArray ( ) ;
for ( JsonValue item : items ) {
int id , owner , coursetype , gamestyle , difficulty , lastmodified , uploaded , downloads , stars , hasimage , ispackage , updatereq ;
2017-06-05 17:16:39 +02:00
String nintendoid , title ;
2017-08-25 14:57:15 +02:00
//TODO add "courseTheme", "time", "autoScroll"
//geht
2017-06-05 17:16:39 +02:00
try {
2017-08-25 14:57:15 +02:00
id = item . asObject ( ) . getInt ( " id " , 0 ) ;
2017-06-05 17:16:39 +02:00
} catch ( Exception e ) {
id = 9 ;
}
2017-08-25 14:57:15 +02:00
//geht
2017-06-05 17:16:39 +02:00
try {
2017-08-25 14:57:15 +02:00
owner = item . asObject ( ) . getInt ( " owner " , 0 ) ;
2017-06-05 17:16:39 +02:00
} catch ( Exception e ) {
owner = 9 ;
}
2017-08-25 14:57:15 +02:00
//test
2017-06-05 17:16:39 +02:00
try {
2017-08-25 14:57:15 +02:00
coursetype = item . asObject ( ) . getInt ( " coursetype " , 0 ) ;
2017-06-05 17:16:39 +02:00
} catch ( Exception e ) {
coursetype = 9 ;
}
2017-08-25 14:57:15 +02:00
//geht
2017-06-05 17:16:39 +02:00
try {
2017-08-25 14:57:15 +02:00
gamestyle = item . asObject ( ) . getInt ( " gamestyle " , 0 ) ;
2017-06-05 17:16:39 +02:00
} catch ( Exception e ) {
2017-08-25 14:57:15 +02:00
gamestyle = 9 ;
2017-06-05 17:16:39 +02:00
}
2017-08-25 14:57:15 +02:00
//geht
2017-06-05 17:16:39 +02:00
try {
2017-08-25 14:57:15 +02:00
difficulty = item . asObject ( ) . getInt ( " difficulty " , 0 ) ;
2017-06-05 17:16:39 +02:00
} catch ( Exception e ) {
difficulty = 9 ;
}
2017-08-25 14:57:15 +02:00
//geht
2017-06-05 17:16:39 +02:00
try {
2017-08-25 14:57:15 +02:00
lastmodified = item . asObject ( ) . getInt ( " lastmodified " , 0 ) ;
2017-06-05 17:16:39 +02:00
} catch ( Exception e ) {
lastmodified = 9 ;
}
2017-08-25 14:57:15 +02:00
//geht
2017-06-05 17:16:39 +02:00
try {
2017-08-25 14:57:15 +02:00
uploaded = item . asObject ( ) . getInt ( " uploaded " , 0 ) ;
2017-06-05 17:16:39 +02:00
} catch ( Exception e ) {
uploaded = 9 ;
}
2017-08-25 14:57:15 +02:00
//gestrichen
2017-06-05 17:16:39 +02:00
try {
2017-08-25 14:57:15 +02:00
downloads = item . asObject ( ) . getInt ( " downloads " , 0 ) ;
2017-06-05 17:16:39 +02:00
} catch ( Exception e ) {
downloads = 9 ;
}
2017-08-25 14:57:15 +02:00
//geht
2017-06-05 17:16:39 +02:00
try {
2017-08-25 14:57:15 +02:00
stars = item . asObject ( ) . getInt ( " stars " , 0 ) ;
2017-06-05 17:16:39 +02:00
} catch ( Exception e ) {
stars = 9 ;
}
2017-08-25 14:57:15 +02:00
//gestrichen
2017-06-18 00:33:07 +02:00
try {
2017-08-25 14:57:15 +02:00
hasimage = item . asObject ( ) . getInt ( " hasimage " , 0 ) ;
2017-06-18 00:33:07 +02:00
} catch ( Exception e ) {
hasimage = 9 ;
}
2017-08-25 14:57:15 +02:00
//gestrichen
2017-06-05 17:16:39 +02:00
try {
2017-08-25 14:57:15 +02:00
ispackage = item . asObject ( ) . getInt ( " ispackage " , 0 ) ;
2017-06-05 17:16:39 +02:00
} catch ( Exception e ) {
ispackage = 9 ;
}
2017-08-25 14:57:15 +02:00
//gestrichen
2017-06-05 17:16:39 +02:00
try {
2017-08-25 14:57:15 +02:00
updatereq = item . asObject ( ) . getInt ( " updatereq " , 0 ) ;
2017-06-05 17:16:39 +02:00
} catch ( Exception e ) {
updatereq = 9 ;
}
2017-08-25 14:57:15 +02:00
//geht
2017-06-05 17:16:39 +02:00
try {
2017-08-25 14:57:15 +02:00
nintendoid = item . asObject ( ) . getString ( " nintendoid " , " " ) ;
2017-06-05 17:16:39 +02:00
} catch ( Exception e ) {
nintendoid = " notset " ;
}
try {
2017-08-25 14:57:15 +02:00
title = item . asObject ( ) . getString ( " title " , " " ) ; ;
2017-06-05 17:16:39 +02:00
} catch ( Exception e ) {
title = " notset " ;
}
2017-08-25 14:57:15 +02:00
course . add ( new SmmdbApiDataType ( id , owner , coursetype , gamestyle , difficulty , lastmodified , uploaded , downloads ,
2017-06-18 00:33:07 +02:00
stars , hasimage , ispackage , updatereq , nintendoid , title ) ) ;
2017-08-25 14:57:15 +02:00
}
2017-06-04 20:15:04 +02:00
return course ;
}
}