Changeset 30234 in osm for applications/editors/josm/plugins/geochat/src
- Timestamp:
- 2014-01-27T00:58:04+01:00 (11 years ago)
- Location:
- applications/editors/josm/plugins/geochat/src/geochat
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
applications/editors/josm/plugins/geochat/src/geochat/ChatServerConnection.java
r29854 r30234 15 15 import java.util.Set; 16 16 17 import org.json.JSONArray; 18 import org.json.JSONException; 19 import org.json.JSONObject; 17 import javax.json.JsonArray; 18 import javax.json.JsonException; 19 import javax.json.JsonObject; 20 20 21 import org.openstreetmap.josm.Main; 21 22 import org.openstreetmap.josm.data.coor.CoordinateFormat; … … 92 93 String query = "whoami&uid=" + uid; 93 94 JsonQueryUtil.queryAsync(query, new JsonQueryCallback() { 94 public void processJson( J SONObject json ) {95 if( json != null && json. has("name") )95 public void processJson( JsonObject json ) { 96 if( json != null && json.get("name") != null ) 96 97 login(uid, json.getString("name")); 97 98 else if( userName != null && userName.length() > 1 ) … … 143 144 + nameAttr; 144 145 JsonQueryUtil.queryAsync(query, new JsonQueryCallback() { 145 public void processJson( J SONObject json ) {146 public void processJson( JsonObject json ) { 146 147 if( json == null ) 147 148 fireLoginFailed(tr("Could not get server response, check logs")); 148 else if( json. has("error") )149 else if( json.get("error") != null ) 149 150 fireLoginFailed(tr("Failed to login as {0}:", userName) + "\n" + json.getString("error")); 150 else if( !json.has("uid") )151 else if( json.get("uid") == null) 151 152 fireLoginFailed(tr("The server did not return user ID")); 152 153 else { 153 String name = json. has("name") ? json.getString("name") : userName;154 String name = json.get("name") != null ? json.getString("name") : userName; 154 155 login(json.getInt("uid"), name); 155 156 } … … 190 191 String query = "logout&uid=" + userId; 191 192 JsonQueryUtil.queryAsync(query, new JsonQueryCallback() { 192 public void processJson( J SONObject json ) {193 if( json != null && json. has("message") ) {193 public void processJson( JsonObject json ) { 194 if( json != null && json.get("message") != null) { 194 195 logoutIntl(); 195 196 } … … 245 246 query += "&to=" + URLEncoder.encode(targetUser, "UTF8"); 246 247 JsonQueryUtil.queryAsync(query, new JsonQueryCallback() { 247 public void processJson( J SONObject json ) {248 public void processJson( JsonObject json ) { 248 249 if( json == null ) 249 250 fireMessageFailed(tr("Could not get server response, check logs")); 250 else if( json. has("error") )251 else if( json.get("error") != null ) 251 252 fireMessageFailed(tr("Failed to send message:") + "\n" + json.getString("error")); 252 253 } … … 356 357 + "&lon=" + pos.lonToString(CoordinateFormat.DECIMAL_DEGREES) 357 358 + "&uid=" + userId + "&last=" + lastId; 358 J SONObject json;359 JsonObject json; 359 360 try { 360 361 json = JsonQueryUtil.query(query); … … 366 367 // fireLoginFailed(tr("Could not get server response, check logs")); 367 368 // logoutIntl(); // todo: uncomment? 368 } else if( json. has("error") ) {369 } else if( json.get("error") != null) { 369 370 fireLoginFailed(tr("Failed to get messages as {0}:", userName) + "\n" + json.getString("error")); 370 371 logoutIntl(); 371 372 } else { 372 if( json. has("users") ) {373 Map<String, LatLon> users = parseUsers(json.getJ SONArray("users"));373 if( json.get("users") != null) { 374 Map<String, LatLon> users = parseUsers(json.getJsonArray("users")); 374 375 for( ChatServerConnectionListener listener : listeners ) 375 376 listener.updateUsers(users); 376 377 } 377 if( json. has("messages") ) {378 List<ChatMessage> messages = parseMessages(json.getJ SONArray("messages"), false);378 if( json.get("messages") != null) { 379 List<ChatMessage> messages = parseMessages(json.getJsonArray("messages"), false); 379 380 for( ChatMessage m : messages ) 380 381 if( m.getId() > lastId ) … … 383 384 listener.receivedMessages(needReset, messages); 384 385 } 385 if( json. has("private") ) {386 List<ChatMessage> messages = parseMessages(json.getJ SONArray("private"), true);386 if( json.get("private") != null) { 387 List<ChatMessage> messages = parseMessages(json.getJsonArray("private"), true); 387 388 for( ChatMessage m : messages ) 388 389 if( m.getId() > lastId ) … … 396 397 } 397 398 398 private List<ChatMessage> parseMessages( J SONArray messages, boolean priv ) {399 private List<ChatMessage> parseMessages( JsonArray messages, boolean priv ) { 399 400 List<ChatMessage> result = new ArrayList<ChatMessage>(); 400 for( int i = 0; i < messages. length(); i++ ) {401 for( int i = 0; i < messages.size(); i++ ) { 401 402 try { 402 JSONObject msg = messages.getJSONObject(i);403 long id = msg.get Long("id");404 double lat = msg.get Double("lat");405 double lon = msg.get Double("lon");406 long timeStamp = msg.get Long("timestamp");403 JsonObject msg = messages.getJsonObject(i); 404 long id = msg.getJsonNumber("id").longValue(); 405 double lat = msg.getJsonNumber("lat").doubleValue(); 406 double lon = msg.getJsonNumber("lon").doubleValue(); 407 long timeStamp = msg.getJsonNumber("timestamp").longValue(); 407 408 String author = msg.getString("author"); 408 409 String message = msg.getString("message"); … … 411 412 incoming, message, new Date(timeStamp * 1000)); 412 413 cm.setPrivate(priv); 413 if( msg. has("recipient") && !incoming )414 if( msg.get("recipient") != null && !incoming ) 414 415 cm.setRecipient(msg.getString("recipient")); 415 416 result.add(cm); 416 } catch( J SONException e ) {417 } catch( JsonException e ) { 417 418 // do nothing, just skip this message 418 419 } … … 421 422 } 422 423 423 private Map<String, LatLon> parseUsers( J SONArray users ) {424 private Map<String, LatLon> parseUsers( JsonArray users ) { 424 425 Map<String, LatLon> result = new HashMap<String, LatLon>(); 425 for( int i = 0; i < users. length(); i++ ) {426 for( int i = 0; i < users.size(); i++ ) { 426 427 try { 427 JSONObject user = users.getJSONObject(i);428 JsonObject user = users.getJsonObject(i); 428 429 String name = user.getString("user"); 429 double lat = user.get Double("lat");430 double lon = user.get Double("lon");430 double lat = user.getJsonNumber("lat").doubleValue(); 431 double lon = user.getJsonNumber("lon").doubleValue(); 431 432 result.put(name, new LatLon(lat, lon)); 432 } catch( J SONException e ) {433 } catch( JsonException e ) { 433 434 // do nothing, just skip this user 434 435 } -
applications/editors/josm/plugins/geochat/src/geochat/JsonQueryCallback.java
r29584 r30234 2 2 package geochat; 3 3 4 import org.json.JSONObject;4 import javax.json.JsonObject; 5 5 6 6 /** … … 17 17 * @param json JSON parsed response or null if the query was unsuccessful. 18 18 */ 19 void processJson( J SONObject json );19 void processJson( JsonObject json ); 20 20 } -
applications/editors/josm/plugins/geochat/src/geochat/JsonQueryUtil.java
r29584 r30234 8 8 import java.net.MalformedURLException; 9 9 import java.net.URL; 10 import org.json.JSONException; 11 import org.json.JSONObject; 12 import org.json.JSONTokener; 10 11 import javax.json.Json; 12 import javax.json.JsonException; 13 import javax.json.JsonObject; 14 13 15 import org.openstreetmap.josm.Main; 14 16 … … 23 25 * Query the server synchronously. 24 26 * @param query Query string, starting with action. Example: <tt>get&lat=1.0&lon=-2.0&uid=12345</tt> 25 * @return Parsed J SONObject if the query was successful, <tt>null</tt> otherwise.27 * @return Parsed JsonObject if the query was successful, <tt>null</tt> otherwise. 26 28 * @throws IOException There was a problem connecting to the server or parsing JSON. 27 29 */ 28 public static J SONObject query( String query ) throws IOException {30 public static JsonObject query( String query ) throws IOException { 29 31 try { 30 32 String serverURL = Main.pref.get("geochat.server", "http://zverik.dev.openstreetmap.org/osmochat.php?action="); … … 40 42 throw new IOException("Empty response"); 41 43 try { 42 JSONTokener tokener = new JSONTokener(inp); 43 JSONObject result = new JSONObject(tokener); 44 return result; 45 } catch( JSONException e ) { 44 return Json.createReader(inp).readObject(); 45 } catch( JsonException e ) { 46 46 throw new IOException("Failed to parse JSON: " + e.getMessage()); 47 47 } finally { … … 75 75 76 76 private void doRealRun() { 77 JSONObject obj;77 JsonObject obj; 78 78 try { 79 79 obj = query(query);
Note:
See TracChangeset
for help on using the changeset viewer.