1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236
| import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import javax.microedition.midlet.*;
import javax.microedition.io.Connector;
import javax.microedition.io.HttpConnection;
import javax.microedition.lcdui.*;
import org.json.me.JSONException;
import org.json.me.JSONObject;
/**
* A minimal example MIDlet implemented
* with Cell-ID look-up API.
*/
public class CellIdLookUpMidlet extends MIDlet
implements CommandListener, Runnable {
// **** Set the developer key here ****
private final static String API_KEY = "";
private Command exitCommand;
private Cell cell;
private Form form;
private StringItem itemLongitude, itemLatitude;
private StringItem itemAccuracy, itemName;
private double longitude, latitude, accuracy;
private String cellName;
public CellIdLookUpMidlet() {
cell = new Cell();
exitCommand = new Command("Exit", Command.EXIT, 1);
form = new Form(null);
itemLongitude = new StringItem("longitude: ",
Double.toString(longitude));
itemLatitude = new StringItem("latitude: ",
Double.toString(latitude));
itemAccuracy = new StringItem("accuracy: ",
Double.toString(accuracy));
itemName = new StringItem("name: ",cellName);
form.insert(0, itemLongitude);
form.insert(1, itemLatitude);
form.insert(2, itemAccuracy);
form.insert(3, itemName);
form.addCommand(exitCommand);
form.setCommandListener(this);
Thread t = new Thread(this);
t.start();
}
public void run() {
updatePosition();
}
private void updateForm() {
itemLongitude.setText(Double.toString(longitude));
itemLatitude.setText(Double.toString(latitude));
itemAccuracy.setText(Double.toString(accuracy));
itemName.setText(cellName);
}
protected void startApp() {
Display.getDisplay(this).setCurrent(form);
}
protected void pauseApp() {}
protected void destroyApp(boolean bool) {}
public void commandAction(Command cmd, Displayable d) {
if(cmd==exitCommand) {
destroyApp(false);
notifyDestroyed();
}
}
/**
* updatePosition
* The format of the position data is either xml or json.
* In this example we are using json
*/
public void updatePosition() {
StringBuffer url = new StringBuffer();
url.append("<a href="http://cellid.labs.ericsson.net/json/lookup"">http://cellid.labs.ericsson.net/json/lookup"</a>);
url.append("?cellid=").append(cell.getCellId());
url.append("&mnc=").append(cell.getMnc());
url.append("&mcc=").append(cell.getMcc());
url.append("&lac=").append(cell.getLac());
url.append("&key=").append(API_KEY);
try {
byte[] data = getHttp(url.toString());
if(data!=null) {
parseJSON(new String(data));
updateForm();
}
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* parseJSON
*
* For JSON support we are using org.json.me
* The JSON package can be downloaded from here:
* <a href="http://www.json.org/java/org.json.me.zip
">www.json.org/java/org.json.me.zip
</a> */
public void parseJSON(String jsonString) {
try {
JSONObject o = new JSONObject(jsonString);
JSONObject pos = o.getJSONObject("position");
this.longitude = pos.getDouble("longitude");
this.latitude = pos.getDouble("latitude");
this.accuracy = pos.getDouble("accuracy");
this.cellName = pos.optString("name");
} catch(JSONException e) {
e.printStackTrace();
}
}
static public byte[] getHttp(String url)
throws IOException {
HttpConnection c = null;
InputStream is = null;
byte[] data = null;
try {
c = (HttpConnection)Connector.open(url);
int rc = c.getResponseCode();
// handle the HTTP response codes used by the API
if(rc!=HttpConnection.HTTP_OK) {
switch(rc) {
case HttpConnection.HTTP_NO_CONTENT:
throw new IOException("The cell could not be "+
"found in the database");
case HttpConnection.HTTP_BAD_REQUEST:
throw new IOException("Check if some parameter "+
"is missing or misspelled");
case HttpConnection.HTTP_UNAUTHORIZED:
throw new IOException("Make sure the API key is "+
"present and valid");
case HttpConnection.HTTP_FORBIDDEN:
throw new IOException("You have reached the limit "+
"for the number of requests per day. The maximum "+
"number of requests per day is currently 500.");
case HttpConnection.HTTP_NOT_FOUND:
throw new IOException("The cell could not be found "+
"in the database");
default:
throw new IOException("HTTP response code: " + rc);
}
}
is = c.openInputStream();
int actual = 0;
int len = (int)c.getLength();
if(len>0) {
int bytesread = 0 ;
// the server returned a length so we can allocate
// a byte array directly
data = new byte[len];
// loop until there is nothing more to read or
// until buffer is full
while((bytesread != len) && (actual != -1)) {
actual = is.read(data, bytesread, len - bytesread);
bytesread += actual;
}
} else {
// the server is not returning a length
// so we need a ByteArrayOutputStream
ByteArrayOutputStream bos = new ByteArrayOutputStream();
// create a 256 bytes read buffer to
// improve reading performance
byte buf[] = new byte[256];
// loop for as long as we get data from the server
// and add it to the output stream
while((actual = is.read(buf, 0, 256))!=-1) {
bos.write(buf, 0, actual);
}
bos.flush();
data = bos.toByteArray();
}
} catch (ClassCastException e) {
throw new IllegalArgumentException("Not an HTTP URL");
} finally {
try { if (c != null) c.close(); } catch(IOException e) {};
try { if (is != null) is.close(); } catch(IOException e) {};
}
return data;
}
/**
* Cell class to handle cell id parameters.
* Note that this is Sony Ericsson specific code.
*/
public class Cell {
private String id;
private String lac;
private String mcc;
private String mnc;
public Cell() {
update();
}
public void update() {
try {
id = System.getProperty("com.sonyericsson.net.cellid");
lac = System.getProperty("com.sonyericsson.net.lac");
mcc = System.getProperty("com.sonyericsson.net.cmcc");
mnc = System.getProperty("com.sonyericsson.net.cmnc");
} catch(Exception e) {
e.printStackTrace();
}
}
public String getCellId() { return id; }
public String getLac() { return lac; }
public String getMcc() { return mcc; }
public String getMnc() { return mnc; }
}
} |
Partager