Android Open Source - NVS Server Handler






From Project

Back to project page NVS.

License

The source code is released under:

Apache License

If you think the Android project NVS listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.jmuenster.server;
/* w  w w. ja  va 2  s  . c o m*/
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;

import org.apache.http.HttpResponse;

import android.util.Log;

import com.jmuenster.extras.Constants;
import com.jmuenster.extras.StickyNote;

public class ServerHandler {
  
  HTTPClient client;
  
  public ServerHandler() {
    client = new HTTPClient();
  }
  
  public ArrayList<StickyNote> getAllNotes() {
    ArrayList<StickyNote> al = new ArrayList<StickyNote>();

    // creating StickyNote - needs to be something meaningful
    // needs to have current GPS coordinates :S
//    StickyNote sn = new StickyNote(null, null, -45868017, 170515441);
    // creating new JSONObject
//    JSONObject newNote = JSONParser.makeNewNote(sn, false, false, false, true, 2000);
    
    // sending off httpGet
    HttpResponse response = client.httpPost(Constants.SERVER_URL+Constants.SEARCH_EXTENSION, "{\"data\": [{\"rad\":\"1000000\"}]}");
    
    // checking response for errors
    if (checkResponse(response)) {
      if (JSONParser.retrieveObjects(response.getEntity())) {
        // grabbing all Sticky Notes
        al = JSONParser.getNotesList();
      }
    }
    return al;
  }
  
//  public ArrayList<StickyNote> getNotes(StickyNote sn, int radius) {
//    ArrayList<StickyNote> al = new ArrayList<StickyNote>();
//    boolean radPresent = false;
//    if (radius > 0) {radPresent = true;}
//    
//    // creating new JSONObject
//    JSONObject newNote = JSONParser.makeNewNote(sn, false, false, false, radPresent,  radius);
//    
//    // sending off httpGet
//    HttpResponse response = client.httpPost(Constants.SERVER_URL+Constants.SEARCH_EXTENSION, newNote);
//    
//    // checking response for errors
//    if (checkResponse(response)) {
//      if (JSONParser.retrieveObjects(response.getEntity())) {
//        // grabbing all Sticky Notes
//        al = JSONParser.getNotesList();
//      }
//    }
//    return al;
//  }
  
//  public StickyNote addNewNote(StickyNote sn) {
//    // creating new JSONObject
//    JSONObject newNote = JSONParser.makeNewNote(sn, true, true, true, false, 0);
//    
//    HttpResponse response = client.httpPost(Constants.SERVER_URL+Constants.SERVER_EXTENSION, newNote);
//    
//    StickyNote newSticky = new StickyNote("", "", 0, 0);
//    newSticky.setUnique_id(null);
//    // checking response for errors
//    if (checkResponse(response)) {
//      ArrayList<StickyNote> al = JSONParser.getNotesList();
//      return al.get(0);
//    }
//    return newSticky;
//  }
  
  public boolean deleteNote(String id) {
    HttpResponse response = client.httpDelete(Constants.SERVER_URL+Constants.SERVER_EXTENSION, id + "/");
    
    // checking response for errors
    if (checkResponse(response)) {
      return true;
    }
    return false;
  }
  
  /////////////////////////////////////////////////////////////////////////
  // doing this last
  public boolean updateNote(StickyNote snNew) {
    return false;
  }
  /////////////////////////////////////////////////////////////////////////
  
  private boolean checkResponse(HttpResponse response) {
    if ((response.getStatusLine().getStatusCode()+"").matches("20\\d")) {
      return true;
    } else {
      String answer = "";
      try {
        answer = convertStreamToString(response.getEntity().getContent());
      } catch (Exception e) {e.printStackTrace();}
      Log.i("HTTPErrorCode", response.getStatusLine().getStatusCode()+"");
      Log.i("ServerResponse", answer);
      return false;
    }
  }
  
  private static String convertStreamToString(InputStream is) {

        BufferedReader reader = new BufferedReader(new InputStreamReader(is));
        StringBuilder sb = new StringBuilder();
 
        String line = null;
        try {
            while ((line = reader.readLine()) != null) {
                sb.append(line + "\n");
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                is.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return sb.toString();
    }
}




Java Source Code List

com.jmuenster.db.DBHelper.java
com.jmuenster.db.PointDbAdapter.java
com.jmuenster.extras.Constants.java
com.jmuenster.extras.Point.java
com.jmuenster.extras.StickyNote.java
com.jmuenster.extras.Syncronisation.java
com.jmuenster.gps.GPSHandler.java
com.jmuenster.gps.MyLocationListener.java
com.jmuenster.map.BalloonItemizedOverlay.java
com.jmuenster.map.BalloonOverlayView.java
com.jmuenster.map.MyItemizedOverlay.java
com.jmuenster.map.MyMapView.java
com.jmuenster.poi.AddEditDelNote.java
com.jmuenster.poi.PointOfInterest.java
com.jmuenster.poi.ViewNote.java
com.jmuenster.server.HTTPClient.java
com.jmuenster.server.JSONParser.java
com.jmuenster.server.ServerHandler.java